Author: Aleksandr Platonov Date: 2020-08-10T15:12:03+03:00 New Revision: dcb8d3b72234ea557df2af1141ad30bf1670a03a
URL: https://github.com/llvm/llvm-project/commit/dcb8d3b72234ea557df2af1141ad30bf1670a03a DIFF: https://github.com/llvm/llvm-project/commit/dcb8d3b72234ea557df2af1141ad30bf1670a03a.diff LOG: [clang-tidy] Fix a crash in bugprone-not-null-terminated-result check when `__STDC_WANT_LIB_EXT1__` is not a literal. If `__STDC_WANT_LIB_EXT1__` is not a literal (e.g. `#define __STDC_WANT_LIB_EXT1__ ((unsigned)1)`) bugprone-not-null-terminated-result check crashes. Stack dump: ``` #0 0x0000000002185e6a llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/llvm-project/build/bin/clang-tidy+0x2185e6a) #1 0x0000000002183e8c llvm::sys::RunSignalHandlers() (/llvm-project/build/bin/clang-tidy+0x2183e8c) #2 0x0000000002183ff3 SignalHandler(int) (/llvm-project/build/bin/clang-tidy+0x2183ff3) #3 0x00007f08d91b1390 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x11390) #4 0x00000000021338bb llvm::StringRef::getAsInteger(unsigned int, llvm::APInt&) const (/llvm-project/build/bin/clang-tidy+0x21338bb) #5 0x000000000052051c clang::tidy::bugprone::NotNullTerminatedResultCheck::check(clang::ast_matchers::MatchFinder::MatchResult const&) (/llvm-project/build/bin/clang-tidy+0x52051c) ``` Reviewed By: hokein Differential Revision: https://reviews.llvm.org/D85525 Added: clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c Modified: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp index 7d484d777165..9a44ba2f58a5 100644 --- a/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp @@ -805,10 +805,12 @@ void NotNullTerminatedResultCheck::check( // PP->getMacroInfo() returns nullptr if macro has no definition. if (MI) { const auto &T = MI->tokens().back(); - StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength()); - llvm::APInt IntValue; - ValueStr.getAsInteger(10, IntValue); - AreSafeFunctionsWanted = IntValue.getZExtValue(); + if (T.isLiteral() && T.getLiteralData()) { + StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength()); + llvm::APInt IntValue; + ValueStr.getAsInteger(10, IntValue); + AreSafeFunctionsWanted = IntValue.getZExtValue(); + } } } diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c new file mode 100644 index 000000000000..bba807fb6e09 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c @@ -0,0 +1,16 @@ +// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \ +// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result + +#include "not-null-terminated-result-c.h" + +#define __STDC_LIB_EXT1__ 1 +#define __STDC_WANT_LIB_EXT1__ ((unsigned)1) + +void f(const char *src) { + char dest[13]; + memcpy_s(dest, 13, src, strlen(src) - 1); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result] + // CHECK-FIXES: char dest[14]; + // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1); +} + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits