-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test cases for invalid deref due to scoping
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
tests/regression/78-invalid-deref-scopes/01-scopes-no-static.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
tests/regression/78-invalid-deref-scopes/02-scopes-global-var.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
tests/regression/78-invalid-deref-scopes/03-scopes-static.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |