From fa83b7df5a0711bf5f4a5162cc33b0a4123a53c7 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Thu, 22 Oct 2026 15:06:13 +0000 Subject: fix(arrays): reject inverted bounds and check array allocation Constant inverted bounds are a compile error and other bounds are checked at run time. Allocation sizes use overflow-checked arithmetic, and a NULL malloc (including the BYVAL copy) reports that the array is too large. Co-Authored-By: Claude Sonnet 5.5 --- include/cps/ArrayHandler.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'include') diff --git a/include/cps/ArrayHandler.h b/include/cps/ArrayHandler.h index a09e708..f2d3490 100644 --- a/include/cps/ArrayHandler.h +++ b/include/cps/ArrayHandler.h @@ -52,6 +52,15 @@ class ArrayHandler { std::vector Lows, std::vector Highs, llvm::Value *&TotalElements); + // Validates every dimension (upper >= lower) and returns the allocation + // size in bytes, trapping on overflow. Constant bounds were already checked + // at compile time; the others get a runtime check. + llvm::Value *emitCheckedByteSize(const std::string &Name, + const std::vector &Lows, + const std::vector &Highs, + llvm::Constant *ElementSize, int Line); + // Traps with "Array NAME is too large" when Ptr (a malloc result) is null. + void emitAllocCheck(llvm::Value *Ptr, const std::string &Name, int Line); bool emitCheckedIndices(const std::string &Name, const std::vector> &IndexExprs, int Line, -- cgit v1.3.1-4-g156e