Skip to content

Commit 42f7a79

Browse files
committed
modify use after free runtime testing to avoid compile-time detection
1 parent ea445a0 commit 42f7a79

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=================================================================
2+
3+
tests/run_time_errors/use_after_free.c:9 runtime error - malloc use after free
4+
5+
dcc explanation: access to memory that has already been freed.
6+
7+
Execution stopped in main() in tests/run_time_errors/use_after_free.c at line 9:
8+
9+
int main(int argc, char *argv[]) {
10+
int *p = (int *)malloc(sizeof(int *));
11+
*p = 1;
12+
if (argc > 0) {
13+
free(p);
14+
}
15+
--> return *p;
16+
}
17+
18+
Values when execution stopped:
19+
20+
argc = 1
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=================================================================
2+
3+
tests/run_time_errors/use_after_return.c:26 runtime error - stack use after return
4+
5+
dcc explanation: You have used a pointer to a local variable that no longer exists.
6+
When a function returns its local variables are destroyed.
7+
8+
For more information see: https://comp1511unsw.github.io/dcc/stack_use_after_return.html
9+
Execution stopped in main() in tests/run_time_errors/use_after_return.c at line 26:
10+
11+
int main(void) {
12+
--> printf("%d\n", *f(50));
13+
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include <stdlib.h>
22

3-
int main(void) {
4-
int *p = (int *)malloc(sizeof (int *));
3+
int main(int argc, char *argv[]) {
4+
int *p = (int *)malloc(sizeof(int *));
55
*p = 1;
6-
free(p);
6+
if (argc > 0) {
7+
free(p);
8+
}
79
return *p;
810
}

tests/run_time_errors/use_after_return.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ int * f(int num) {
1515
}
1616
factors[0] = count;
1717
int *factorPointer = factors;
18+
if (num == 0) {
19+
factorPointer = NULL;
20+
}
1821
return factorPointer;
1922

2023
}

0 commit comments

Comments
 (0)