1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 03:33:20 +01:00

If new[] fails, return 0 rather then trying to dereference a null pointer.

llvm-svn: 64444
This commit is contained in:
Evan Cheng 2009-02-13 07:54:34 +00:00
parent 260e80bd90
commit 643730a194

View File

@ -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;