Skip to content

Commit

Permalink
Add test cases for invalid deref due to scoping
Browse files Browse the repository at this point in the history
  • Loading branch information
mrstanb committed Oct 4, 2023
1 parent 5cb10f6 commit ea4410d
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/regression/78-invalid-deref-scopes/01-scopes-no-static.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// PARAM: --set ana.activated[+] memOutOfBounds
// TODO: I haven't checked why, but we need memOutOfBounds for this case
extern int printf ( const char * format, ... );

int *foo2(void)
{
int arr[1024];
arr[194] = 13;
return arr + 1;
}

int *foo(void)
{
int arr[123];
return foo2();
}

int main(void) {
int *a = foo();
printf("%d\n", *a);//WARN
return 0;
}
29 changes: 29 additions & 0 deletions tests/regression/78-invalid-deref-scopes/02-scopes-global-var.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
int array[10];

// function returns array of numbers
int* getNumbers(void) {
for (int i = 0; i < 10; ++i) {
array[i] = i;//NOWARN
}

return array;
}

int* getNumbers2(void) {
int* numbers = getNumbers();
// numbers2 is local
int numbers2[10];

for (int i = 0; i < 10; ++i) {
numbers2[i] = numbers[i];//NOWARN
}

return numbers2;
}

int main(void) {
int *numbers = getNumbers2();
numbers[0] = 100;//WARN

return 0;
}
52 changes: 52 additions & 0 deletions tests/regression/78-invalid-deref-scopes/03-scopes-static.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
extern int printf (const char* format, ...);

// function returns array of numbers
int* getNumbers() {

static int array[10];

for (int i = 0; i < 10; ++i) {
array[i] = i;//NOWARN
}

return array;
}

int* getNumbers2() {
int* numbers = getNumbers();
static int numbers2[10];
for (int i = 0; i < 10; ++i) {
numbers2[i] = numbers[i];//NOWARN
}
return numbers2;
}

int* getNumbers3() {
int* numbers = getNumbers2();
int numbers3[10];
for (int i = 0; i < 10; ++i) {
numbers3[i] = numbers[i];//NOWARN
}

return numbers3;
}

int* getNumbers4() {
int* numbers = getNumbers3();
static int numbers4[10];
for (int i = 0; i < 10; ++i) {
numbers4[i] = numbers[i];//WARN
}
return numbers4;
}

int main (void) {

int *numbers = getNumbers4();

for (int i = 0; i < 10; i++ ) {
printf( "%d\n", *(numbers + i));//NOWARN
}

return 0;
}

0 comments on commit ea4410d

Please sign in to comment.