Skip to content

Commit

Permalink
[clang-tidy] Fix bugprone-bad-signal-to-kill-thread crash when `SIGTE…
Browse files Browse the repository at this point in the history
…RM` 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)
 rust-lang#2 0x000000000217b2e3 SignalHandler(int) (/llvm-project/build/bin/clang-tidy+0x217b2e3)
 rust-lang#3 0x00007f39be5b1390 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x11390)
 rust-lang#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
  • Loading branch information
ArcsinX committed Aug 6, 2020
1 parent 504a197 commit 8671166
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 8671166

Please sign in to comment.