Issue errors if array dimensions are negative or too large to fit in 32 bits.
This commit is contained in:
48
parse.yy
48
parse.yy
@@ -947,10 +947,16 @@ direct_declarator
|
|||||||
{
|
{
|
||||||
int size;
|
int size;
|
||||||
if ($1 != NULL && lGetConstantInt($3, &size, @3, "Array dimension")) {
|
if ($1 != NULL && lGetConstantInt($3, &size, @3, "Array dimension")) {
|
||||||
Declarator *d = new Declarator(DK_ARRAY, Union(@1, @4));
|
if (size < 0) {
|
||||||
d->arraySize = size;
|
Error(@3, "Array dimension must be non-negative.");
|
||||||
d->child = $1;
|
$$ = NULL;
|
||||||
$$ = d;
|
}
|
||||||
|
else {
|
||||||
|
Declarator *d = new Declarator(DK_ARRAY, Union(@1, @4));
|
||||||
|
d->arraySize = size;
|
||||||
|
d->child = $1;
|
||||||
|
$$ = d;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
$$ = NULL;
|
$$ = NULL;
|
||||||
@@ -1141,10 +1147,16 @@ direct_abstract_declarator
|
|||||||
| '[' constant_expression ']'
|
| '[' constant_expression ']'
|
||||||
{
|
{
|
||||||
int size;
|
int size;
|
||||||
if (lGetConstantInt($2, &size, @2, "Array dimension")) {
|
if ($2 != NULL && lGetConstantInt($2, &size, @2, "Array dimension")) {
|
||||||
Declarator *d = new Declarator(DK_ARRAY, Union(@1, @3));
|
if (size < 0) {
|
||||||
d->arraySize = size;
|
Error(@2, "Array dimension must be non-negative.");
|
||||||
$$ = d;
|
$$ = NULL;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Declarator *d = new Declarator(DK_ARRAY, Union(@1, @3));
|
||||||
|
d->arraySize = size;
|
||||||
|
$$ = d;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
$$ = NULL;
|
$$ = NULL;
|
||||||
@@ -1159,11 +1171,17 @@ direct_abstract_declarator
|
|||||||
| direct_abstract_declarator '[' constant_expression ']'
|
| direct_abstract_declarator '[' constant_expression ']'
|
||||||
{
|
{
|
||||||
int size;
|
int size;
|
||||||
if (lGetConstantInt($3, &size, @3, "Array dimension")) {
|
if ($3 != NULL && lGetConstantInt($3, &size, @3, "Array dimension")) {
|
||||||
Declarator *d = new Declarator(DK_ARRAY, Union(@1, @4));
|
if (size < 0) {
|
||||||
d->arraySize = size;
|
Error(@3, "Array dimension must be non-negative.");
|
||||||
d->child = $1;
|
$$ = NULL;
|
||||||
$$ = d;
|
}
|
||||||
|
else {
|
||||||
|
Declarator *d = new Declarator(DK_ARRAY, Union(@1, @4));
|
||||||
|
d->arraySize = size;
|
||||||
|
d->child = $1;
|
||||||
|
$$ = d;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
$$ = NULL;
|
$$ = 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);
|
Error(pos, "%s must be a compile-time integer constant.", usage);
|
||||||
return false;
|
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();
|
*value = (int)ci->getZExtValue();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
7
tests_errors/array-dim-huge.ispc
Normal file
7
tests_errors/array-dim-huge.ispc
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
// Array dimension must be representable with a 32-bit integer.
|
||||||
|
|
||||||
|
struct foo {
|
||||||
|
int x[0xffffffffffff];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
7
tests_errors/array-dim-negative.ispc
Normal file
7
tests_errors/array-dim-negative.ispc
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
// Array dimension must be non-negative
|
||||||
|
|
||||||
|
struct foo {
|
||||||
|
int x[-1];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user