Skip to content

Commit 0141a3c

Browse files
authored
[analyzer] Fix nullptr dereference for symbols from pointer invalidation (#106568)
As reported in #105648 (comment) commit 08ad8dc7154bf3ab79f750e6d5fb7df597c7601a introduced a nullptr dereference in the case when store contains a binding to a symbol that has no origin region associated with it, such as the symbol generated when a pointer is passed to an opaque function.
1 parent f08f9cd commit 0141a3c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,10 @@ static const MemSpaceRegion *getStackOrGlobalSpaceRegion(const MemRegion *R) {
308308
const MemRegion *getOriginBaseRegion(const MemRegion *Reg) {
309309
Reg = Reg->getBaseRegion();
310310
while (const auto *SymReg = dyn_cast<SymbolicRegion>(Reg)) {
311-
Reg = SymReg->getSymbol()->getOriginRegion()->getBaseRegion();
311+
const auto *OriginReg = SymReg->getSymbol()->getOriginRegion();
312+
if (!OriginReg)
313+
break;
314+
Reg = OriginReg->getBaseRegion();
312315
}
313316
return Reg;
314317
}

clang/test/Analysis/stack-addr-ps.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,21 @@ void caller_for_nested_leaking() {
126126
int *ptr = 0;
127127
caller_mid_for_nested_leaking(&ptr);
128128
}
129+
130+
// This used to crash StackAddrEscapeChecker because
131+
// it features a symbol conj_$1{struct c *, LC1, S763, #1}
132+
// that has no origin region.
133+
struct a {
134+
int member;
135+
};
136+
137+
struct c {
138+
struct a *nested_ptr;
139+
};
140+
void opaque(struct c*);
141+
struct c* get_c(void);
142+
void no_crash_for_symbol_without_origin_region(void) {
143+
struct c *ptr = get_c();
144+
opaque(ptr);
145+
ptr->nested_ptr->member++;
146+
} // No crash at the end of the function

0 commit comments

Comments
 (0)