Skip to content

Commit a65c197

Browse files
author
Daniel Kroening
authored
Merge pull request #4772 from tautschnig/free-deref
C library/free: distinguish invalid pointer from invalid free
2 parents 4cf13df + 98a3ea9 commit a65c197

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
#include <assert.h>
21
#include <stdlib.h>
32

43
int main()
54
{
6-
free();
7-
assert(0);
5+
int *ptr;
6+
free(ptr);
87
return 0;
98
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
KNOWNBUG
1+
CORE
22
main.c
3-
--pointer-check --bounds-check
4-
^EXIT=0$
3+
--pointer-check --bounds-check --stop-on-fail
4+
free argument must be NULL or valid pointer
5+
^EXIT=10$
56
^SIGNAL=0$
6-
^VERIFICATION SUCCESSFUL$
7+
^VERIFICATION FAILED$
78
--
89
^warning: ignoring

regression/cbmc/alloca1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ main.c
55
^SIGNAL=0$
66
^VERIFICATION FAILED$
77
free called for stack-allocated object: FAILURE$
8-
^\*\* 1 of 12 failed
8+
^\*\* 1 of 13 failed
99
--
1010
^warning: ignoring

src/ansi-c/library/stdlib.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ inline void free(void *ptr)
184184
{
185185
__CPROVER_HIDE:;
186186
// If ptr is NULL, no operation is performed.
187+
__CPROVER_precondition(
188+
ptr == 0 || __CPROVER_r_ok(ptr, 0),
189+
"free argument must be NULL or valid pointer");
187190
__CPROVER_precondition(ptr==0 || __CPROVER_DYNAMIC_OBJECT(ptr),
188191
"free argument must be dynamic object");
189192
__CPROVER_precondition(ptr==0 || __CPROVER_POINTER_OFFSET(ptr)==0,

0 commit comments

Comments
 (0)