Skip to content

Commit 479ea2a

Browse files
committed
[analyzer] Check the checker name, rather than the ProgramPointTag when silencing a checker
The program point created by the checker, even if it is an error node, might not be the same as the name under which the report is emitted. Make sure we're checking the name of the checker, because thats what we're silencing after all. Differential Revision: https://reviews.llvm.org/D102683
1 parent ca23a38 commit 479ea2a

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

clang/lib/StaticAnalyzer/Core/BugReporter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,12 +1988,11 @@ PathDiagnosticBuilder::generate(const PathDiagnosticConsumer *PDC) const {
19881988

19891989
const SourceManager &SM = getSourceManager();
19901990
const AnalyzerOptions &Opts = getAnalyzerOptions();
1991-
StringRef ErrorTag = ErrorNode->getLocation().getTag()->getTagDescription();
19921991

19931992
// See whether we need to silence the checker/package.
19941993
// FIXME: This will not work if the report was emitted with an incorrect tag.
19951994
for (const std::string &CheckerOrPackage : Opts.SilencedCheckersAndPackages) {
1996-
if (ErrorTag.startswith(CheckerOrPackage))
1995+
if (R->getBugType().getCheckerName().startswith(CheckerOrPackage))
19971996
return nullptr;
19981997
}
19991998

clang/test/Analysis/malloc.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
1-
// RUN: %clang_analyze_cc1 -w -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -verify %s
2-
// RUN: %clang_analyze_cc1 -triple i386-unknown-linux-gnu -w -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -verify %s
3-
// RUN: %clang_analyze_cc1 -w -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -DTEST_INLINABLE_ALLOCATORS -verify %s
4-
// RUN: %clang_analyze_cc1 -triple i386-unknown-linux-gnu -w -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -DTEST_INLINABLE_ALLOCATORS -verify %s
1+
// RUN: %clang_analyze_cc1 -w -verify %s \
2+
// RUN: -analyzer-checker=core \
3+
// RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
4+
// RUN: -analyzer-checker=alpha.core.CastSize \
5+
// RUN: -analyzer-checker=unix.Malloc \
6+
// RUN: -analyzer-checker=cplusplus.NewDelete
7+
8+
// RUN: %clang_analyze_cc1 -w -verify %s \
9+
// RUN: -triple i386-unknown-linux-gnu \
10+
// RUN: -analyzer-checker=core \
11+
// RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
12+
// RUN: -analyzer-checker=alpha.core.CastSize \
13+
// RUN: -analyzer-checker=unix.Malloc \
14+
// RUN: -analyzer-checker=cplusplus.NewDelete
15+
16+
// RUN: %clang_analyze_cc1 -w -verify %s -DTEST_INLINABLE_ALLOCATORS \
17+
// RUN: -analyzer-checker=core \
18+
// RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
19+
// RUN: -analyzer-checker=alpha.core.CastSize \
20+
// RUN: -analyzer-checker=unix.Malloc \
21+
// RUN: -analyzer-checker=cplusplus.NewDelete
22+
23+
// RUN: %clang_analyze_cc1 -w -verify %s -DTEST_INLINABLE_ALLOCATORS \
24+
// RUN: -triple i386-unknown-linux-gnu \
25+
// RUN: -analyzer-checker=core \
26+
// RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
27+
// RUN: -analyzer-checker=alpha.core.CastSize \
28+
// RUN: -analyzer-checker=unix.Malloc \
29+
// RUN: -analyzer-checker=cplusplus.NewDelete
530

631
#include "Inputs/system-header-simulator-cxx.h"
732

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %clang_analyze_cc1 -verify="no-silence" %s \
2+
// RUN: -triple i386-unknown-linux-gnu \
3+
// RUN: -analyzer-checker=core,apiModeling \
4+
// RUN: -analyzer-checker=unix.Malloc \
5+
// RUN: -analyzer-checker=cplusplus.NewDelete
6+
7+
// RUN: %clang_analyze_cc1 -verify="unix-silenced" %s \
8+
// RUN: -triple i386-unknown-linux-gnu \
9+
// RUN: -analyzer-checker=core,apiModeling \
10+
// RUN: -analyzer-checker=unix.Malloc \
11+
// RUN: -analyzer-checker=cplusplus.NewDelete\
12+
// RUN: -analyzer-config silence-checkers="unix"
13+
14+
#include "Inputs/system-header-simulator-cxx.h"
15+
16+
typedef __typeof(sizeof(int)) size_t;
17+
void *malloc(size_t);
18+
void free(void *);
19+
void *realloc(void *ptr, size_t size);
20+
void *calloc(size_t nmemb, size_t size);
21+
char *strdup(const char *s);
22+
23+
void checkThatMallocCheckerIsRunning() {
24+
malloc(4);
25+
} // no-silence-warning{{Potential memory leak [unix.Malloc]}}
26+
27+
int const_ptr_and_callback_def_param_null(int, const char *, int n, void (*)(void *) = 0);
28+
void r11160612_no_callback() {
29+
char *x = (char *)malloc(12);
30+
const_ptr_and_callback_def_param_null(0, x, 12);
31+
} // no-silence-warning{{Potential leak of memory pointed to by 'x' [unix.Malloc]}}
32+
33+
#define ZERO_SIZE_PTR ((void *)16)
34+
35+
void test_delete_ZERO_SIZE_PTR() {
36+
int *Ptr = (int *)ZERO_SIZE_PTR;
37+
// ZERO_SIZE_PTR is specially handled but only for malloc family
38+
delete Ptr; // no-silence-warning{{Argument to 'delete' is a constant address (16), which is not memory allocated by 'new' [cplusplus.NewDelete]}}
39+
// unix-silenced-warning@-1{{Argument to 'delete' is a constant address (16), which is not memory allocated by 'new' [cplusplus.NewDelete]}}
40+
}

0 commit comments

Comments
 (0)