summaryrefslogtreecommitdiffstats
path: root/clang/test
diff options
context:
space:
mode:
Diffstat (limited to 'clang/test')
-rw-r--r--clang/test/Analysis/Malloc+MismatchedDeallocator_intersections.cpp13
-rw-r--r--clang/test/Analysis/NewDelete-checker-test.cpp24
-rw-r--r--clang/test/Analysis/NewDelete-intersections.mm5
-rw-r--r--clang/test/Analysis/malloc.c106
4 files changed, 147 insertions, 1 deletions
diff --git a/clang/test/Analysis/Malloc+MismatchedDeallocator_intersections.cpp b/clang/test/Analysis/Malloc+MismatchedDeallocator_intersections.cpp
index 639790d31a9..bca223b6e04 100644
--- a/clang/test/Analysis/Malloc+MismatchedDeallocator_intersections.cpp
+++ b/clang/test/Analysis/Malloc+MismatchedDeallocator_intersections.cpp
@@ -24,5 +24,16 @@ void testNewDeleteNoWarn() {
int *p4 = new int;
delete p4;
- int j = *p4; // no-warning
+ int j = *p4; // no-warning
+}
+
+void testUseZeroAllocNoWarn() {
+ int *p1 = (int *)operator new(0);
+ *p1 = 1; // no-warning
+
+ int *p2 = (int *)operator new[](0);
+ p2[0] = 1; // no-warning
+
+ int *p3 = new int[0];
+ p3[0] = 1; // no-warning
}
diff --git a/clang/test/Analysis/NewDelete-checker-test.cpp b/clang/test/Analysis/NewDelete-checker-test.cpp
index 3f28c2e8f2f..443cb2e87e9 100644
--- a/clang/test/Analysis/NewDelete-checker-test.cpp
+++ b/clang/test/Analysis/NewDelete-checker-test.cpp
@@ -87,6 +87,30 @@ void testNewInvalidationPlacement(PtrWrapper *w) {
new (w) PtrWrapper(new int); // no warn
}
+//-----------------------------------------
+// check for usage of zero-allocated memory
+//-----------------------------------------
+
+void testUseZeroAlloc1() {
+ int *p = (int *)operator new(0);
+ *p = 1; // expected-warning {{Use of zero-allocated memory}}
+ delete p;
+}
+
+int testUseZeroAlloc2() {
+ int *p = (int *)operator new[](0);
+ return p[0]; // expected-warning {{Use of zero-allocated memory}}
+ delete[] p;
+}
+
+void f(int);
+
+void testUseZeroAlloc3() {
+ int *p = new int[0];
+ f(*p); // expected-warning {{Use of zero-allocated memory}}
+ delete[] p;
+}
+
//---------------
// other checks
//---------------
diff --git a/clang/test/Analysis/NewDelete-intersections.mm b/clang/test/Analysis/NewDelete-intersections.mm
index 7b3fb6eae73..cde8122b727 100644
--- a/clang/test/Analysis/NewDelete-intersections.mm
+++ b/clang/test/Analysis/NewDelete-intersections.mm
@@ -43,6 +43,11 @@ void testDeleteMalloced() {
delete p2; // no warn
}
+void testUseZeroAllocatedMalloced() {
+ int *p1 = (int *)malloc(0);
+ *p1 = 1; // no warn
+}
+
//----- Test free standard new
void testFreeOpNew() {
void *p = operator new(0);
diff --git a/clang/test/Analysis/malloc.c b/clang/test/Analysis/malloc.c
index 5762061ba13..662df4c28b7 100644
--- a/clang/test/Analysis/malloc.c
+++ b/clang/test/Analysis/malloc.c
@@ -200,6 +200,112 @@ void reallocfPtrZero1() {
char *r = reallocf(0, 12);
} // expected-warning {{Potential leak of memory pointed to by}}
+//------------------- Check usage of zero-allocated memory ---------------------
+void CheckUseZeroAllocatedNoWarn1() {
+ int *p = malloc(0);
+ free(p); // no warning
+}
+
+void CheckUseZeroAllocatedNoWarn2() {
+ int *p = alloca(0); // no warning
+}
+
+void CheckUseZeroAllocatedNoWarn3() {
+ int *p = malloc(0);
+ int *q = realloc(p, 8); // no warning
+ free(q);
+}
+
+void CheckUseZeroAllocatedNoWarn4() {
+ int *p = realloc(0, 8);
+ *p = 1; // no warning
+ free(p);
+}
+
+void CheckUseZeroAllocated1() {
+ int *p = malloc(0);
+ *p = 1; // expected-warning {{Use of zero-allocated memory}}
+ free(p);
+}
+
+char CheckUseZeroAllocated2() {
+ char *p = alloca(0);
+ return *p; // expected-warning {{Use of zero-allocated memory}}
+}
+
+void UseZeroAllocated(int *p) {
+ if (p)
+ *p = 7; // expected-warning {{Use of zero-allocated memory}}
+}
+void CheckUseZeroAllocated3() {
+ int *p = malloc(0);
+ UseZeroAllocated(p);
+}
+
+void f(char);
+void CheckUseZeroAllocated4() {
+ char *p = valloc(0);
+ f(*p); // expected-warning {{Use of zero-allocated memory}}
+ free(p);
+}
+
+void CheckUseZeroAllocated5() {
+ int *p = calloc(0, 2);
+ *p = 1; // expected-warning {{Use of zero-allocated memory}}
+ free(p);
+}
+
+void CheckUseZeroAllocated6() {
+ int *p = calloc(2, 0);
+ *p = 1; // expected-warning {{Use of zero-allocated memory}}
+ free(p);
+}
+
+void CheckUseZeroAllocated7() {
+ int *p = realloc(0, 0);
+ *p = 1; //TODO: warn about use of zero-allocated memory
+ free(p);
+}
+
+void CheckUseZeroAllocated8() {
+ int *p = malloc(8);
+ int *q = realloc(p, 0);
+ *q = 1; //TODO: warn about use of zero-allocated memory
+ free(q);
+}
+
+void CheckUseZeroAllocated9() {
+ int *p = realloc(0, 0);
+ int *q = realloc(p, 0);
+ *q = 1; //TODO: warn about use of zero-allocated memory
+ free(q);
+}
+
+void CheckUseZeroAllocatedPathNoWarn(_Bool b) {
+ int s = 0;
+ if (b)
+ s= 10;
+
+ char *p = malloc(s);
+
+ if (b)
+ *p = 1; // no warning
+
+ free(p);
+}
+
+void CheckUseZeroAllocatedPathWarn(_Bool b) {
+ int s = 10;
+ if (b)
+ s= 0;
+
+ char *p = malloc(s);
+
+ if (b)
+ *p = 1; // expected-warning {{Use of zero-allocated memory}}
+
+ free(p);
+}
// This case tests that storing malloc'ed memory to a static variable which is
// then returned is not leaked. In the absence of known contracts for functions
OpenPOWER on IntegriCloud