1 // RUN: %clang_analyze_cc1 -analyzer-checker=unix.Malloc -analyzer-inline-max-stack-depth=5 -verify %s
3 #include "Inputs/system-header-simulator.h"
8 void *realloc(void *ptr
, size_t size
);
9 void *reallocf(void *ptr
, size_t size
);
10 void *calloc(size_t nmemb
, size_t size
);
12 void exit(int) __attribute__ ((__noreturn__
));
13 void *memcpy(void * restrict s1
, const void * restrict s2
, size_t n
);
14 size_t strlen(const char *);
16 static void my_malloc1(void **d
, size_t size
) {
20 static void *my_malloc2(int elevel
, size_t size
) {
28 static void my_free1(void *p
) {
32 static void test1(void) {
35 } // expected-warning {{Potential leak of memory pointed to by 'data'}}
37 static void test11(void) {
43 static void testUniqueingByallocationSiteInTopLevelFunction(void) {
44 void *data
= my_malloc2(1, 4);
46 int x
= 5;// expected-warning {{Potential leak of memory pointed to by 'data'}}
47 data
= my_malloc2(1, 4);
48 } // expected-warning {{Potential leak of memory pointed to by 'data'}}
50 static void test3(void) {
51 void *data
= my_malloc2(1, 4);
53 data
= my_malloc2(1, 4);
58 int *data
= (int*)my_malloc2(1, 4);
60 data
= (int *)my_malloc2(1, 4);
62 return *data
; // expected-warning {{Use of memory after it is freed}}
66 int *data
= (int *)my_malloc2(1, 4);
68 my_free1((int*)data
); // expected-warning{{Use of memory after it is freed}}
71 // TODO: We should warn here.
77 static char *reshape(char *in
) {
81 void testThatRemoveDeadBindingsRunBeforeEachCall(void) {
84 v
= reshape(v
);// expected-warning {{Potential leak of memory pointed to by 'v'}}
87 // Test that we keep processing after 'return;'
88 void fooWithEmptyReturn(int x
) {
95 int uafAndCallsFooWithEmptyReturn(void) {
96 int *x
= (int*)malloc(12);
98 fooWithEmptyReturn(12);
99 return *x
; // expected-warning {{Use of memory after it is freed}}
103 // If we inline any of the malloc-family functions, the checker shouldn't also
104 // try to do additional modeling.
105 char *strndup(const char *str
, size_t n
) {
109 // DO NOT FIX. This is to test that we are actually using the inlined
114 size_t length
= strlen(str
);
118 char *result
= malloc(n
+ 1);
119 memcpy(result
, str
, n
);
124 void useStrndup(size_t n
) {
126 (void)strndup(0, 20); // no-warning
129 (void)strndup("hi there", n
); // no-warning
132 (void)strndup("hi there", n
);
133 return; // expected-warning{{leak}}