From: Devin Coughlin Date: Sat, 20 Jan 2018 23:11:17 +0000 (+0000) Subject: [analyzer] Provide a check name when MallocChecker enables CStringChecker X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=23a20415d5f95650e2e51200580ca97f2df1061c;p=clang [analyzer] Provide a check name when MallocChecker enables CStringChecker Fix an assertion failure caused by a missing CheckName. The malloc checker enables "basic" support in the CStringChecker, which causes some CString bounds checks to be enabled. In this case, make sure that we have a valid CheckName for the BugType. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@323052 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Checkers/CStringChecker.cpp b/lib/StaticAnalyzer/Checkers/CStringChecker.cpp index 079032329a..bd4033784e 100644 --- a/lib/StaticAnalyzer/Checkers/CStringChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/CStringChecker.cpp @@ -309,9 +309,19 @@ ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C, if (!N) return nullptr; + CheckName Name; + // These checks are either enabled by the CString out-of-bounds checker + // explicitly or the "basic" CStringNullArg checker support that Malloc + // checker enables. + assert(Filter.CheckCStringOutOfBounds || Filter.CheckCStringNullArg); + if (Filter.CheckCStringOutOfBounds) + Name = Filter.CheckNameCStringOutOfBounds; + else + Name = Filter.CheckNameCStringNullArg; + if (!BT_Bounds) { BT_Bounds.reset(new BuiltinBug( - Filter.CheckNameCStringOutOfBounds, "Out-of-bound array access", + Name, "Out-of-bound array access", "Byte string function accesses out-of-bound array element")); } BuiltinBug *BT = static_cast(BT_Bounds.get()); diff --git a/test/Analysis/Inputs/system-header-simulator.h b/test/Analysis/Inputs/system-header-simulator.h index 2e6f1e7d4a..ff69a1502b 100644 --- a/test/Analysis/Inputs/system-header-simulator.h +++ b/test/Analysis/Inputs/system-header-simulator.h @@ -32,6 +32,7 @@ typedef __typeof(sizeof(int)) size_t; size_t strlen(const char *); char *strcpy(char *restrict, const char *restrict); +char *strncpy(char *dst, const char *src, size_t n); void *memcpy(void *dst, const void *src, size_t n); typedef unsigned long __darwin_pthread_key_t; diff --git a/test/Analysis/malloc.c b/test/Analysis/malloc.c index e08ec1b76c..6e3f3faaa1 100644 --- a/test/Analysis/malloc.c +++ b/test/Analysis/malloc.c @@ -1777,6 +1777,15 @@ void freeFunctionPtr() { free((void *)fnptr); // expected-warning {{Argument to free() is a function pointer}} } +// Enabling the malloc checker enables some of the buffer-checking portions +// of the C-string checker. +void cstringchecker_bounds_nocrash() { + char *p = malloc(2); + strncpy(p, "AAA", sizeof("AAA")); // expected-warning {{Size argument is greater than the length of the destination buffer}} + + free(p); +} + // ---------------------------------------------------------------------------- // False negatives.