[llvm-commits] [llvm] r118370 - /llvm/trunk/lib/Support/StringRef.cpp
Ted Kremenek
kremenek at apple.com
Sat Nov 6 23:09:02 PDT 2010
Author: kremenek
Date: Sun Nov 7 01:09:02 2010
New Revision: 118370
URL: http://llvm.org/viewvc/llvm-project?rev=118370&view=rev
Log:
Fix memory leak in StringRef::edit_distance(). 'Allocated' could be leaked on an early return.
Modified:
llvm/trunk/lib/Support/StringRef.cpp
Modified: llvm/trunk/lib/Support/StringRef.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringRef.cpp?rev=118370&r1=118369&r2=118370&view=diff
==============================================================================
--- llvm/trunk/lib/Support/StringRef.cpp (original)
+++ llvm/trunk/lib/Support/StringRef.cpp Sun Nov 7 01:09:02 2010
@@ -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 @@
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 Result = previous[n];
- delete [] Allocated;
-
return Result;
}
More information about the llvm-commits
mailing list