Skip to content

Commit 7d8616e

Browse files
authored
[analyzer] Fix stores through label locations (#89265)
Interestingly, this case crashed from the very beginning of the project, at least starting by clang-3. As a "fix" I just do the same thing as we do for concrete integers. It might not be the best we could do, but arguably, it's still better than crashing. Fixes #89185
1 parent 58764dd commit 7d8616e

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ Static Analyzer
695695
- Support C++23 static operator calls. (#GH84972)
696696
- Fixed a crash in ``security.cert.env.InvalidPtr`` checker when accidentally
697697
matched user-defined ``strerror`` and similar library functions. (GH#88181)
698+
- Fixed a crash when storing through an address that refers to the address of
699+
a label. (GH#89185)
698700

699701
New features
700702
^^^^^^^^^^^^

clang/lib/StaticAnalyzer/Core/RegionStore.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,11 +2358,12 @@ StoreRef RegionStoreManager::killBinding(Store ST, Loc L) {
23582358

23592359
RegionBindingsRef
23602360
RegionStoreManager::bind(RegionBindingsConstRef B, Loc L, SVal V) {
2361-
if (L.getAs<loc::ConcreteInt>())
2361+
// We only care about region locations.
2362+
auto MemRegVal = L.getAs<loc::MemRegionVal>();
2363+
if (!MemRegVal)
23622364
return B;
23632365

2364-
// If we get here, the location should be a region.
2365-
const MemRegion *R = L.castAs<loc::MemRegionVal>().getRegion();
2366+
const MemRegion *R = MemRegVal->getRegion();
23662367

23672368
// Check if the region is a struct region.
23682369
if (const TypedValueRegion* TR = dyn_cast<TypedValueRegion>(R)) {

clang/test/Analysis/gh-issue-89185.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s
2+
3+
void clang_analyzer_dump(char);
4+
void clang_analyzer_dump_ptr(char*);
5+
6+
// https://github.com/llvm/llvm-project/issues/89185
7+
void binding_to_label_loc() {
8+
char *b = &&MyLabel;
9+
MyLabel:
10+
*b = 0; // no-crash
11+
clang_analyzer_dump_ptr(b); // expected-warning {{&&MyLabel}}
12+
clang_analyzer_dump(*b); // expected-warning {{Unknown}}
13+
// FIXME: We should never reach here, as storing to a label is invalid.
14+
}

0 commit comments

Comments
 (0)