Skip to content

Commit 1c42123

Browse files
authored
Merge pull request #5512 from feliperodri/update-posix_memalign
Updates posix_memalign to consider malloc may fail
2 parents 0ec7ed4 + d2eea3b commit 1c42123

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
CORE
22
main.c
3-
--pointer-check --bounds-check
3+
--pointer-check --bounds-check --malloc-may-fail --malloc-fail-null
44
VERIFICATION SUCCESSFUL
55
^EXIT=0$
66
^SIGNAL=0$
77
--
8-
^\*\*\*\* WARNING: no body for function posix_memalign
8+
^warning: ignoring
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
4+
int main()
5+
{
6+
size_t size = 4;
7+
size_t page_size = 4096;
8+
void *src = "testing";
9+
void *dest;
10+
posix_memalign(&dest, page_size, size);
11+
memcpy(dest, src, size);
12+
return 0;
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--pointer-check --bounds-check --malloc-may-fail --malloc-fail-null
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
\[main.precondition_instance.1\] .* memcpy src/dst overlap: FAILURE
8+
\[main.precondition_instance.3\] .* memcpy destination region writeable: FAILURE
9+
\*\* 2 of 14 failed
10+
--
11+
^warning: ignoring

src/ansi-c/library/stdlib.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -527,20 +527,13 @@ __CPROVER_HIDE:;
527527
// As _mid_memalign simplifies for alignment <= MALLOC_ALIGNMENT
528528
// to a malloc call, it should be sound, if we do it too.
529529

530-
// The original posix_memalign check on the pointer is:
531-
532-
// void *tmp = malloc(size);
533-
// if(tmp != NULL){
534-
// *ptr = tmp;
535-
// return 0;
536-
// }
537-
// return ENOMEM;
538-
539-
// As _CPROVER_allocate used in malloc never returns null,
540-
// this check is not applicable and can be simplified:
541-
542-
*ptr = malloc(size);
543-
return 0;
530+
void *tmp = malloc(size);
531+
if(tmp != (void *)0)
532+
{
533+
*ptr = tmp;
534+
return 0;
535+
}
536+
return ENOMEM;
544537
}
545538

546539
/* FUNCTION: random */

0 commit comments

Comments
 (0)