aboutsummaryrefslogtreecommitdiff
path: root/test/Analysis/malloc-sizeof.c
diff options
context:
space:
mode:
authorPeter Collingbourne <peter@pcc.me.uk>2011-12-08 08:31:14 +0000
committerPeter Collingbourne <peter@pcc.me.uk>2011-12-08 08:31:14 +0000
commitdc30967a4633186782e0e204c65dba2552301ec9 (patch)
tree3cf88e20ef0271a4fb26a841b06b126619706f31 /test/Analysis/malloc-sizeof.c
parent2e55df49929a515b05f3af89b47a13357eccd9d0 (diff)
Add an experimental MallocSizeofChecker, which reports inconsistencies
between the casted type of the return value of a malloc/calloc/realloc call and the operand of any sizeof expressions contained within its argument(s). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@146144 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/Analysis/malloc-sizeof.c')
-rw-r--r--test/Analysis/malloc-sizeof.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/test/Analysis/malloc-sizeof.c b/test/Analysis/malloc-sizeof.c
new file mode 100644
index 0000000000..d2b3bcf3c8
--- /dev/null
+++ b/test/Analysis/malloc-sizeof.c
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -analyze -analyzer-checker=experimental.unix.MallocSizeof -verify %s
+
+#include <stddef.h>
+
+void *malloc(size_t size);
+void *calloc(size_t nmemb, size_t size);
+void *realloc(void *ptr, size_t size);
+
+struct A {};
+struct B {};
+
+void foo() {
+ int *ip1 = malloc(sizeof(1));
+ int *ip2 = malloc(4 * sizeof(int));
+
+ 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'}}
+ 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'}}
+ 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'}}
+
+ struct A *ap1 = calloc(1, sizeof(struct A));
+ struct A *ap2 = calloc(2, sizeof(*ap1));
+ 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 *'}}
+ 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 *'}}
+ 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'}}
+ struct A *ap6 = realloc(ap5, sizeof(struct A));
+ 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'}}
+}