Skip to content

Commit 134faae

Browse files
committed
[analyzer] CStringChecker: Improve warning messages.
Differential Revision: https://reviews.llvm.org/D71321
1 parent b361d3b commit 134faae

File tree

7 files changed

+55
-50
lines changed

7 files changed

+55
-50
lines changed

clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C,
290290
SmallString<80> buf;
291291
llvm::raw_svector_ostream OS(buf);
292292
assert(CurrentFunctionDescription);
293-
OS << "Null pointer argument in call to " << CurrentFunctionDescription
294-
<< ' ' << IdxOfArg << llvm::getOrdinalSuffix(IdxOfArg)
295-
<< " parameter";
293+
OS << "Null pointer passed as " << IdxOfArg
294+
<< llvm::getOrdinalSuffix(IdxOfArg) << " argument to "
295+
<< CurrentFunctionDescription;
296296

297297
emitNullArgBug(C, stateNull, S, OS.str());
298298
}
@@ -1536,7 +1536,10 @@ void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
15361536
bool ReturnEnd, bool IsBounded,
15371537
ConcatFnKind appendK,
15381538
bool returnPtr) const {
1539-
CurrentFunctionDescription = "string copy function";
1539+
if (appendK == ConcatFnKind::none)
1540+
CurrentFunctionDescription = "string copy function";
1541+
else
1542+
CurrentFunctionDescription = "string concatenation function";
15401543
ProgramStateRef state = C.getState();
15411544
const LocationContext *LCtx = C.getLocationContext();
15421545

clang/test/Analysis/bsd-string.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ void f3() {
3333
}
3434

3535
void f4() {
36-
strlcpy(NULL, "abcdef", 6); // expected-warning{{Null pointer argument in call to string copy function}}
36+
strlcpy(NULL, "abcdef", 6); // expected-warning{{Null pointer passed as 1st argument to string copy function}}
3737
}
3838

3939
void f5() {
40-
strlcat(NULL, "abcdef", 6); // expected-warning{{Null pointer argument in call to string copy function}}
40+
strlcat(NULL, "abcdef", 6); // expected-warning{{Null pointer passed as 1st argument to string concatenation function}}
4141
}
4242

4343
void f6() {

clang/test/Analysis/bstring.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ void memcpy9() {
148148

149149
void memcpy10() {
150150
char a[4] = {0};
151-
memcpy(0, a, 4); // expected-warning{{Null pointer argument in call to memory copy function}}
151+
memcpy(0, a, 4); // expected-warning{{Null pointer passed as 1st argument to memory copy function}}
152152
}
153153

154154
void memcpy11() {
155155
char a[4] = {0};
156-
memcpy(a, 0, 4); // expected-warning{{Null pointer argument in call to memory copy function}}
156+
memcpy(a, 0, 4); // expected-warning{{Null pointer passed as 2nd argument to memory copy function}}
157157
}
158158

159159
void memcpy12() {
@@ -173,7 +173,7 @@ void memcpy_unknown_size (size_t n) {
173173

174174
void memcpy_unknown_size_warn (size_t n) {
175175
char a[4];
176-
void *result = memcpy(a, 0, n); // expected-warning{{Null pointer argument in call to memory copy function}}
176+
void *result = memcpy(a, 0, n); // expected-warning{{Null pointer passed as 2nd argument to memory copy function}}
177177
clang_analyzer_eval(result == a); // no-warning (above is fatal)
178178
}
179179

@@ -268,12 +268,12 @@ void mempcpy9() {
268268

269269
void mempcpy10() {
270270
char a[4] = {0};
271-
mempcpy(0, a, 4); // expected-warning{{Null pointer argument in call to memory copy function}}
271+
mempcpy(0, a, 4); // expected-warning{{Null pointer passed as 1st argument to memory copy function}}
272272
}
273273

274274
void mempcpy11() {
275275
char a[4] = {0};
276-
mempcpy(a, 0, 4); // expected-warning{{Null pointer argument in call to memory copy function}}
276+
mempcpy(a, 0, 4); // expected-warning{{Null pointer passed as 2nd argument to memory copy function}}
277277
}
278278

279279
void mempcpy12() {
@@ -327,7 +327,7 @@ void mempcpy16() {
327327

328328
void mempcpy_unknown_size_warn (size_t n) {
329329
char a[4];
330-
void *result = mempcpy(a, 0, n); // expected-warning{{Null pointer argument in call to memory copy function}}
330+
void *result = mempcpy(a, 0, n); // expected-warning{{Null pointer passed as 2nd argument to memory copy function}}
331331
clang_analyzer_eval(result == a); // no-warning (above is fatal)
332332
}
333333

clang/test/Analysis/cstring-ranges.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
// This test verifies argument source range highlighting.
44
// Otherwise we've no idea which of the arguments is null.
5+
// These days we actually also have it in the message,
6+
// but the range is still great to have.
57

68
char *strcpy(char *, const char *);
79

@@ -10,6 +12,6 @@ void foo() {
1012
strcpy(a, b);
1113
}
1214

13-
// CHECK: warning: Null pointer argument in call to string copy function
15+
// CHECK: warning: Null pointer passed as 1st argument to string copy function
1416
// CHECK-NEXT: strcpy(a, b);
1517
// CHECK-NEXT: ^ ~

clang/test/Analysis/null-deref-path-notes.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,40 @@ void *memcpy(void *dest, const void *src, unsigned long count);
1313

1414
void f1(char *source) {
1515
char *destination = 0; // expected-note{{'destination' initialized to a null pointer value}}
16-
memcpy(destination + 0, source, 10); // expected-warning{{Null pointer argument in call to memory copy function}}
17-
// expected-note@-1{{Null pointer argument in call to memory copy function}}
16+
memcpy(destination + 0, source, 10); // expected-warning{{Null pointer passed as 1st argument to memory copy function}}
17+
// expected-note@-1{{Null pointer passed as 1st argument to memory copy function}}
1818
}
1919

2020
void f2(char *source) {
2121
char *destination = 0; // expected-note{{'destination' initialized to a null pointer value}}
22-
memcpy(destination - 0, source, 10); // expected-warning{{Null pointer argument in call to memory copy function}}
23-
// expected-note@-1{{Null pointer argument in call to memory copy function}}
22+
memcpy(destination - 0, source, 10); // expected-warning{{Null pointer passed as 1st argument to memory copy function}}
23+
// expected-note@-1{{Null pointer passed as 1st argument to memory copy function}}
2424
}
2525

2626
void f3(char *source) {
2727
char *destination = 0; // expected-note{{'destination' initialized to a null pointer value}}
2828
destination = destination + 0; // expected-note{{Null pointer value stored to 'destination'}}
29-
memcpy(destination, source, 10); // expected-warning{{Null pointer argument in call to memory copy function}}
30-
// expected-note@-1{{Null pointer argument in call to memory copy function}}
29+
memcpy(destination, source, 10); // expected-warning{{Null pointer passed as 1st argument to memory copy function}}
30+
// expected-note@-1{{Null pointer passed as 1st argument to memory copy function}}
3131
}
3232

3333
void f4(char *source) {
3434
char *destination = 0; // expected-note{{'destination' initialized to a null pointer value}}
3535
destination = destination - 0; // expected-note{{Null pointer value stored to 'destination'}}
36-
memcpy(destination, source, 10); // expected-warning{{Null pointer argument in call to memory copy function}}
37-
// expected-note@-1{{Null pointer argument in call to memory copy function}}
36+
memcpy(destination, source, 10); // expected-warning{{Null pointer passed as 1st argument to memory copy function}}
37+
// expected-note@-1{{Null pointer passed as 1st argument to memory copy function}}
3838
}
3939

4040
void f5(char *source) {
4141
char *destination1 = 0; // expected-note{{'destination1' initialized to a null pointer value}}
4242
char *destination2 = destination1 + 0; // expected-note{{'destination2' initialized to a null pointer value}}
43-
memcpy(destination2, source, 10); // expected-warning{{Null pointer argument in call to memory copy function}}
44-
// expected-note@-1{{Null pointer argument in call to memory copy function}}
43+
memcpy(destination2, source, 10); // expected-warning{{Null pointer passed as 1st argument to memory copy function}}
44+
// expected-note@-1{{Null pointer passed as 1st argument to memory copy function}}
4545
}
4646

4747
void f6(char *source) {
4848
char *destination1 = 0; // expected-note{{'destination1' initialized to a null pointer value}}
4949
char *destination2 = destination1 - 0; // expected-note{{'destination2' initialized to a null pointer value}}
50-
memcpy(destination2, source, 10); // expected-warning{{Null pointer argument in call to memory copy function}}
51-
// expected-note@-1{{Null pointer argument in call to memory copy function}}
50+
memcpy(destination2, source, 10); // expected-warning{{Null pointer passed as 1st argument to memory copy function}}
51+
// expected-note@-1{{Null pointer passed as 1st argument to memory copy function}}
5252
}

clang/test/Analysis/null-deref-ps-region.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void bar() {
3939

4040
void testConcreteNull() {
4141
int *x = 0;
42-
memset(x, 0, 1); // expected-warning {{Null pointer argument in call to memory set function}}
42+
memset(x, 0, 1); // expected-warning {{Null pointer passed as 1st argument to memory set function}}
4343
}
4444

4545
void testStackArray() {

clang/test/Analysis/string.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void strlen_constant2(char x) {
9797
}
9898

9999
size_t strlen_null() {
100-
return strlen(0); // expected-warning{{Null pointer argument in call to string length function}}
100+
return strlen(0); // expected-warning{{Null pointer passed as 1st argument to string length function}}
101101
}
102102

103103
size_t strlen_fn() {
@@ -251,7 +251,7 @@ void strnlen_constant6(char x) {
251251
}
252252

253253
size_t strnlen_null() {
254-
return strnlen(0, 3); // expected-warning{{Null pointer argument in call to string length function}}
254+
return strnlen(0, 3); // expected-warning{{Null pointer passed as 1st argument to string length function}}
255255
}
256256

257257
size_t strnlen_fn() {
@@ -322,11 +322,11 @@ char *strcpy(char *restrict s1, const char *restrict s2);
322322

323323

324324
void strcpy_null_dst(char *x) {
325-
strcpy(NULL, x); // expected-warning{{Null pointer argument in call to string copy function}}
325+
strcpy(NULL, x); // expected-warning{{Null pointer passed as 1st argument to string copy function}}
326326
}
327327

328328
void strcpy_null_src(char *x) {
329-
strcpy(x, NULL); // expected-warning{{Null pointer argument in call to string copy function}}
329+
strcpy(x, NULL); // expected-warning{{Null pointer passed as 2nd argument to string copy function}}
330330
}
331331

332332
void strcpy_fn(char *x) {
@@ -424,15 +424,15 @@ char *strcat(char *restrict s1, const char *restrict s2);
424424

425425

426426
void strcat_null_dst(char *x) {
427-
strcat(NULL, x); // expected-warning{{Null pointer argument in call to string copy function}}
427+
strcat(NULL, x); // expected-warning{{Null pointer passed as 1st argument to string concatenation function}}
428428
}
429429

430430
void strcat_null_src(char *x) {
431-
strcat(x, NULL); // expected-warning{{Null pointer argument in call to string copy function}}
431+
strcat(x, NULL); // expected-warning{{Null pointer passed as 2nd argument to string concatenation function}}
432432
}
433433

434434
void strcat_fn(char *x) {
435-
strcat(x, (char*)&strcat_fn); // expected-warning{{Argument to string copy function is the address of the function 'strcat_fn', which is not a null-terminated string}}
435+
strcat(x, (char*)&strcat_fn); // expected-warning{{Argument to string concatenation function is the address of the function 'strcat_fn', which is not a null-terminated string}}
436436
}
437437

438438
void strcat_effects(char *y) {
@@ -523,11 +523,11 @@ char *strncpy(char *restrict s1, const char *restrict s2, size_t n);
523523

524524

525525
void strncpy_null_dst(char *x) {
526-
strncpy(NULL, x, 5); // expected-warning{{Null pointer argument in call to string copy function}}
526+
strncpy(NULL, x, 5); // expected-warning{{Null pointer passed as 1st argument to string copy function}}
527527
}
528528

529529
void strncpy_null_src(char *x) {
530-
strncpy(x, NULL, 5); // expected-warning{{Null pointer argument in call to string copy function}}
530+
strncpy(x, NULL, 5); // expected-warning{{Null pointer passed as 2nd argument to string copy function}}
531531
}
532532

533533
void strncpy_fn(char *x) {
@@ -631,15 +631,15 @@ char *strncat(char *restrict s1, const char *restrict s2, size_t n);
631631

632632

633633
void strncat_null_dst(char *x) {
634-
strncat(NULL, x, 4); // expected-warning{{Null pointer argument in call to string copy function}}
634+
strncat(NULL, x, 4); // expected-warning{{Null pointer passed as 1st argument to string concatenation function}}
635635
}
636636

637637
void strncat_null_src(char *x) {
638-
strncat(x, NULL, 4); // expected-warning{{Null pointer argument in call to string copy function}}
638+
strncat(x, NULL, 4); // expected-warning{{Null pointer passed as 2nd argument to string concatenation function}}
639639
}
640640

641641
void strncat_fn(char *x) {
642-
strncat(x, (char*)&strncat_fn, 4); // expected-warning{{Argument to string copy function is the address of the function 'strncat_fn', which is not a null-terminated string}}
642+
strncat(x, (char*)&strncat_fn, 4); // expected-warning{{Argument to string concatenation function is the address of the function 'strncat_fn', which is not a null-terminated string}}
643643
}
644644

645645
void strncat_effects(char *y) {
@@ -812,13 +812,13 @@ void strcmp_2() {
812812
void strcmp_null_0() {
813813
char *x = NULL;
814814
char *y = "123";
815-
strcmp(x, y); // expected-warning{{Null pointer argument in call to string comparison function}}
815+
strcmp(x, y); // expected-warning{{Null pointer passed as 1st argument to string comparison function}}
816816
}
817817

818818
void strcmp_null_1() {
819819
char *x = "123";
820820
char *y = NULL;
821-
strcmp(x, y); // expected-warning{{Null pointer argument in call to string comparison function}}
821+
strcmp(x, y); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
822822
}
823823

824824
void strcmp_diff_length_0() {
@@ -921,13 +921,13 @@ void strncmp_2() {
921921
void strncmp_null_0() {
922922
char *x = NULL;
923923
char *y = "123";
924-
strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to string comparison function}}
924+
strncmp(x, y, 3); // expected-warning{{Null pointer passed as 1st argument to string comparison function}}
925925
}
926926

927927
void strncmp_null_1() {
928928
char *x = "123";
929929
char *y = NULL;
930-
strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to string comparison function}}
930+
strncmp(x, y, 3); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
931931
}
932932

933933
void strncmp_diff_length_0() {
@@ -1030,13 +1030,13 @@ void strcasecmp_2() {
10301030
void strcasecmp_null_0() {
10311031
char *x = NULL;
10321032
char *y = "123";
1033-
strcasecmp(x, y); // expected-warning{{Null pointer argument in call to string comparison function}}
1033+
strcasecmp(x, y); // expected-warning{{Null pointer passed as 1st argument to string comparison function}}
10341034
}
10351035

10361036
void strcasecmp_null_1() {
10371037
char *x = "123";
10381038
char *y = NULL;
1039-
strcasecmp(x, y); // expected-warning{{Null pointer argument in call to string comparison function}}
1039+
strcasecmp(x, y); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
10401040
}
10411041

10421042
void strcasecmp_diff_length_0() {
@@ -1121,13 +1121,13 @@ void strncasecmp_2() {
11211121
void strncasecmp_null_0() {
11221122
char *x = NULL;
11231123
char *y = "123";
1124-
strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to string comparison function}}
1124+
strncasecmp(x, y, 3); // expected-warning{{Null pointer passed as 1st argument to string comparison function}}
11251125
}
11261126

11271127
void strncasecmp_null_1() {
11281128
char *x = "123";
11291129
char *y = NULL;
1130-
strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to string comparison function}}
1130+
strncasecmp(x, y, 3); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
11311131
}
11321132

11331133
void strncasecmp_diff_length_0() {
@@ -1183,11 +1183,11 @@ void strncasecmp_embedded_null () {
11831183
char *strsep(char **stringp, const char *delim);
11841184

11851185
void strsep_null_delim(char *s) {
1186-
strsep(&s, NULL); // expected-warning{{Null pointer argument in call to strsep()}}
1186+
strsep(&s, NULL); // expected-warning{{Null pointer passed as 2nd argument to strsep()}}
11871187
}
11881188

11891189
void strsep_null_search() {
1190-
strsep(NULL, ""); // expected-warning{{Null pointer argument in call to strsep()}}
1190+
strsep(NULL, ""); // expected-warning{{Null pointer passed as 1st argument to strsep()}}
11911191
}
11921192

11931193
void strsep_return_original_pointer(char *s) {
@@ -1433,7 +1433,7 @@ void memset26_upper_UCHAR_MAX() {
14331433
void bzero1_null() {
14341434
char *a = NULL;
14351435

1436-
bzero(a, 10); // expected-warning{{Null pointer argument in call to memory clearance function}}
1436+
bzero(a, 10); // expected-warning{{Null pointer passed as 1st argument to memory clearance function}}
14371437
}
14381438

14391439
void bzero2_char_array_null() {
@@ -1453,7 +1453,7 @@ void bzero3_char_ptr_null() {
14531453
void explicit_bzero1_null() {
14541454
char *a = NULL;
14551455

1456-
explicit_bzero(a, 10); // expected-warning{{Null pointer argument in call to memory clearance function}}
1456+
explicit_bzero(a, 10); // expected-warning{{Null pointer passed as 1st argument to memory clearance function}}
14571457
}
14581458

14591459
void explicit_bzero2_clear_mypassword() {

0 commit comments

Comments
 (0)