Fix an assertion abort on sparc. malloc(0) is allowed to
authorGabor Greif <ggreif@gmail.com>
Thu, 11 Oct 2007 19:40:35 +0000 (19:40 +0000)
committerGabor Greif <ggreif@gmail.com>
Thu, 11 Oct 2007 19:40:35 +0000 (19:40 +0000)
return NULL.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42871 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ExecutionEngine/Interpreter/Execution.cpp

index 95e2143787cbaf0cff5928f44874f0a5335c224d..f11cf816b22bb4dbbe5e902513b8475fd79ffd4b 100644 (file)
@@ -24,6 +24,7 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/MathExtras.h"
 #include <cmath>
+#include <algorithm>
 using namespace llvm;
 
 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
@@ -747,7 +748,8 @@ void Interpreter::visitAllocationInst(AllocationInst &I) {
 
   unsigned TypeSize = (size_t)TD.getTypeSize(Ty);
 
-  unsigned MemToAlloc = NumElements * TypeSize;
+  // Avoid malloc-ing zero bytes, use max()...
+  unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
 
   // Allocate enough memory to hold the type...
   void *Memory = malloc(MemToAlloc);