diff --git a/expr.cpp b/expr.cpp index 8373529d..3008923f 100644 --- a/expr.cpp +++ b/expr.cpp @@ -2681,6 +2681,22 @@ IndexExpr::GetLValue(FunctionEmitContext *ctx) const { if (!basePtr) return NULL; + // If the array index is a compile time constant, check to see if it + // may lead to an out-of-bounds access. + ConstExpr *ce = dynamic_cast(index); + const SequentialType *seqType = dynamic_cast(type); + assert(seqType != NULL); + int nElements = seqType->GetElementCount(); + if (ce != NULL && nElements > 0) { + int32_t indices[ISPC_MAX_NVEC]; + int count = ce->AsInt32(indices); + for (int i = 0; i < count; ++i) { + if (indices[i] < 0 || indices[i] >= nElements) + Warning(index->pos, "Array index \"%d\" may be out of bounds for " + "\"%d\" element array.", indices[i], nElements); + } + } + basePtr = lCastUniformVectorBasePtr(basePtr, ctx); ctx->SetDebugPos(pos); diff --git a/tests/array-1.ispc b/tests/array-1.ispc index f2073b2b..731d4deb 100644 --- a/tests/array-1.ispc +++ b/tests/array-1.ispc @@ -3,7 +3,7 @@ export uniform int width() { return programCount; } -static float x[2][1]; +static float x[1][2]; export void f_f(uniform float RET[], uniform float aFOO[]) { float a = aFOO[programIndex]; diff --git a/tests/array-scatter-vary.ispc b/tests/array-scatter-vary.ispc index d8d7b7bb..07527519 100644 --- a/tests/array-scatter-vary.ispc +++ b/tests/array-scatter-vary.ispc @@ -13,7 +13,7 @@ export void f_fu(uniform float RET[], uniform float aFOO[], uniform float b) { } -export void result(uniform float RET[4]) { +export void result(uniform float RET[]) { RET[programIndex] = 0; RET[3] = 4; RET[4] = 5;