Dominic Chen | feaf9ff | 2017-03-02 23:05:45 +0000 | [diff] [blame^] | 1 | // RUN: %clang_analyze_cc1 -triple x86_64-unknown-unknown -analyzer-checker=alpha.security.MallocOverflow,unix -verify %s |
Devin Coughlin | 683dfd3 | 2015-09-23 23:27:55 +0000 | [diff] [blame] | 2 | |
| 3 | typedef __typeof__(sizeof(int)) size_t; |
| 4 | extern void *malloc(size_t); |
| 5 | extern void free(void *ptr); |
| 6 | |
| 7 | void *malloc(unsigned long s); |
| 8 | |
| 9 | struct table { |
| 10 | int nentry; |
| 11 | unsigned *table; |
| 12 | unsigned offset_max; |
| 13 | }; |
| 14 | |
| 15 | static int table_build(struct table *t) { |
| 16 | |
| 17 | t->nentry = ((t->offset_max >> 2) + 31) / 32; |
| 18 | t->table = (unsigned *)malloc(sizeof(unsigned) * t->nentry); // expected-warning {{the computation of the size of the memory allocation may overflow}} |
| 19 | |
| 20 | int n; |
| 21 | n = 10000; |
| 22 | int *p = malloc(sizeof(int) * n); // no-warning |
| 23 | |
| 24 | free(p); |
| 25 | return t->nentry; |
| 26 | } |
| 27 | |
| 28 | static int table_build_1(struct table *t) { |
| 29 | t->nentry = (sizeof(struct table) * 2 + 31) / 32; |
| 30 | t->table = (unsigned *)malloc(sizeof(unsigned) * t->nentry); // no-warning |
| 31 | return t->nentry; |
| 32 | } |
| 33 | |
| 34 | void *f(int n) { |
| 35 | return malloc(n * 0 * sizeof(int)); // expected-warning {{Call to 'malloc' has an allocation size of 0 bytes}} |
| 36 | } |