From 479ea2a8ed95544d2f5aaede34bfe5c298ae8bdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20Umann?= Date: Tue, 18 May 2021 13:06:02 +0200 Subject: [PATCH] [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 --- clang/lib/StaticAnalyzer/Core/BugReporter.cpp | 3 +- clang/test/Analysis/malloc.cpp | 33 +++++++++++++-- .../test/Analysis/silence-checkers-malloc.cpp | 40 +++++++++++++++++++ 3 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 clang/test/Analysis/silence-checkers-malloc.cpp diff --git a/clang/lib/StaticAnalyzer/Core/BugReporter.cpp b/clang/lib/StaticAnalyzer/Core/BugReporter.cpp index b64c0798d7e2..4608ee5cd40b 100644 --- a/clang/lib/StaticAnalyzer/Core/BugReporter.cpp +++ b/clang/lib/StaticAnalyzer/Core/BugReporter.cpp @@ -1988,12 +1988,11 @@ PathDiagnosticBuilder::generate(const PathDiagnosticConsumer *PDC) const { const SourceManager &SM = getSourceManager(); const AnalyzerOptions &Opts = getAnalyzerOptions(); - StringRef ErrorTag = ErrorNode->getLocation().getTag()->getTagDescription(); // See whether we need to silence the checker/package. // FIXME: This will not work if the report was emitted with an incorrect tag. for (const std::string &CheckerOrPackage : Opts.SilencedCheckersAndPackages) { - if (ErrorTag.startswith(CheckerOrPackage)) + if (R->getBugType().getCheckerName().startswith(CheckerOrPackage)) return nullptr; } diff --git a/clang/test/Analysis/malloc.cpp b/clang/test/Analysis/malloc.cpp index 21e8e79e1a89..2bbf26ac2cda 100644 --- a/clang/test/Analysis/malloc.cpp +++ b/clang/test/Analysis/malloc.cpp @@ -1,7 +1,32 @@ -// RUN: %clang_analyze_cc1 -w -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -verify %s -// 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 -// 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 -// 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 +// RUN: %clang_analyze_cc1 -w -verify %s \ +// RUN: -analyzer-checker=core \ +// RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \ +// RUN: -analyzer-checker=alpha.core.CastSize \ +// RUN: -analyzer-checker=unix.Malloc \ +// RUN: -analyzer-checker=cplusplus.NewDelete + +// RUN: %clang_analyze_cc1 -w -verify %s \ +// RUN: -triple i386-unknown-linux-gnu \ +// RUN: -analyzer-checker=core \ +// RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \ +// RUN: -analyzer-checker=alpha.core.CastSize \ +// RUN: -analyzer-checker=unix.Malloc \ +// RUN: -analyzer-checker=cplusplus.NewDelete + +// RUN: %clang_analyze_cc1 -w -verify %s -DTEST_INLINABLE_ALLOCATORS \ +// RUN: -analyzer-checker=core \ +// RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \ +// RUN: -analyzer-checker=alpha.core.CastSize \ +// RUN: -analyzer-checker=unix.Malloc \ +// RUN: -analyzer-checker=cplusplus.NewDelete + +// RUN: %clang_analyze_cc1 -w -verify %s -DTEST_INLINABLE_ALLOCATORS \ +// RUN: -triple i386-unknown-linux-gnu \ +// RUN: -analyzer-checker=core \ +// RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \ +// RUN: -analyzer-checker=alpha.core.CastSize \ +// RUN: -analyzer-checker=unix.Malloc \ +// RUN: -analyzer-checker=cplusplus.NewDelete #include "Inputs/system-header-simulator-cxx.h" diff --git a/clang/test/Analysis/silence-checkers-malloc.cpp b/clang/test/Analysis/silence-checkers-malloc.cpp new file mode 100644 index 000000000000..2f6a9dd2d5b8 --- /dev/null +++ b/clang/test/Analysis/silence-checkers-malloc.cpp @@ -0,0 +1,40 @@ +// RUN: %clang_analyze_cc1 -verify="no-silence" %s \ +// RUN: -triple i386-unknown-linux-gnu \ +// RUN: -analyzer-checker=core,apiModeling \ +// RUN: -analyzer-checker=unix.Malloc \ +// RUN: -analyzer-checker=cplusplus.NewDelete + +// RUN: %clang_analyze_cc1 -verify="unix-silenced" %s \ +// RUN: -triple i386-unknown-linux-gnu \ +// RUN: -analyzer-checker=core,apiModeling \ +// RUN: -analyzer-checker=unix.Malloc \ +// RUN: -analyzer-checker=cplusplus.NewDelete\ +// RUN: -analyzer-config silence-checkers="unix" + +#include "Inputs/system-header-simulator-cxx.h" + +typedef __typeof(sizeof(int)) size_t; +void *malloc(size_t); +void free(void *); +void *realloc(void *ptr, size_t size); +void *calloc(size_t nmemb, size_t size); +char *strdup(const char *s); + +void checkThatMallocCheckerIsRunning() { + malloc(4); +} // no-silence-warning{{Potential memory leak [unix.Malloc]}} + +int const_ptr_and_callback_def_param_null(int, const char *, int n, void (*)(void *) = 0); +void r11160612_no_callback() { + char *x = (char *)malloc(12); + const_ptr_and_callback_def_param_null(0, x, 12); +} // no-silence-warning{{Potential leak of memory pointed to by 'x' [unix.Malloc]}} + +#define ZERO_SIZE_PTR ((void *)16) + +void test_delete_ZERO_SIZE_PTR() { + int *Ptr = (int *)ZERO_SIZE_PTR; + // ZERO_SIZE_PTR is specially handled but only for malloc family + delete Ptr; // no-silence-warning{{Argument to 'delete' is a constant address (16), which is not memory allocated by 'new' [cplusplus.NewDelete]}} + // unix-silenced-warning@-1{{Argument to 'delete' is a constant address (16), which is not memory allocated by 'new' [cplusplus.NewDelete]}} +}