xref: /llvm-project/clang/test/Analysis/malloc-three-arg.c (revision 7d644e1215b376ec5e915df9ea2eeb56e2d94626)
1 // RUN: %clang_analyze_cc1 -triple x86_64-unknown-freebsd %s
2 
3 #include "Inputs/system-header-simulator.h"
4 
5 #define M_ZERO 0x0100
6 #define NULL ((void *)0)
7 
8 void *malloc(size_t, void *, int);
9 void free(void *);
10 
11 struct test {
12 };
13 
14 void foo(struct test *);
15 
test_zeroed(void)16 void test_zeroed(void) {
17   struct test **list, *t;
18   int i;
19 
20   list = malloc(sizeof(*list) * 10, NULL, M_ZERO);
21   if (list == NULL)
22     return;
23 
24   for (i = 0; i < 10; i++) {
25     t = list[i];
26     foo(t);
27   }
28   free(list); // no-warning
29 }
30 
test_nonzero(void)31 void test_nonzero(void) {
32   struct test **list, *t;
33   int i;
34 
35   list = malloc(sizeof(*list) * 10, NULL, 0);
36   if (list == NULL)
37     return;
38 
39   for (i = 0; i < 10; i++) {
40     t = list[i]; // expected-warning{{undefined}}
41     foo(t);
42   }
43   free(list);
44 }
45 
test_indeterminate(int flags)46 void test_indeterminate(int flags) {
47   struct test **list, *t;
48   int i;
49 
50   list = malloc(sizeof(*list) * 10, NULL, flags);
51   if (list == NULL)
52     return;
53 
54   for (i = 0; i < 10; i++) {
55     t = list[i]; // expected-warning{{undefined}}
56     foo(t);
57   }
58   free(list);
59 }
60