Skip to content

Commit f6f45fc

Browse files
author
Daniel Kroening
committed
turn some assertions in the stdlib.h models into preconditions
1 parent 9ea0cc6 commit f6f45fc

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/ansi-c/library/stdlib.c

+18-16
Original file line numberDiff line numberDiff line change
@@ -162,24 +162,26 @@ inline void free(void *ptr)
162162
__CPROVER_precondition(ptr==0 || __CPROVER_POINTER_OFFSET(ptr)==0,
163163
"free argument has offset zero");
164164

165-
if(ptr!=0)
166-
{
167-
// catch double free
168-
if(__CPROVER_deallocated==ptr)
169-
__CPROVER_assert(0, "double free");
165+
// catch double free
166+
__CPROVER_precondition(ptr==0 || __CPROVER_deallocated!=ptr,
167+
"double free");
170168

171-
// catch people who try to use free(...) for stuff
172-
// allocated with new[]
173-
__CPROVER_assert(__CPROVER_malloc_object!=ptr ||
174-
!__CPROVER_malloc_is_new_array,
175-
"free called for new[] object");
169+
// catch people who try to use free(...) for stuff
170+
// allocated with new[]
171+
__CPROVER_precondition(ptr==0 ||
172+
__CPROVER_malloc_object!=ptr ||
173+
!__CPROVER_malloc_is_new_array,
174+
"free called for new[] object");
176175

176+
if(ptr!=0)
177+
{
177178
// non-deterministically record as deallocated
178179
__CPROVER_bool record=__VERIFIER_nondet___CPROVER_bool();
179180
if(record) __CPROVER_deallocated=ptr;
180181

181182
// detect memory leaks
182-
if(__CPROVER_memory_leak==ptr) __CPROVER_memory_leak=0;
183+
if(__CPROVER_memory_leak==ptr)
184+
__CPROVER_memory_leak=0;
183185
}
184186
}
185187

@@ -206,7 +208,7 @@ inline long strtol(const char *nptr, char **endptr, int base)
206208
{
207209
__CPROVER_HIDE:;
208210
#ifdef __CPROVER_STRING_ABSTRACTION
209-
__CPROVER_assert(__CPROVER_is_zero_string(nptr),
211+
__CPROVER_precondition(__CPROVER_is_zero_string(nptr),
210212
"zero-termination of argument of strtol");
211213
#endif
212214

@@ -329,7 +331,7 @@ inline char *getenv(const char *name)
329331

330332
(void)*name;
331333
#ifdef __CPROVER_STRING_ABSTRACTION
332-
__CPROVER_assert(__CPROVER_is_zero_string(name),
334+
__CPROVER_precondition(__CPROVER_is_zero_string(name),
333335
"zero-termination of argument of getenv");
334336
#endif
335337

@@ -367,6 +369,9 @@ inline void *realloc(void *ptr, __CPROVER_size_t malloc_size)
367369
{
368370
__CPROVER_HIDE:;
369371

372+
__CPROVER_precondition(ptr==0 || __CPROVER_DYNAMIC_OBJECT(ptr),
373+
"realloc argument is dynamic object");
374+
370375
// if ptr is NULL, this behaves like malloc
371376
if(ptr==0)
372377
return malloc(malloc_size);
@@ -379,9 +384,6 @@ inline void *realloc(void *ptr, __CPROVER_size_t malloc_size)
379384
return malloc(1);
380385
}
381386

382-
__CPROVER_assert(__CPROVER_DYNAMIC_OBJECT(ptr),
383-
"realloc argument is dynamic object");
384-
385387
// this shouldn't move if the new size isn't bigger
386388
void *res;
387389
res=malloc(malloc_size);

0 commit comments

Comments
 (0)