Skip to content

Commit

Permalink
[analyzer] Fix nullptr dereference for symbols from pointer invalidat…
Browse files Browse the repository at this point in the history
…ion (llvm#106568)

As reported in
llvm#105648 (comment)
commit 08ad8dc
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.
  • Loading branch information
necto authored Aug 29, 2024
1 parent f08f9cd commit 0141a3c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,10 @@ static const MemSpaceRegion *getStackOrGlobalSpaceRegion(const MemRegion *R) {
const MemRegion *getOriginBaseRegion(const MemRegion *Reg) {
Reg = Reg->getBaseRegion();
while (const auto *SymReg = dyn_cast<SymbolicRegion>(Reg)) {
Reg = SymReg->getSymbol()->getOriginRegion()->getBaseRegion();
const auto *OriginReg = SymReg->getSymbol()->getOriginRegion();
if (!OriginReg)
break;
Reg = OriginReg->getBaseRegion();
}
return Reg;
}
Expand Down
18 changes: 18 additions & 0 deletions clang/test/Analysis/stack-addr-ps.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,21 @@ void caller_for_nested_leaking() {
int *ptr = 0;
caller_mid_for_nested_leaking(&ptr);
}

// This used to crash StackAddrEscapeChecker because
// it features a symbol conj_$1{struct c *, LC1, S763, #1}
// that has no origin region.
struct a {
int member;
};

struct c {
struct a *nested_ptr;
};
void opaque(struct c*);
struct c* get_c(void);
void no_crash_for_symbol_without_origin_region(void) {
struct c *ptr = get_c();
opaque(ptr);
ptr->nested_ptr->member++;
} // No crash at the end of the function

0 comments on commit 0141a3c

Please sign in to comment.