Skip to content

[analyzer] Fix crash when casting the result of a malformed fptr call #111390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,9 @@ SVal SValBuilder::evalIntegralCast(ProgramStateRef state, SVal val,
if (getContext().getTypeSize(castTy) >= getContext().getTypeSize(originalTy))
return evalCast(val, castTy, originalTy);

SymbolRef se = val.getAsSymbol();
if (!se) // Let evalCast handle non symbolic expressions.
auto AsNonLoc = val.getAs<NonLoc>();
SymbolRef AsSymbol = val.getAsSymbol();
if (!AsSymbol || !AsNonLoc) // Let evalCast handle non symbolic expressions.
return evalCast(val, castTy, originalTy);

// Find the maximum value of the target type.
Expand All @@ -613,15 +614,14 @@ SVal SValBuilder::evalIntegralCast(ProgramStateRef state, SVal val,

// Check the range of the symbol being casted against the maximum value of the
// target type.
NonLoc FromVal = val.castAs<NonLoc>();
QualType CmpTy = getConditionType();
NonLoc CompVal =
evalBinOpNN(state, BO_LE, FromVal, ToTypeMaxVal, CmpTy).castAs<NonLoc>();
NonLoc CompVal = evalBinOpNN(state, BO_LE, *AsNonLoc, ToTypeMaxVal, CmpTy)
.castAs<NonLoc>();
ProgramStateRef IsNotTruncated, IsTruncated;
std::tie(IsNotTruncated, IsTruncated) = state->assume(CompVal);
if (!IsNotTruncated && IsTruncated) {
// Symbol is truncated so we evaluate it as a cast.
return makeNonLoc(se, originalTy, castTy);
return makeNonLoc(AsSymbol, originalTy, castTy);
}
return evalCast(val, castTy, originalTy);
}
Expand Down
9 changes: 9 additions & 0 deletions clang/test/Analysis/range_casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,12 @@ void f15(long foo)
else
clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
}

int *getIntPtr(void) {
extern int *intPtr;
return intPtr;
}
char call_malformed_fptr() {
int (*fptr)(void) = (int (*)(void))getIntPtr;
return fptr(); // no-crash
}
Loading