[analyzer] Fix crash in MallocChecker.
authorDevin Coughlin <dcoughlin@apple.com>
Fri, 16 Dec 2016 18:41:40 +0000 (18:41 +0000)
committerDevin Coughlin <dcoughlin@apple.com>
Fri, 16 Dec 2016 18:41:40 +0000 (18:41 +0000)
Fix a crash in the MallocChecker when the extent size for the argument
to new[] is not known.

A patch by Abramo Bagnara and Dániel Krupp!

https://reviews.llvm.org/D27849

Differential Revision: https://reviews.llvm.org/D27849

llvm-svn: 289970

clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
clang/test/Analysis/out-of-bounds-new.cpp

index 07c6072..f7c4ea1 100644 (file)
@@ -1026,8 +1026,7 @@ ProgramStateRef MallocChecker::addExtentSize(CheckerContext &C,
   ASTContext &AstContext = C.getASTContext();
   CharUnits TypeSize = AstContext.getTypeSizeInChars(ElementType);
 
-  if (Optional<DefinedOrUnknownSVal> DefinedSize =
-          ElementCount.getAs<DefinedOrUnknownSVal>()) {
+  if (ElementCount.getAs<NonLoc>()) {
     DefinedOrUnknownSVal Extent = Region->getExtent(svalBuilder);
     // size in Bytes = ElementCount*TypeSize
     SVal SizeInBytes = svalBuilder.evalBinOpNN(
index 41ecbee..ee7bb1e 100644 (file)
@@ -148,3 +148,9 @@ void test_dynamic_size(int s) {
   int *buf = new int[s];
   buf[0] = 1; // no-warning
 }
+//Tests complex arithmetic
+//in new expression
+void test_dynamic_size2(unsigned m,unsigned n){
+  unsigned *U = nullptr;
+  U = new unsigned[m + n + 1];
+}