Skip to content

Commit af05be9

Browse files
committed
[clang][analyzer] Fix alpha.unix.BlockInCriticalSection
When analyzing C code with function pointers the checker crashes because of how the implementation extracts IdentifierInfo. Without the fix, this test crashes. Add crashing test
1 parent 415616d commit af05be9

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

clang/lib/StaticAnalyzer/Checkers/BlockInCriticalSectionChecker.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17+
#include "clang/Analysis/AnalysisDeclContext.h"
1718
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
1819
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
1920
#include "clang/StaticAnalyzer/Core/Checker.h"
@@ -103,9 +104,10 @@ class RAIIMutexDescriptor {
103104
// this function is called instead of early returning it. To avoid this, a
104105
// bool variable (IdentifierInfoInitialized) is used and the function will
105106
// be run only once.
106-
Guard = &Call.getCalleeAnalysisDeclContext()->getASTContext().Idents.get(
107-
GuardName);
108-
IdentifierInfoInitialized = true;
107+
if (AnalysisDeclContext *CalleCtx = Call.getCalleeAnalysisDeclContext()) {
108+
Guard = &CalleCtx->getASTContext().Idents.get(GuardName);
109+
IdentifierInfoInitialized = true;
110+
}
109111
}
110112
}
111113

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.unix.BlockInCriticalSection -verify %s
2+
// expected-no-diagnostics
3+
4+
// This should not crash
5+
int (*a)(void);
6+
void b(void) { a(); }

0 commit comments

Comments
 (0)