Skip to content

Commit e6a800f

Browse files
StdVariantChecker: fix crash when argument to std::get is UnknownVal
1 parent 2681497 commit e6a800f

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

clang/lib/StaticAnalyzer/Checkers/StdVariantChecker.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,12 @@ class StdVariantChecker : public Checker<eval::Call, check::RegionChanges> {
219219
bool handleStdGetCall(const CallEvent &Call, CheckerContext &C) const {
220220
ProgramStateRef State = C.getState();
221221

222-
const auto &ArgType = Call.getArgSVal(0)
223-
.getType(C.getASTContext())
224-
->getPointeeType()
225-
.getTypePtr();
222+
SVal ArgSVal = Call.getArgSVal(0);
223+
if (ArgSVal.isUnknown())
224+
return false;
225+
226+
const auto &ArgType =
227+
ArgSVal.getType(C.getASTContext())->getPointeeType().getTypePtr();
226228
// We have to make sure that the argument is an std::variant.
227229
// There is another std::get with std::pair argument
228230
if (!isStdVariant(ArgType))

clang/test/Analysis/std-variant-checker.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,15 @@ void nonInlineFunctionCallPtr() {
355355
char c = std::get<char> (v); // no-warning
356356
(void)a;
357357
(void)c;
358-
}
358+
}
359+
360+
// ----------------------------------------------------------------------------//
361+
// Misc
362+
// ----------------------------------------------------------------------------//
363+
364+
using uintptr_t = unsigned long long;
365+
366+
void unknownVal() {
367+
// force the argument to be UnknownVal
368+
(void)std::get<int>(*(std::variant<int, float>*)(uintptr_t)3.14f); // no crash
369+
}

0 commit comments

Comments
 (0)