xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/malloc-sizeof.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=unix.MallocSizeof -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc #include <stddef.h>
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc void *malloc(size_t size);
6*f4a2713aSLionel Sambuc void *calloc(size_t nmemb, size_t size);
7*f4a2713aSLionel Sambuc void *realloc(void *ptr, size_t size);
8*f4a2713aSLionel Sambuc void free(void *ptr);
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc struct A {};
11*f4a2713aSLionel Sambuc struct B {};
12*f4a2713aSLionel Sambuc 
foo(unsigned int unsignedInt,unsigned int readSize)13*f4a2713aSLionel Sambuc void foo(unsigned int unsignedInt, unsigned int readSize) {
14*f4a2713aSLionel Sambuc   int *ip1 = malloc(sizeof(1));
15*f4a2713aSLionel Sambuc   int *ip2 = malloc(4 * sizeof(int));
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc   long *lp1 = malloc(sizeof(short)); // expected-warning {{Result of 'malloc' is converted to a pointer of type 'long', which is incompatible with sizeof operand type 'short'}}
18*f4a2713aSLionel Sambuc   long *lp2 = malloc(5 * sizeof(double)); // expected-warning {{Result of 'malloc' is converted to a pointer of type 'long', which is incompatible with sizeof operand type 'double'}}
19*f4a2713aSLionel Sambuc   char *cp3 = malloc(5 * sizeof(char) + 2); // no warning
20*f4a2713aSLionel Sambuc   unsigned char *buf = malloc(readSize + sizeof(unsignedInt)); // no warning
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc   struct A *ap1 = calloc(1, sizeof(struct A));
23*f4a2713aSLionel Sambuc   struct A *ap2 = calloc(2, sizeof(*ap1));
24*f4a2713aSLionel Sambuc   struct A *ap3 = calloc(2, sizeof(ap1)); // expected-warning {{Result of 'calloc' is converted to a pointer of type 'struct A', which is incompatible with sizeof operand type 'struct A *'}}
25*f4a2713aSLionel Sambuc   struct A *ap4 = calloc(3, sizeof(struct A*)); // expected-warning {{Result of 'calloc' is converted to a pointer of type 'struct A', which is incompatible with sizeof operand type 'struct A *'}}
26*f4a2713aSLionel Sambuc   struct A *ap5 = calloc(4, sizeof(struct B)); // expected-warning {{Result of 'calloc' is converted to a pointer of type 'struct A', which is incompatible with sizeof operand type 'struct B'}}
27*f4a2713aSLionel Sambuc   struct A *ap6 = realloc(ap5, sizeof(struct A));
28*f4a2713aSLionel Sambuc   struct A *ap7 = realloc(ap5, sizeof(struct B)); // expected-warning {{Result of 'realloc' is converted to a pointer of type 'struct A', which is incompatible with sizeof operand type 'struct B'}}
29*f4a2713aSLionel Sambuc }
30*f4a2713aSLionel Sambuc 
31*f4a2713aSLionel Sambuc // Don't warn when the types differ only by constness.
ignore_const()32*f4a2713aSLionel Sambuc void ignore_const() {
33*f4a2713aSLionel Sambuc   const char **x = (const char **)malloc(1 * sizeof(char *)); // no-warning
34*f4a2713aSLionel Sambuc   const char ***y = (const char ***)malloc(1 * sizeof(char *)); // expected-warning {{Result of 'malloc' is converted to a pointer of type 'const char **', which is incompatible with sizeof operand type 'char *'}}
35*f4a2713aSLionel Sambuc   free(x);
36*f4a2713aSLionel Sambuc }
37*f4a2713aSLionel Sambuc 
mallocArraySize()38*f4a2713aSLionel Sambuc int *mallocArraySize() {
39*f4a2713aSLionel Sambuc   static const int sTable[10];
40*f4a2713aSLionel Sambuc   static const int nestedTable[10][2];
41*f4a2713aSLionel Sambuc   int *table = malloc(sizeof sTable);
42*f4a2713aSLionel Sambuc   int *table1 = malloc(sizeof nestedTable);
43*f4a2713aSLionel Sambuc   int (*table2)[2] = malloc(sizeof nestedTable);
44*f4a2713aSLionel Sambuc   int (*table3)[10][2] = malloc(sizeof nestedTable);
45*f4a2713aSLionel Sambuc   return table;
46*f4a2713aSLionel Sambuc }
47*f4a2713aSLionel Sambuc 
mallocWrongArraySize()48*f4a2713aSLionel Sambuc int *mallocWrongArraySize() {
49*f4a2713aSLionel Sambuc   static const double sTable[10];
50*f4a2713aSLionel Sambuc   int *table = malloc(sizeof sTable); // expected-warning {{Result of 'malloc' is converted to a pointer of type 'int', which is incompatible with sizeof operand type 'const double [10]'}}
51*f4a2713aSLionel Sambuc   return table;
52*f4a2713aSLionel Sambuc }
53