Skip to content

Commit 325447c

Browse files
committed
Add warning for array index out of range
1 parent fa76071 commit 325447c

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

Changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
Version 6.6.2
2+
- Added warnings for constant array indices out of bounds
3+
14
Version 6.6.1
25
- Changed type of `dat` array in C++ output to `unsigned char`
36
- Fixed function type usage in -2nu for some Spin method pointers

expr.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,20 @@ GetArrayBase(AST *arraytype)
22542254
return NULL;
22552255
}
22562256

2257+
AST *
2258+
GetArraySize(AST *arraytype)
2259+
{
2260+
AST *lim;
2261+
arraytype = RemoveTypeModifiers(arraytype);
2262+
if (arraytype->kind == AST_ARRAYTYPE) {
2263+
lim = arraytype->right;
2264+
if (lim) {
2265+
return lim;
2266+
}
2267+
}
2268+
return NULL;
2269+
}
2270+
22572271
/* find minimum alignment for a type */
22582272
int TypeAlign(AST *typ)
22592273
{

expr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ int IsArrayOrPointerSymbol(Symbol *);
8181
int PointerTypeIncrement(AST *typ);
8282
// get base of an array type
8383
AST *GetArrayBase(AST *type);
84+
85+
// get size of array type
86+
AST *GetArraySize(AST *type);
87+
8488
// create an array based on a type and expression or expression list
8589
// the elements of the list can either be simple expressions (giving the array size)
8690
// or AST_RANGE (giving the start and end indices)

frontends/types.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,23 @@ static AST *doCheckTypes(AST *ast)
19681968
} else if (IsArrayType(lefttype)) {
19691969
// convert the array index to subtract base
19701970
AST *base = GetArrayBase(lefttype);
1971+
if (IsConstExpr(ast->right)) {
1972+
int idx = EvalConstExpr(ast->right);
1973+
int low = 0;
1974+
if (!base || IsConstExpr(base)) {
1975+
low = base ? EvalConstExpr(base) : 0;
1976+
if (idx < low) {
1977+
WARNING(ast, "Array index %d is less than array start (%d)", idx, low);
1978+
}
1979+
}
1980+
AST *aSize = GetArraySize(lefttype);
1981+
if (IsConstExpr(aSize)) {
1982+
int high = EvalConstExpr(aSize) + low - 1;
1983+
if (idx > high) {
1984+
WARNING(ast, "Array index %d is above end of array (%d)", idx, high);
1985+
}
1986+
}
1987+
}
19711988
if (base) {
19721989
ast->right = AstOperator('-', ast->right, base);
19731990
}

version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#define VERSION_MAJOR 6
88
#define VERSION_MINOR 6
9-
#define VERSION_REV 1
10-
//#define BETA "-beta"
9+
#define VERSION_REV 2
10+
#define BETA "-beta"
1111

1212
#define VERSIONSTR version_string
1313

0 commit comments

Comments
 (0)