1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.security.MallocOverflow -verify %s
3 #define NULL ((void *) 0)
4 typedef __typeof__(sizeof(int)) size_t;
5 extern void * malloc(size_t);
9 return malloc(n
* sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
14 return malloc(sizeof(int) * n
); // // expected-warning {{the computation of the size of the memory allocation may overflow}}
19 return malloc(4 * sizeof(int)); // no-warning
27 void * f4(struct s4
*s
)
29 return malloc(s
->n
* sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
32 void * f5(struct s4
*s
)
35 return malloc(s2
.n
* sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
40 return malloc((n
+ 1) * sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
43 extern void * malloc (size_t);
49 return malloc(n
* sizeof(int)); // no-warning
55 return malloc(n
* sizeof(int)); // no-warning
62 int * x
= malloc(n
* sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
63 for (int i
= 0; i
< n
; i
++)
70 int * x
= malloc(n
* sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
79 int * x
= malloc(n
* sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
89 n
= (n
> 10 ? 10 : n
);
90 int * x
= malloc(n
* sizeof(int)); // no-warning
91 for (int i
= 0; i
< n
; i
++)
101 void * f13(struct s13
*s
)
105 return malloc(s
->n
* sizeof(int)); // no-warning
112 return malloc(n
* sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
115 void *check_before_malloc(int n
, int x
) {
120 p
= malloc(n
* sizeof(int)); // no-warning, the check precedes the allocation
122 // Do some other stuff, e.g. initialize the memory.
126 void *check_after_malloc(int n
, int x
) {
129 p
= malloc(n
* sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
131 // The check is after the allocation!
133 // Do something conditionally.
138 #define GREATER_THAN(lhs, rhs) (lhs > rhs)
139 void *check_after_malloc_using_macros(int n
, int x
) {
142 p
= malloc(n
* sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
144 if (GREATER_THAN(n
, 10))
147 // Do some other stuff, e.g. initialize the memory.