From 9e7773a0f9ca7af2d1a2281c4cd510c46ea71966 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Thu, 14 Oct 2021 18:15:04 +0200 Subject: [PATCH] Add warning for constant out of bound array access --- oscar64/Errors.h | 1 + oscar64/InterCodeGenerator.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/oscar64/Errors.h b/oscar64/Errors.h index a2f9fe1..73b80d0 100644 --- a/oscar64/Errors.h +++ b/oscar64/Errors.h @@ -16,6 +16,7 @@ enum ErrorID EWARN_GENERIC = 2000, EWARN_CONSTANT_TRUNCATED, EWARN_UNKNOWN_PRAGMA, + EWARN_INDEX_OUT_OF_BOUNDS, EWARN_SYNTAX, EERR_GENERIC = 3000, diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index 2004926..477722e 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -1119,6 +1119,16 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper); vr = TranslateExpression(procType, proc, block, exp->mRight, breakBlock, continueBlock, inlineMapper); + if (vl.mType->mType == DT_TYPE_ARRAY && exp->mRight->mType == EX_CONSTANT && exp->mRight->mDecValue->mType == DT_CONST_INTEGER) + { + if (vl.mType->mFlags & DTF_DEFINED) + { + int64 index = exp->mRight->mDecValue->mInteger; + if (index < 0 || index * vl.mType->mBase->mSize >= vl.mType->mSize) + mErrors->Error(exp->mLocation, EWARN_INDEX_OUT_OF_BOUNDS, "Constant array index out of bounds"); + } + } + vl = Dereference(proc, block, vl, vl.mType->mType == DT_TYPE_POINTER ? 0 : 1); vr = Dereference(proc, block, vr);