Skip to content

Commit 86c443d

Browse files
ArcsinXmemfrob
authored andcommitted
[clangd] Fix crash in bugprone-bad-signal-to-kill-thread clang-tidy check.
Inside clangd, clang-tidy checks don't see preprocessor events in the preamble. This leads to `Token::PtrData == nullptr` for tokens that the macro is defined to. E.g. `#define SIGTERM 15`: - Token::Kind == tok::numeric_constant (Token::isLiteral() == true) - Token::UintData == 2 - Token::PtrData == nullptr As the result of this, bugprone-bad-signal-to-kill-thread check crashes at null-dereference inside clangd. Reviewed By: hokein Differential Revision: https://reviews.llvm.org/D85417
1 parent 7c273a1 commit 86c443d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) {
3939
return llvm::None;
4040
const MacroInfo *MI = PP->getMacroInfo(It->first);
4141
const Token &T = MI->tokens().back();
42-
if (!T.isLiteral())
42+
if (!T.isLiteral() || !T.getLiteralData())
4343
return llvm::None;
4444
StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
4545

clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,21 @@ TEST(DiagnosticTest, ClangTidySuppressionCommentTrumpsWarningAsError) {
438438
EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
439439
}
440440

441+
TEST(DiagnosticTest, ClangTidyNoLiteralDataInMacroToken) {
442+
Annotations Main(R"cpp(
443+
#define SIGTERM 15
444+
using pthread_t = int;
445+
int pthread_kill(pthread_t thread, int sig);
446+
int func() {
447+
pthread_t thread;
448+
return pthread_kill(thread, 0);
449+
}
450+
)cpp");
451+
TestTU TU = TestTU::withCode(Main.code());
452+
TU.ClangTidyChecks = "-*,bugprone-bad-signal-to-kill-thread";
453+
EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); // no-crash
454+
}
455+
441456
TEST(DiagnosticsTest, Preprocessor) {
442457
// This looks like a preamble, but there's an #else in the middle!
443458
// Check that:

0 commit comments

Comments
 (0)