Issue errors if array dimensions are negative or too large to fit in 32 bits.

This commit is contained in:
Matt Pharr
2011-12-14 15:39:12 -08:00
parent 1fa6520cb6
commit 17fdab2793
3 changed files with 49 additions and 13 deletions

View File

@@ -947,10 +947,16 @@ direct_declarator
{
int size;
if ($1 != NULL && lGetConstantInt($3, &size, @3, "Array dimension")) {
Declarator *d = new Declarator(DK_ARRAY, Union(@1, @4));
d->arraySize = size;
d->child = $1;
$$ = d;
if (size < 0) {
Error(@3, "Array dimension must be non-negative.");
$$ = NULL;
}
else {
Declarator *d = new Declarator(DK_ARRAY, Union(@1, @4));
d->arraySize = size;
d->child = $1;
$$ = d;
}
}
else
$$ = NULL;
@@ -1141,10 +1147,16 @@ direct_abstract_declarator
| '[' constant_expression ']'
{
int size;
if (lGetConstantInt($2, &size, @2, "Array dimension")) {
Declarator *d = new Declarator(DK_ARRAY, Union(@1, @3));
d->arraySize = size;
$$ = d;
if ($2 != NULL && lGetConstantInt($2, &size, @2, "Array dimension")) {
if (size < 0) {
Error(@2, "Array dimension must be non-negative.");
$$ = NULL;
}
else {
Declarator *d = new Declarator(DK_ARRAY, Union(@1, @3));
d->arraySize = size;
$$ = d;
}
}
else
$$ = NULL;
@@ -1159,11 +1171,17 @@ direct_abstract_declarator
| direct_abstract_declarator '[' constant_expression ']'
{
int size;
if (lGetConstantInt($3, &size, @3, "Array dimension")) {
Declarator *d = new Declarator(DK_ARRAY, Union(@1, @4));
d->arraySize = size;
d->child = $1;
$$ = d;
if ($3 != NULL && lGetConstantInt($3, &size, @3, "Array dimension")) {
if (size < 0) {
Error(@3, "Array dimension must be non-negative.");
$$ = NULL;
}
else {
Declarator *d = new Declarator(DK_ARRAY, Union(@1, @4));
d->arraySize = size;
d->child = $1;
$$ = d;
}
}
else
$$ = NULL;
@@ -1674,6 +1692,10 @@ lGetConstantInt(Expr *expr, int *value, SourcePos pos, const char *usage) {
Error(pos, "%s must be a compile-time integer constant.", usage);
return false;
}
if ((int64_t)((int32_t)ci->getSExtValue()) != ci->getSExtValue()) {
Error(pos, "%s must be representable with a 32-bit integer.", usage);
return false;
}
*value = (int)ci->getZExtValue();
return true;
}

View File

@@ -0,0 +1,7 @@
// Array dimension must be representable with a 32-bit integer.
struct foo {
int x[0xffffffffffff];
};

View File

@@ -0,0 +1,7 @@
// Array dimension must be non-negative
struct foo {
int x[-1];
};