Skip to content

Commit 793f472

Browse files
committed
[msan] Add test for deferencing zero-sized malloc/calloc
MSan fails to catch this, because 0-byte allocations are converted into 1-byte allocations. Bug originally reported by dvyukov
1 parent 6127e46 commit 793f472

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %clang_msan -Wno-alloc-size -fsanitize-recover=memory %s -o %t && not %run %t 2>&1 | FileCheck %s
2+
3+
// XFAIL: *
4+
5+
#include <malloc.h>
6+
#include <stdio.h>
7+
8+
int main(int argc, char **argv) {
9+
{
10+
char* p1 = (char*)calloc(1, 0);
11+
printf ("p1 is %p\n", p1);
12+
printf ("Content of p1 is: %d\n", *p1);
13+
// CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
14+
// CHECK: {{#0 0x.* in main .*zero_alloc.cpp:}}[[@LINE-2]]
15+
free(p1);
16+
}
17+
18+
{
19+
char* p2 = (char*)calloc(0, 1);
20+
printf ("p2 is %p\n", p2);
21+
printf ("Content of p2 is: %d\n", *p2);
22+
// CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
23+
// CHECK: {{#0 0x.* in main .*zero_alloc.cpp:}}[[@LINE-2]]
24+
free(p2);
25+
}
26+
27+
{
28+
char* p3 = (char*)malloc(0);
29+
printf ("p3 is %p\n", p3);
30+
printf ("Content of p2 is: %d\n", *p3);
31+
// CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
32+
// CHECK: {{#0 0x.* in main .*zero_alloc.cpp:}}[[@LINE-2]]
33+
free(p3);
34+
}
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)