blob: 30227a9cbc80628fe3d7d9b70af3fd5ff93643fb [file] [log] [blame]
Dominic Chen184c6242017-03-03 18:02:02 +00001// RUN: %clang_analyze_cc1 -analyzer-checker=unix.MallocSizeof -verify %s
Ted Kremenek0c28bc22014-10-19 07:30:55 +00002
3#include <stddef.h>
4
5void *malloc(size_t size);
6void *calloc(size_t nmemb, size_t size);
7void *realloc(void *ptr, size_t size);
8void free(void *ptr);
9
10struct A {};
11struct B {};
12
13void foo(unsigned int unsignedInt, unsigned int readSize) {
14 // Sanity check the checker is working as expected.
15 A* a = static_cast<A*>(malloc(sizeof(int))); // expected-warning {{Result of 'malloc' is converted to a pointer of type 'struct A', which is incompatible with sizeof operand type 'int'}}
16 free(a);
17}
18
19void bar() {
20 A *x = static_cast<A*>(calloc(10, sizeof(void*))); // expected-warning {{Result of 'calloc' is converted to a pointer of type 'struct A', which is incompatible with sizeof operand type 'void *'}}
21 // sizeof(void*) is compatible with any pointer.
22 A **y = static_cast<A**>(calloc(10, sizeof(void*))); // no-warning
23 free(x);
24 free(y);
25}
26