Issue warning when compile-time constant out-of-bounds array index is used.

Issue #98.
Also fixes two examples that had bugs of this type that this warning
  uncovered!
This commit is contained in:
Matt Pharr
2011-09-13 14:42:20 -07:00
parent dd153d3c5c
commit 6734021520
3 changed files with 18 additions and 2 deletions

View File

@@ -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<ConstExpr *>(index);
const SequentialType *seqType = dynamic_cast<const SequentialType *>(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);

View File

@@ -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];

View File

@@ -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;