Skip to content

Commit 7fd7080

Browse files
committed
csa/MallocChecker: quote funtion names in error output
1 parent 44df42e commit 7fd7080

15 files changed

+102
-99
lines changed

clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,9 +1952,12 @@ static bool printMemFnName(raw_ostream &os, CheckerContext &C, const Expr *E) {
19521952
if (!FD)
19531953
return false;
19541954

1955-
os << *FD;
1955+
os << '\'' << *FD;
1956+
19561957
if (!FD->isOverloadedOperator())
19571958
os << "()";
1959+
1960+
os << '\'';
19581961
return true;
19591962
}
19601963

@@ -1988,7 +1991,7 @@ static void printExpectedAllocName(raw_ostream &os, AllocationFamily Family) {
19881991

19891992
switch (Family.Kind) {
19901993
case AF_Malloc:
1991-
os << "malloc()";
1994+
os << "'malloc()'";
19921995
return;
19931996
case AF_CXXNew:
19941997
os << "'new'";
@@ -2014,7 +2017,7 @@ static void printExpectedAllocName(raw_ostream &os, AllocationFamily Family) {
20142017
static void printExpectedDeallocName(raw_ostream &os, AllocationFamily Family) {
20152018
switch (Family.Kind) {
20162019
case AF_Malloc:
2017-
os << "free()";
2020+
os << "'free()'";
20182021
return;
20192022
case AF_CXXNew:
20202023
os << "'delete'";
@@ -2414,11 +2417,11 @@ void MallocChecker::HandleFreeAlloca(CheckerContext &C, SVal ArgVal,
24142417
if (ExplodedNode *N = C.generateErrorNode()) {
24152418
if (!BT_FreeAlloca[*CheckKind])
24162419
BT_FreeAlloca[*CheckKind].reset(new BugType(
2417-
CheckNames[*CheckKind], "Free alloca()", categories::MemoryError));
2420+
CheckNames[*CheckKind], "Free 'alloca()'", categories::MemoryError));
24182421

24192422
auto R = std::make_unique<PathSensitiveBugReport>(
24202423
*BT_FreeAlloca[*CheckKind],
2421-
"Memory allocated by alloca() should not be deallocated", N);
2424+
"Memory allocated by 'alloca()' should not be deallocated", N);
24222425
R->markInteresting(ArgVal.getAsRegion());
24232426
R->addRange(Range);
24242427
C.emitReport(std::move(R));

clang/test/Analysis/Inputs/expected-plists/plist-macros.cpp.plist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@
130130
</array>
131131
<key>depth</key><integer>0</integer>
132132
<key>extended_message</key>
133-
<string>Memory allocated by malloc() should be deallocated by free(), not &apos;delete&apos;</string>
133+
<string>Memory allocated by &apos;malloc()&apos; should be deallocated by &apos;free()&apos;, not &apos;delete&apos;</string>
134134
<key>message</key>
135-
<string>Memory allocated by malloc() should be deallocated by free(), not &apos;delete&apos;</string>
135+
<string>Memory allocated by &apos;malloc()&apos; should be deallocated by &apos;free()&apos;, not &apos;delete&apos;</string>
136136
</dict>
137137
</array>
138-
<key>description</key><string>Memory allocated by malloc() should be deallocated by free(), not &apos;delete&apos;</string>
138+
<key>description</key><string>Memory allocated by &apos;malloc()&apos; should be deallocated by &apos;free()&apos;, not &apos;delete&apos;</string>
139139
<key>category</key><string>Memory error</string>
140140
<key>type</key><string>Bad deallocator</string>
141141
<key>check_name</key><string>unix.MismatchedDeallocator</string>

clang/test/Analysis/Malloc+MismatchedDeallocator+NewDelete.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ void testMallocUseAfterFree() {
2424

2525
void testMallocBadFree() {
2626
int i;
27-
free(&i); // expected-warning{{Argument to free() is the address of the local variable 'i', which is not memory allocated by malloc()}}
27+
free(&i); // expected-warning{{Argument to 'free()' is the address of the local variable 'i', which is not memory allocated by 'malloc()'}}
2828
}
2929

3030
void testMallocOffsetFree() {
3131
int *p = (int *)malloc(sizeof(int));
32-
free(++p); // expected-warning{{Argument to free() is offset by 4 bytes from the start of memory allocated by malloc()}}
32+
free(++p); // expected-warning{{Argument to 'free()' is offset by 4 bytes from the start of memory allocated by 'malloc()'}}
3333
}
3434

3535
//-----------------------------------------------------------------
3636
// Check that unix.MismatchedDeallocator catches all types of bugs.
3737
//-----------------------------------------------------------------
3838
void testMismatchedDeallocator() {
3939
int *x = (int *)malloc(sizeof(int));
40-
delete x; // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not 'delete'}}
40+
delete x; // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'delete'}}
4141
}
4242

4343
//----------------------------------------------------------------
@@ -69,7 +69,7 @@ void testNewBadFree() {
6969

7070
void testNewOffsetFree() {
7171
int *p = new int;
72-
operator delete(++p); // expected-warning{{Argument to operator delete is offset by 4 bytes from the start of memory allocated by 'new'}}
72+
operator delete(++p); // expected-warning{{Argument to 'operator delete' is offset by 4 bytes from the start of memory allocated by 'new'}}
7373
}
7474

7575
//----------------------------------------------------------------
@@ -88,7 +88,7 @@ void testMismatchedChangePtrThroughCall() {
8888
void testMismatchedChangePointeeThroughCall() {
8989
int *p = (int*)malloc(sizeof(int)*4);
9090
changePointee(p);
91-
delete p; // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not 'delete'}}
91+
delete p; // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'delete'}}
9292
}
9393

9494
void testShouldReportDoubleFreeNotMismatched() {

clang/test/Analysis/MismatchedDeallocator-checker-test.mm

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,79 +21,79 @@
2121
//--------------- test malloc family
2222
void testMalloc1() {
2323
int *p = (int *)malloc(sizeof(int));
24-
delete p; // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not 'delete'}}
24+
delete p; // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'delete'}}
2525
}
2626

2727
void testMalloc2() {
2828
int *p = (int *)malloc(8);
2929
int *q = (int *)realloc(p, 16);
30-
delete q; // expected-warning{{Memory allocated by realloc() should be deallocated by free(), not 'delete'}}
30+
delete q; // expected-warning{{Memory allocated by 'realloc()' should be deallocated by 'free()', not 'delete'}}
3131
}
3232

3333
void testMalloc3() {
3434
int *p = (int *)calloc(1, sizeof(int));
35-
delete p; // expected-warning{{Memory allocated by calloc() should be deallocated by free(), not 'delete'}}
35+
delete p; // expected-warning{{Memory allocated by 'calloc()' should be deallocated by 'free()', not 'delete'}}
3636
}
3737

3838
void testMalloc4(const char *s) {
3939
char *p = strdup(s);
40-
delete p; // expected-warning{{Memory allocated by strdup() should be deallocated by free(), not 'delete'}}
40+
delete p; // expected-warning{{Memory allocated by 'strdup()' should be deallocated by 'free()', not 'delete'}}
4141
}
4242

4343
void testMalloc5() {
4444
int *p = (int *)my_malloc(sizeof(int));
45-
delete p; // expected-warning{{Memory allocated by my_malloc() should be deallocated by free(), not 'delete'}}
45+
delete p; // expected-warning{{Memory allocated by 'my_malloc()' should be deallocated by 'free()', not 'delete'}}
4646
}
4747

4848
void testMalloc6() {
4949
int *p = (int *)malloc(sizeof(int));
50-
operator delete(p); // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not operator delete}}
50+
operator delete(p); // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'operator delete'}}
5151
}
5252

5353
void testMalloc7() {
5454
int *p = (int *)malloc(sizeof(int));
55-
delete[] p; // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not 'delete[]'}}
55+
delete[] p; // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'delete[]'}}
5656
}
5757

5858
void testMalloc8() {
5959
int *p = (int *)malloc(sizeof(int));
60-
operator delete[](p); // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not operator delete[]}}
60+
operator delete[](p); // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'operator delete[]'}}
6161
}
6262

6363
void testAlloca() {
6464
int *p = (int *)__builtin_alloca(sizeof(int));
65-
delete p; // expected-warning{{Memory allocated by alloca() should not be deallocated}}
65+
delete p; // expected-warning{{Memory allocated by 'alloca()' should not be deallocated}}
6666
}
6767

6868
//--------------- test new family
6969
void testNew1() {
7070
int *p = new int;
71-
free(p); // expected-warning{{Memory allocated by 'new' should be deallocated by 'delete', not free()}}
71+
free(p); // expected-warning{{Memory allocated by 'new' should be deallocated by 'delete', not 'free()'}}
7272
}
7373

7474
void testNew2() {
7575
int *p = (int *)operator new(0);
76-
free(p); // expected-warning{{Memory allocated by operator new should be deallocated by 'delete', not free()}}
76+
free(p); // expected-warning{{Memory allocated by 'operator new' should be deallocated by 'delete', not 'free()'}}
7777
}
7878

7979
void testNew3() {
8080
int *p = new int[1];
81-
free(p); // expected-warning{{Memory allocated by 'new[]' should be deallocated by 'delete[]', not free()}}
81+
free(p); // expected-warning{{Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'free()'}}
8282
}
8383

8484
void testNew4() {
8585
int *p = new int;
86-
realloc(p, sizeof(long)); // expected-warning{{Memory allocated by 'new' should be deallocated by 'delete', not realloc()}}
86+
realloc(p, sizeof(long)); // expected-warning{{Memory allocated by 'new' should be deallocated by 'delete', not 'realloc()'}}
8787
}
8888

8989
void testNew5() {
9090
int *p = (int *)operator new(0);
91-
realloc(p, sizeof(long)); // expected-warning{{Memory allocated by operator new should be deallocated by 'delete', not realloc()}}
91+
realloc(p, sizeof(long)); // expected-warning{{Memory allocated by 'operator new' should be deallocated by 'delete', not 'realloc()'}}
9292
}
9393

9494
void testNew6() {
9595
int *p = new int[1];
96-
realloc(p, sizeof(long)); // expected-warning{{Memory allocated by 'new[]' should be deallocated by 'delete[]', not realloc()}}
96+
realloc(p, sizeof(long)); // expected-warning{{Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'realloc()'}}
9797
}
9898

9999
int *allocInt() {
@@ -106,7 +106,7 @@ void testNew7() {
106106

107107
void testNew8() {
108108
int *p = (int *)operator new(0);
109-
delete[] p; // expected-warning{{Memory allocated by operator new should be deallocated by 'delete', not 'delete[]'}}
109+
delete[] p; // expected-warning{{Memory allocated by 'operator new' should be deallocated by 'delete', not 'delete[]'}}
110110
}
111111

112112
int *allocIntArray(unsigned c) {
@@ -120,7 +120,7 @@ void testNew9() {
120120

121121
void testNew10() {
122122
int *p = (int *)operator new[](0);
123-
delete p; // expected-warning{{Memory allocated by operator new[] should be deallocated by 'delete[]', not 'delete'}}
123+
delete p; // expected-warning{{Memory allocated by 'operator new[]' should be deallocated by 'delete[]', not 'delete'}}
124124
}
125125

126126
void testNew11(NSUInteger dataLength) {
@@ -208,7 +208,7 @@ explicit SimpleSmartPointer(T *p = 0) : ptr(p) {}
208208
~SimpleSmartPointer() {
209209
delete ptr;
210210
// expected-warning@-1 {{Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'delete'}}
211-
// expected-warning@-2 {{Memory allocated by malloc() should be deallocated by free(), not 'delete'}}
211+
// expected-warning@-2 {{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'delete'}}
212212
}
213213
};
214214

clang/test/Analysis/NewDelete-intersections.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void testMallocFreeNoWarn() {
4444
void testDeleteMalloced() {
4545
int *p1 = (int *)malloc(sizeof(int));
4646
delete p1;
47-
// mismatch-warning@-1{{Memory allocated by malloc() should be deallocated by free(), not 'delete'}}
47+
// mismatch-warning@-1{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'delete'}}
4848

4949
int *p2 = (int *)__builtin_alloca(sizeof(int));
5050
delete p2; // no warn
@@ -59,13 +59,13 @@ void testUseZeroAllocatedMalloced() {
5959
void testFreeOpNew() {
6060
void *p = operator new(0);
6161
free(p);
62-
// mismatch-warning@-1{{Memory allocated by operator new should be deallocated by 'delete', not free()}}
62+
// mismatch-warning@-1{{Memory allocated by 'operator new' should be deallocated by 'delete', not 'free()'}}
6363
}
6464

6565
void testFreeNewExpr() {
6666
int *p = new int;
6767
free(p);
68-
// mismatch-warning@-1{{Memory allocated by 'new' should be deallocated by 'delete', not free()}}
68+
// mismatch-warning@-1{{Memory allocated by 'new' should be deallocated by 'delete', not 'free()'}}
6969
free(p);
7070
}
7171

clang/test/Analysis/free.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ void *alloca(size_t);
1313
void t1 (void) {
1414
int a[] = { 1 };
1515
free(a);
16-
// expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
16+
// expected-warning@-1{{Argument to 'free()' is the address of the local variable 'a', which is not memory allocated by 'malloc()'}}
1717
// expected-warning@-2{{attempt to call free on non-heap object 'a'}}
1818
}
1919

2020
void t2 (void) {
2121
int a = 1;
2222
free(&a);
23-
// expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
23+
// expected-warning@-1{{Argument to 'free()' is the address of the local variable 'a', which is not memory allocated by 'malloc()'}}
2424
// expected-warning@-2{{attempt to call free on non-heap object 'a'}}
2525
}
2626

2727
void t3 (void) {
2828
static int a[] = { 1 };
2929
free(a);
30-
// expected-warning@-1{{Argument to free() is the address of the static variable 'a', which is not memory allocated by malloc()}}
30+
// expected-warning@-1{{Argument to 'free()' is the address of the static variable 'a', which is not memory allocated by 'malloc()'}}
3131
// expected-warning@-2{{attempt to call free on non-heap object 'a'}}
3232
}
3333

@@ -42,7 +42,7 @@ void t5 (void) {
4242

4343
void t6 (void) {
4444
free((void*)1000);
45-
// expected-warning@-1{{Argument to free() is a constant address (1000), which is not memory allocated by malloc()}}
45+
// expected-warning@-1{{Argument to 'free()' is a constant address (1000), which is not memory allocated by 'malloc()'}}
4646
// expected-warning@-2{{attempt to call free on non-heap object '(void *)1000'}}
4747
}
4848

@@ -58,42 +58,42 @@ void t8 (char **x) {
5858
void t9 (void) {
5959
label:
6060
free(&&label);
61-
// expected-warning@-1{{Argument to free() is the address of the label 'label', which is not memory allocated by malloc()}}
61+
// expected-warning@-1{{Argument to 'free()' is the address of the label 'label', which is not memory allocated by 'malloc()'}}
6262
// expected-warning@-2{{attempt to call free on non-heap object 'label'}}
6363
}
6464

6565
void t10 (void) {
6666
free((void*)&t10);
67-
// expected-warning@-1{{Argument to free() is the address of the function 't10', which is not memory allocated by malloc()}}
67+
// expected-warning@-1{{Argument to 'free()' is the address of the function 't10', which is not memory allocated by 'malloc()'}}
6868
// expected-warning@-2{{attempt to call free on non-heap object 't10'}}
6969
}
7070

7171
void t11 (void) {
7272
char *p = (char*)alloca(2);
73-
free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
73+
free(p); // expected-warning {{Memory allocated by 'alloca()' should not be deallocated}}
7474
}
7575

7676
void t12 (void) {
7777
char *p = (char*)__builtin_alloca(2);
78-
free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
78+
free(p); // expected-warning {{Memory allocated by 'alloca()' should not be deallocated}}
7979
}
8080

8181
void t13 (void) {
8282
free(^{return;});
83-
// expected-warning@-1{{Argument to free() is a block, which is not memory allocated by malloc()}}
83+
// expected-warning@-1{{Argument to 'free()' is a block, which is not memory allocated by 'malloc()'}}
8484
// expected-warning@-2{{attempt to call free on non-heap object: block expression}}
8585
}
8686

8787
void t14 (char a) {
8888
free(&a);
89-
// expected-warning@-1{{Argument to free() is the address of the parameter 'a', which is not memory allocated by malloc()}}
89+
// expected-warning@-1{{Argument to 'free()' is the address of the parameter 'a', which is not memory allocated by 'malloc()'}}
9090
// expected-warning@-2{{attempt to call free on non-heap object 'a'}}
9191
}
9292

9393
static int someGlobal[2];
9494
void t15 (void) {
9595
free(someGlobal);
96-
// expected-warning@-1{{Argument to free() is the address of the global variable 'someGlobal', which is not memory allocated by malloc()}}
96+
// expected-warning@-1{{Argument to 'free()' is the address of the global variable 'someGlobal', which is not memory allocated by 'malloc()'}}
9797
// expected-warning@-2{{attempt to call free on non-heap object 'someGlobal'}}
9898
}
9999

@@ -105,7 +105,7 @@ void t16 (char **x, int offset) {
105105
int *iptr(void);
106106
void t17(void) {
107107
free(iptr); // Oops, forgot to call iptr().
108-
// expected-warning@-1{{Argument to free() is the address of the function 'iptr', which is not memory allocated by malloc()}}
108+
// expected-warning@-1{{Argument to 'free()' is the address of the function 'iptr', which is not memory allocated by 'malloc()'}}
109109
// expected-warning@-2{{attempt to call free on non-heap object 'iptr'}}
110110
}
111111

0 commit comments

Comments
 (0)