Fix memory leak in StringRef::edit_distance(). 'Allocated' could be leaked on an early return.

llvm-svn: 118370
This commit is contained in:
Ted Kremenek 2010-11-07 06:09:02 +00:00
parent d48eca9b43
commit 3bb32def7e

View File

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