Skip to content

Commit a6506b7

Browse files
committed
Add handling for Loc::ConcreteInt
1 parent 24a6aad commit a6506b7

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,17 @@ void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
311311
for (ExplodedNode *Node : DstEvalLoc) {
312312
ProgramStateRef State = Node->getState();
313313
const LocationContext *LCtx = Node->getLocationContext();
314-
// getAsRegion should always be successful since Ex is an lvalue:
315-
SVal OrigV = State->getSVal(State->getSVal(Ex, LCtx).getAsRegion());
316-
SVal CastedV =
317-
svalBuilder.evalCast(svalBuilder.simplifySVal(State, OrigV),
318-
CastE->getType(), Ex->getType());
319-
314+
// Although `Ex` is an lvalue, it could have `Loc::ConcreteInt` kind
315+
// (e.g., `(int *)123456`). In such cases, there is no MemRegion
316+
// available and we can't get the value to be casted.
317+
const MemRegion *MR = State->getSVal(Ex, LCtx).getAsRegion();
318+
SVal CastedV = UnknownVal();
319+
320+
if (MR) {
321+
SVal OrigV = State->getSVal(MR);
322+
CastedV = svalBuilder.evalCast(svalBuilder.simplifySVal(State, OrigV),
323+
CastE->getType(), Ex->getType());
324+
}
320325
State = State->BindExpr(CastE, LCtx, CastedV);
321326
Bldr.generateNode(CastE, Node, State);
322327
}

clang/test/Analysis/builtin_bitcast.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clang_analyze_cc1 -triple x86_64-unknown-unknown -verify %s \
2-
// RUN: -analyzer-checker=core,debug.ExprInspection
2+
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-disable-checker=core
33

44
template <typename T> void clang_analyzer_dump(T);
55
using size_t = decltype(sizeof(int));
@@ -60,4 +60,9 @@ namespace {
6060
// expected-warning-re@-1 {{{{[0-9]+}} (Loc)}}
6161
return ptr == __builtin_bit_cast(void*, static_cast<uintptr_t>(-1));
6262
}
63+
64+
void check_loc_concreteInt() {
65+
clang_analyzer_dump(__builtin_bit_cast(unsigned, *(reinterpret_cast<int*>(0xdeadbeef))));
66+
// expected-warning@-1 {{Unknown}}
67+
}
6368
}

0 commit comments

Comments
 (0)