Fix memory leak in StringRef::edit_distance(). 'Allocated' could be leaked on an...
authorTed Kremenek <kremenek@apple.com>
Sun, 7 Nov 2010 06:09:02 +0000 (06:09 +0000)
committerTed Kremenek <kremenek@apple.com>
Sun, 7 Nov 2010 06:09:02 +0000 (06:09 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@118370 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/StringRef.cpp

index 5ad862815b53b8c500095b67ec5adb9901b606c3..d5fd12727dbc04300af88edbbfe60d3a461c7054 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/APInt.h"
+#include "llvm/ADT/OwningPtr.h"
 #include <bitset>
 
 using namespace llvm;
@@ -84,10 +85,12 @@ unsigned StringRef::edit_distance(llvm::StringRef Other,
 
   const unsigned SmallBufferSize = 64;
   unsigned SmallBuffer[SmallBufferSize];
-  unsigned *Allocated = 0;
+  llvm::OwningArrayPtr<unsigned> Allocated;
   unsigned *previous = SmallBuffer;
-  if (2*(n + 1) > SmallBufferSize)
-    Allocated = previous = new unsigned [2*(n+1)];
+  if (2*(n + 1) > SmallBufferSize) {
+    previous = new unsigned [2*(n+1)];
+    Allocated.reset(previous);
+  }
   unsigned *current = previous + (n + 1);
   
   for (unsigned i = 0; i <= n; ++i) 
@@ -118,8 +121,6 @@ unsigned StringRef::edit_distance(llvm::StringRef Other,
   }
 
   unsigned Result = previous[n];
-  delete [] Allocated;
-  
   return Result;
 }