Skip to content

Commit 87cdfad

Browse files
vabridgersZijunZhaoCCK
authored andcommitted
[analyzer] Fix crash analyzing _BitInt() in evalIntegralCast (llvm#65887)
evalIntegralCast was using makeIntVal, and when _BitInt() types were introduced this exposed a crash in evalIntegralCast as a result. Improve evalIntegralCast to use makeIntVal more efficiently to avoid the crash exposed by use of _BitInt. This was caught with our internal randomized testing. <src-root>/llvm/include/llvm/ADT/APInt.h:1510: int64_t llvm::APInt::getSExtValue() const: Assertion `getSignificantBits() <= 64 && "Too many bits for int64_t"' failed.a ... llvm#9 <address> llvm::APInt::getSExtValue() const <src-root>/llvm/include/llvm/ADT/APInt.h:1510:5 llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>, clang::ento::SVal, clang::QualType, clang::QualType) <src-root>/clang/lib/StaticAnalyzer/Core/SValBuilder.cpp:607:24 clang::Expr const*, clang::ento::ExplodedNode*, clang::ento::ExplodedNodeSet&) <src-root>/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp:413:61 ... Fixes: llvm#61960 Reviewed By: donat.nagy
1 parent 39b23a7 commit 87cdfad

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

clang/lib/StaticAnalyzer/Core/SValBuilder.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,9 @@ SVal SValBuilder::evalIntegralCast(ProgramStateRef state, SVal val,
598598
APSIntType ToType(getContext().getTypeSize(castTy),
599599
castTy->isUnsignedIntegerType());
600600
llvm::APSInt ToTypeMax = ToType.getMaxValue();
601-
NonLoc ToTypeMaxVal =
602-
makeIntVal(ToTypeMax.isUnsigned() ? ToTypeMax.getZExtValue()
603-
: ToTypeMax.getSExtValue(),
604-
castTy)
605-
.castAs<NonLoc>();
601+
602+
NonLoc ToTypeMaxVal = makeIntVal(ToTypeMax);
603+
606604
// Check the range of the symbol being casted against the maximum value of the
607605
// target type.
608606
NonLoc FromVal = val.castAs<NonLoc>();

clang/test/Analysis/bitint-no-crash.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core \
2+
// RUN: -analyzer-checker=debug.ExprInspection \
3+
// RUN: -verify %s
4+
5+
// Don't crash when using _BitInt()
6+
// expected-no-diagnostics
7+
_BitInt(256) a;
8+
_BitInt(129) b;
9+
void c() {
10+
b = a;
11+
}

0 commit comments

Comments
 (0)