[clang-tools-extra] 8671166 - [clang-tidy] Fix bugprone-bad-signal-to-kill-thread crash when `SIGTERM` was undefined after definition.
Aleksandr Platonov via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 6 00:29:22 PDT 2020
Author: Aleksandr Platonov
Date: 2020-08-06T10:28:20+03:00
New Revision: 86711668330cf48325a5e960f30d33f6a7364db2
URL: https://github.com/llvm/llvm-project/commit/86711668330cf48325a5e960f30d33f6a7364db2
DIFF: https://github.com/llvm/llvm-project/commit/86711668330cf48325a5e960f30d33f6a7364db2.diff
LOG: [clang-tidy] Fix bugprone-bad-signal-to-kill-thread crash when `SIGTERM` was undefined after definition.
`PP->getMacroInfo()` returns nullptr for undefined macro, which leads to null-dereference at `MI->tockens().back()`.
Stack dump:
```
#0 0x000000000217d15a llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/llvm-project/build/bin/clang-tidy+0x217d15a)
#1 0x000000000217b17c llvm::sys::RunSignalHandlers() (/llvm-project/build/bin/clang-tidy+0x217b17c)
#2 0x000000000217b2e3 SignalHandler(int) (/llvm-project/build/bin/clang-tidy+0x217b2e3)
#3 0x00007f39be5b1390 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x11390)
#4 0x0000000000593532 clang::tidy::bugprone::BadSignalToKillThreadCheck::check(clang::ast_matchers::MatchFinder::MatchResult const&) (/llvm-project/build/bin/clang-tidy+0x593532)
```
Reviewed By: hokein
Differential Revision: https://reviews.llvm.org/D85401
Added:
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
Modified:
clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
index 3591a7da8eb7..2c7b7b9faa9b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -30,7 +30,8 @@ static Preprocessor *PP;
void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) {
const auto IsSigterm = [](const auto &KeyValue) -> bool {
- return KeyValue.first->getName() == "SIGTERM";
+ return KeyValue.first->getName() == "SIGTERM" &&
+ KeyValue.first->hasMacroDefinition();
};
const auto TryExpandAsInteger =
[](Preprocessor::macro_iterator It) -> Optional<unsigned> {
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
new file mode 100644
index 000000000000..7c2ae16a08e8
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
@@ -0,0 +1,11 @@
+// RUN: clang-tidy %s --checks=-*,bugprone-bad-signal-to-kill-thread -- | count 0
+
+#define SIGTERM 15
+#undef SIGTERM // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+ pthread_t thread;
+ return pthread_kill(thread, 0);
+}
More information about the cfe-commits
mailing list