Skip to content

Fix double-free bug in stdlib models with --malloc-fail-null #5518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdlib.h>

int main(void)
{
void *ptr = malloc(sizeof(char));
if(ptr == NULL)
return 0;

void *ptr1 = realloc(ptr, 2 * sizeof(char));
if(ptr1 == NULL)
{
free(ptr);
return 0;
}

free(ptr1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
test.c
--malloc-may-fail --malloc-fail-null
^\[main.precondition_instance.\d+] line \d+ double free: SUCCESS$
^VERIFICATION SUCCESSFUL$
^EXIT=0$
^SIGNAL=0$
--
--
This is a test checking for a regression where the --malloc-may-fail flag
introduced a double-free bug in the stdlib models with realloc.
4 changes: 3 additions & 1 deletion src/ansi-c/library/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,10 @@ inline void *realloc(void *ptr, __CPROVER_size_t malloc_size)
void *res;
res=malloc(malloc_size);
if(res != (void *)0)
{
__CPROVER_array_copy(res, ptr);
free(ptr);
free(ptr);
}

return res;
}
Expand Down