Skip to content

Commit 2b08c66

Browse files
petr-bauchdanpoe
authored andcommitted
Calloc may fail
using the same code as for malloc.
1 parent bdd8b8a commit 2b08c66

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/ansi-c/library/stdlib.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ __CPROVER_HIDE:;
7575
__CPROVER_size_t alloc_size = nmemb * size;
7676
#pragma CPROVER check pop
7777

78+
if(__CPROVER_malloc_failure_mode == __CPROVER_malloc_failure_mode_return_null)
79+
{
80+
__CPROVER_bool should_malloc_fail = __VERIFIER_nondet___CPROVER_bool();
81+
if(
82+
alloc_size > __CPROVER_max_malloc_size ||
83+
(__CPROVER_malloc_may_fail && should_malloc_fail))
84+
{
85+
return (void *)0;
86+
}
87+
}
88+
else if(
89+
__CPROVER_malloc_failure_mode ==
90+
__CPROVER_malloc_failure_mode_assert_then_assume)
91+
{
92+
__CPROVER_assert(
93+
alloc_size <= __CPROVER_max_malloc_size, "max allocation size exceeded");
94+
__CPROVER_assume(alloc_size <= __CPROVER_max_malloc_size);
95+
96+
__CPROVER_bool should_malloc_fail = __VERIFIER_nondet___CPROVER_bool();
97+
__CPROVER_assert(
98+
!__CPROVER_malloc_may_fail || !should_malloc_fail,
99+
"max allocation may fail");
100+
__CPROVER_assume(!__CPROVER_malloc_may_fail || !should_malloc_fail);
101+
}
102+
78103
void *malloc_res;
79104
// realistically, calloc may return NULL,
80105
// and __CPROVER_allocate doesn't, but no one cares

0 commit comments

Comments
 (0)