[PATCH] D81061: [Analyzer][VLASizeChecker] Fix problem with zero index assumption.
Balázs Kéri via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 3 08:45:00 PDT 2020
balazske updated this revision to Diff 268216.
balazske added a comment.
Improved assumption on array size.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D81061/new/
https://reviews.llvm.org/D81061
Files:
clang/lib/StaticAnalyzer/Checkers/VLASizeChecker.cpp
clang/test/Analysis/vla.c
Index: clang/test/Analysis/vla.c
===================================================================
--- clang/test/Analysis/vla.c
+++ clang/test/Analysis/vla.c
@@ -137,3 +137,17 @@
clang_analyzer_eval(clang_analyzer_getExtent(&vla3m) == 2 * x * 4 * sizeof(int));
// expected-warning at -1{{TRUE}}
}
+
+// https://bugs.llvm.org/show_bug.cgi?id=46128
+// Analyzer doesn't handle more than simple symbolic expressions correct.
+// Just don't crash.
+extern void foo(void);
+int a;
+void b() {
+ int c = a + 1;
+ for (;;) {
+ int d[c];
+ for (; 0 < c;)
+ foo();
+ }
+} // no-crash
Index: clang/lib/StaticAnalyzer/Checkers/VLASizeChecker.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/VLASizeChecker.cpp
+++ clang/lib/StaticAnalyzer/Checkers/VLASizeChecker.cpp
@@ -122,11 +122,19 @@
return State;
if (const llvm::APSInt *IndexLVal = SVB.getKnownValue(State, IndexLength)) {
+ uint64_t IndexL = IndexLVal->getZExtValue();
+ if (IndexL == 0) {
+ // Despite the previous assumption for positive size,
+ // this value might be non-positive (probably caused by other bugs).
+ // At least check for zero again.
+ // Assume that this is a more exact fact than the previous assumptions
+ // (in checkVLAIndexSize), so report error too.
+ reportBug(VLA_Zero, SizeE, State, C);
+ return nullptr;
+ }
// Check if the array size will overflow.
// Size overflow check does not work with symbolic expressions because a
// overflow situation can not be detected easily.
- uint64_t IndexL = IndexLVal->getZExtValue();
- assert(IndexL > 0 && "Index length should have been checked for zero.");
if (KnownSize <= SizeMax / IndexL) {
KnownSize *= IndexL;
} else {
@@ -166,35 +174,21 @@
return nullptr;
}
- // Check if the size is zero.
+ QualType SizeTy = SizeE->getType();
DefinedSVal SizeD = SizeV.castAs<DefinedSVal>();
-
- ProgramStateRef StateNotZero, StateZero;
- std::tie(StateNotZero, StateZero) = State->assume(SizeD);
-
- if (StateZero && !StateNotZero) {
- reportBug(VLA_Zero, SizeE, StateZero, C);
- return nullptr;
- }
-
- // From this point on, assume that the size is not zero.
- State = StateNotZero;
-
- // Check if the size is negative.
SValBuilder &SVB = C.getSValBuilder();
-
- QualType SizeTy = SizeE->getType();
DefinedOrUnknownSVal Zero = SVB.makeZeroVal(SizeTy);
- SVal LessThanZeroVal = SVB.evalBinOp(State, BO_LT, SizeD, Zero, SizeTy);
- if (Optional<DefinedSVal> LessThanZeroDVal =
- LessThanZeroVal.getAs<DefinedSVal>()) {
- ConstraintManager &CM = C.getConstraintManager();
- ProgramStateRef StatePos, StateNeg;
+ // Check if the size is zero or negative.
+ SVal PositiveVal = SVB.evalBinOp(State, BO_GT, SizeD, Zero, SizeTy);
+ if (Optional<DefinedSVal> PositiveDVal = PositiveVal.getAs<DefinedSVal>()) {
+ ProgramStateRef StatePos, StateNotPos;
- std::tie(StateNeg, StatePos) = CM.assumeDual(State, *LessThanZeroDVal);
- if (StateNeg && !StatePos) {
- reportBug(VLA_Negative, SizeE, State, C);
+ std::tie(StatePos, StateNotPos) = State->assume(*PositiveDVal);
+ if (StateNotPos && !StatePos) {
+ ConditionTruthVal IsZeroSize = StateNotPos->isNull(SizeD);
+ reportBug(IsZeroSize.isConstrainedTrue() ? VLA_Zero : VLA_Negative, SizeE,
+ State, C);
return nullptr;
}
State = StatePos;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81061.268216.patch
Type: text/x-patch
Size: 3522 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200603/96a23ff6/attachment.bin>
More information about the cfe-commits
mailing list