Add basic error checking to MemoryBuffer::getSTDIN.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@104855 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2010-05-27 17:31:51 +00:00
parent 95131fcb67
commit 30377e7809
4 changed files with 26 additions and 20 deletions

View File

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

View File

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

View File

@ -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<char> 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();

View File

@ -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) {