From 30377e780918aacda1befa47054c61b0155c194c Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 27 May 2010 17:31:51 +0000 Subject: [PATCH 1/1] Add basic error checking to MemoryBuffer::getSTDIN. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@104855 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/MemoryBuffer.h | 3 ++- lib/Linker/LinkItems.cpp | 21 +++++++++++---------- lib/Support/MemoryBuffer.cpp | 9 +++++++-- lib/VMCore/Core.cpp | 13 ++++++------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/include/llvm/Support/MemoryBuffer.h b/include/llvm/Support/MemoryBuffer.h index ef7af696840..38f61935d97 100644 --- a/include/llvm/Support/MemoryBuffer.h +++ b/include/llvm/Support/MemoryBuffer.h @@ -89,7 +89,8 @@ public: StringRef BufferName = ""); /// getSTDIN - Read all of stdin into a file buffer, and return it. - static MemoryBuffer *getSTDIN(); + /// If an error occurs, this returns null and fills in *ErrStr with a reason. + static MemoryBuffer *getSTDIN(std::string *ErrStr = 0); /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin diff --git a/lib/Linker/LinkItems.cpp b/lib/Linker/LinkItems.cpp index f0c7b68e69b..1be2becc86c 100644 --- a/lib/Linker/LinkItems.cpp +++ b/lib/Linker/LinkItems.cpp @@ -160,16 +160,17 @@ bool Linker::LinkInFile(const sys::Path &File, bool &is_native) { // Check for a file of name "-", which means "read standard input" if (File.str() == "-") { std::auto_ptr M; - MemoryBuffer *Buffer = MemoryBuffer::getSTDIN(); - if (!Buffer->getBufferSize()) { - delete Buffer; - Error = "standard input is empty"; - } else { - M.reset(ParseBitcodeFile(Buffer, Context, &Error)); - delete Buffer; - if (M.get()) - if (!LinkInModule(M.get(), &Error)) - return false; + if (MemoryBuffer *Buffer = MemoryBuffer::getSTDIN(&Error)) { + if (!Buffer->getBufferSize()) { + delete Buffer; + Error = "standard input is empty"; + } else { + M.reset(ParseBitcodeFile(Buffer, Context, &Error)); + delete Buffer; + if (M.get()) + if (!LinkInModule(M.get(), &Error)) + return false; + } } return error("Cannot link stdin: " + Error); } diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index 2b95089b524..ac795189036 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -137,7 +137,7 @@ MemoryBuffer *MemoryBuffer::getFileOrSTDIN(StringRef Filename, int64_t FileSize, struct stat *FileInfo) { if (Filename == "-") - return getSTDIN(); + return getSTDIN(ErrStr); return getFile(Filename, ErrStr, FileSize, FileInfo); } @@ -263,7 +263,7 @@ public: }; } -MemoryBuffer *MemoryBuffer::getSTDIN() { +MemoryBuffer *MemoryBuffer::getSTDIN(std::string *ErrStr) { char Buffer[4096*4]; std::vector FileData; @@ -279,6 +279,11 @@ MemoryBuffer *MemoryBuffer::getSTDIN() { FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes); } while (ReadBytes == sizeof(Buffer)); + if (!feof(stdin)) { + if (ErrStr) *ErrStr = "error reading from stdin"; + return 0; + } + FileData.push_back(0); // &FileData[Size] is invalid. So is &*FileData.end(). size_t Size = FileData.size(); MemoryBuffer *B = new STDINBufferFile(); diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp index bbf1375ab0c..295d14868f7 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -2205,15 +2205,14 @@ LLVMBool LLVMCreateMemoryBufferWithContentsOfFile( LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf, char **OutMessage) { - MemoryBuffer *MB = MemoryBuffer::getSTDIN(); - if (!MB->getBufferSize()) { - delete MB; - *OutMessage = strdup("stdin is empty."); - return 1; + std::string Error; + if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(&Error)) { + *OutMemBuf = wrap(MB); + return 0; } - *OutMemBuf = wrap(MB); - return 0; + *OutMessage = strdup(Error.c_str()); + return 1; } void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) { -- 2.34.1