blob: d2b3bcf3c84deee7293b767e70bcd3c6b027e136 [file] [log] [blame]
Peter Collingbournedc309672011-12-08 08:31:14 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=experimental.unix.MallocSizeof -verify %s
2
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);
8
9struct A {};
10struct B {};
11
12void foo() {
13 int *ip1 = malloc(sizeof(1));
14 int *ip2 = malloc(4 * sizeof(int));
15
16 long *lp1 = malloc(sizeof(short)); // expected-warning {{Result of 'malloc' is converted to type 'long *', whose pointee type 'long' is incompatible with sizeof operand type 'short'}}
17 long *lp2 = malloc(5 * sizeof(double)); // expected-warning {{Result of 'malloc' is converted to type 'long *', whose pointee type 'long' is incompatible with sizeof operand type 'double'}}
18 long *lp3 = malloc(5 * sizeof(char) + 2); // expected-warning {{Result of 'malloc' is converted to type 'long *', whose pointee type 'long' is incompatible with sizeof operand type 'char'}}
19
20 struct A *ap1 = calloc(1, sizeof(struct A));
21 struct A *ap2 = calloc(2, sizeof(*ap1));
22 struct A *ap3 = calloc(2, sizeof(ap1)); // expected-warning {{Result of 'calloc' is converted to type 'struct A *', whose pointee type 'struct A' is incompatible with sizeof operand type 'struct A *'}}
23 struct A *ap4 = calloc(3, sizeof(struct A*)); // expected-warning {{Result of 'calloc' is converted to type 'struct A *', whose pointee type 'struct A' is incompatible with sizeof operand type 'struct A *'}}
24 struct A *ap5 = calloc(4, sizeof(struct B)); // expected-warning {{Result of 'calloc' is converted to type 'struct A *', whose pointee type 'struct A' is incompatible with sizeof operand type 'struct B'}}
25 struct A *ap6 = realloc(ap5, sizeof(struct A));
26 struct A *ap7 = realloc(ap5, sizeof(struct B)); // expected-warning {{Result of 'realloc' is converted to type 'struct A *', whose pointee type 'struct A' is incompatible with sizeof operand type 'struct B'}}
27}