[llvm-commits] [llvm] r104855 - in /llvm/trunk: include/llvm/Support/MemoryBuffer.h lib/Linker/LinkItems.cpp lib/Support/MemoryBuffer.cpp lib/VMCore/Core.cpp
Dan Gohman
gohman at apple.com
Thu May 27 10:31:51 PDT 2010
Author: djg
Date: Thu May 27 12:31:51 2010
New Revision: 104855
URL: http://llvm.org/viewvc/llvm-project?rev=104855&view=rev
Log:
Add basic error checking to MemoryBuffer::getSTDIN.
Modified:
llvm/trunk/include/llvm/Support/MemoryBuffer.h
llvm/trunk/lib/Linker/LinkItems.cpp
llvm/trunk/lib/Support/MemoryBuffer.cpp
llvm/trunk/lib/VMCore/Core.cpp
Modified: llvm/trunk/include/llvm/Support/MemoryBuffer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MemoryBuffer.h?rev=104855&r1=104854&r2=104855&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MemoryBuffer.h (original)
+++ llvm/trunk/include/llvm/Support/MemoryBuffer.h Thu May 27 12:31:51 2010
@@ -89,7 +89,8 @@
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
Modified: llvm/trunk/lib/Linker/LinkItems.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkItems.cpp?rev=104855&r1=104854&r2=104855&view=diff
==============================================================================
--- llvm/trunk/lib/Linker/LinkItems.cpp (original)
+++ llvm/trunk/lib/Linker/LinkItems.cpp Thu May 27 12:31:51 2010
@@ -160,16 +160,17 @@
// Check for a file of name "-", which means "read standard input"
if (File.str() == "-") {
std::auto_ptr<Module> 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);
}
Modified: llvm/trunk/lib/Support/MemoryBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MemoryBuffer.cpp?rev=104855&r1=104854&r2=104855&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Thu May 27 12:31:51 2010
@@ -137,7 +137,7 @@
int64_t FileSize,
struct stat *FileInfo) {
if (Filename == "-")
- return getSTDIN();
+ return getSTDIN(ErrStr);
return getFile(Filename, ErrStr, FileSize, FileInfo);
}
@@ -263,7 +263,7 @@
};
}
-MemoryBuffer *MemoryBuffer::getSTDIN() {
+MemoryBuffer *MemoryBuffer::getSTDIN(std::string *ErrStr) {
char Buffer[4096*4];
std::vector<char> FileData;
@@ -279,6 +279,11 @@
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();
Modified: llvm/trunk/lib/VMCore/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Core.cpp?rev=104855&r1=104854&r2=104855&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Core.cpp (original)
+++ llvm/trunk/lib/VMCore/Core.cpp Thu May 27 12:31:51 2010
@@ -2205,15 +2205,14 @@
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) {
More information about the llvm-commits
mailing list