Merge pull request #320 from YashasSamaga/fix-i314

check dimensions of uninitialized array declarations
This commit is contained in:
Zeex 2018-08-05 13:03:54 +06:00 committed by GitHub
commit ae53783c0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 6 deletions

View File

@ -2490,17 +2490,21 @@ static void initials(int ident,int tag,cell *size,int dim[],int numdim,
cell tablesize;
int curlit=litidx;
int err=0;
int i;
if (!matchtoken('=')) {
assert(ident!=iARRAY || numdim>0);
if (ident==iARRAY && dim[numdim-1]==0) {
/* declared as "myvar[];" which is senseless (note: this *does* make
* sense in the case of a iREFARRAY, which is a function parameter)
*/
error(9); /* array has zero length -> invalid size */
} /* if */
if (ident==iARRAY) {
assert(numdim>0 && numdim<=sDIMEN_MAX);
for (i=0; i<numdim; i++) {
if (dim[i]==0) {
/* declared like "myvar[];" which is senseless (note: this *does* make
* sense in the case of a iREFARRAY, which is a function parameter)
*/
error(9); /* array has zero length -> invalid size */
return;
} /* if */
} /* for */
*size=calc_arraysize(dim,numdim,0);
if (*size==(cell)CELL_MAX) {
error(9); /* array is too big -> invalid size */

View File

@ -70,6 +70,16 @@ set_tests_properties(const_array_args_and_literals_gh_276 PROPERTIES PASS_REGULA
.*\\.pwn\\(41\\) : warning 239: literal array/string passed to a non-const parameter
")
add_compiler_test(md_array_size_chk_gh_314 ${CMAKE_CURRENT_SOURCE_DIR}/md_array_size_chk_gh_314.pwn)
set_tests_properties(md_array_size_chk_gh_314 PROPERTIES PASS_REGULAR_EXPRESSION
"*\\.pwn\\(1\\) : error 009: invalid array size \\(negative, zero or out of bounds\\)
.*\\.pwn\\(2\\) : error 009: invalid array size \\(negative, zero or out of bounds\\)
.*\\.pwn\\(3\\) : error 009: invalid array size \\(negative, zero or out of bounds\\)
.*\\.pwn\\(5\\) : error 009: invalid array size \\(negative, zero or out of bounds\\)
.*\\.pwn\\(30\\) : warning 224: indeterminate array size in \"sizeof\" expression \\(symbol \"\"\\)
")
set_tests_properties(md_array_size_chk_gh_314 PROPERTIES WILL_FAIL TRUE)
add_compiler_test(destructor_not_impl_gh_310 ${CMAKE_CURRENT_SOURCE_DIR}/destructor_not_impl_gh_310.pwn)
set_tests_properties(destructor_not_impl_gh_310 PROPERTIES PASS_REGULAR_EXPRESSION
".*\\.pwn\\(6\\) : error 004: function \"operator~(Error:)\" is not implemented

View File

@ -0,0 +1,40 @@
new arr1[] = {};
new arr2[5][];
new arr3[5][][5];
new arr4[5][5];
new arr5[][]= { { } };
f1(arr[]) {
#pragma unused arr
}
f2(arr[5][]) {
#pragma unused arr
}
f3(arr[5][][5]) {
#pragma unused arr
}
f4(arr[5][5]) {
#pragma unused arr
}
f5(arr[][]) {
#pragma unused arr
}
main () {
arr1[0] = 0;
arr2[0][0] = 0;
arr3[0][0][0] = 0;
arr4[0][0] = 0;
arr5[0][0] = 0;
new a = sizeof(arr1);
a = sizeof(arr1[]);
a = sizeof(arr5[][]);
#pragma unused a
f1(arr1);
f2(arr2);
f3(arr3);
f4(arr4);
f5(arr5);
}