diff options
author | Evan Cheng <evan.cheng@apple.com> | 2009-02-13 07:54:34 +0000 |
---|---|---|
committer | Evan Cheng <evan.cheng@apple.com> | 2009-02-13 07:54:34 +0000 |
commit | 122f882fb77dc3c07b4d5ed7387acd71d5926227 (patch) | |
tree | 3e8b423551c1e1db5d34fd8071a59beb103e8dda /lib/Support | |
parent | 6f9646c3788d01f552aa87c9940f978c66b758b0 (diff) | |
download | external_llvm-122f882fb77dc3c07b4d5ed7387acd71d5926227.zip external_llvm-122f882fb77dc3c07b4d5ed7387acd71d5926227.tar.gz external_llvm-122f882fb77dc3c07b4d5ed7387acd71d5926227.tar.bz2 |
If new[] fails, return 0 rather then trying to dereference a null pointer.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@64444 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support')
-rw-r--r-- | lib/Support/MemoryBuffer.cpp | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index 83f1494..0e60eab 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -109,6 +109,7 @@ MemoryBuffer *MemoryBuffer::getMemBufferCopy(const char *StartPtr, MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size, const char *BufferName) { char *Buf = new char[Size+1]; + if (!Buf) return 0; Buf[Size] = 0; MemoryBufferMem *SB = new MemoryBufferMem(Buf, Buf+Size, BufferName); // The memory for this buffer is owned by the MemoryBuffer. @@ -123,6 +124,7 @@ MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size, MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, const char *BufferName) { MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName); + if (!SB) return 0; memset(const_cast<char*>(SB->getBufferStart()), 0, Size+1); return SB; } @@ -209,9 +211,16 @@ MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr, return new MemoryBufferMMapFile(Filename, Pages, FileSize); } } - - OwningPtr<MemoryBuffer> SB; - SB.reset(MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename)); + + MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename); + if (!Buf) { + // Failed to create a buffer. + if (ErrStr) *ErrStr = "could not allocate buffer"; + ::close(FD); + return 0; + } + + OwningPtr<MemoryBuffer> SB(Buf); char *BufPtr = const_cast<char*>(SB->getBufferStart()); size_t BytesLeft = FileSize; |