diff options
author | Ted Kremenek <kremenek@apple.com> | 2010-11-07 06:09:02 +0000 |
---|---|---|
committer | Ted Kremenek <kremenek@apple.com> | 2010-11-07 06:09:02 +0000 |
commit | 3e100cf58216ea1f1aeaff233f5b08f51492f363 (patch) | |
tree | 1c47cf75b6815061a2d82b0f2d46fac18a5c3268 /llvm/lib/Support/StringRef.cpp | |
parent | 7bcbfe9c80afd4ac0d2f4e9b59711ddb3a3e2caa (diff) | |
download | bcm5719-llvm-3e100cf58216ea1f1aeaff233f5b08f51492f363.tar.gz bcm5719-llvm-3e100cf58216ea1f1aeaff233f5b08f51492f363.zip |
Fix memory leak in StringRef::edit_distance(). 'Allocated' could be leaked on an early return.
llvm-svn: 118370
Diffstat (limited to 'llvm/lib/Support/StringRef.cpp')
-rw-r--r-- | llvm/lib/Support/StringRef.cpp | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp index 5ad862815b5..d5fd12727db 100644 --- a/llvm/lib/Support/StringRef.cpp +++ b/llvm/lib/Support/StringRef.cpp @@ -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; } |