[PATCH] D36780: [llvm-dlltool] Don't crash if no def file is provided or it can't be opened
Martin Storsjö via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 15 22:09:26 PDT 2017
mstorsjo created this revision.
Herald added subscribers: inglorion, mehdi_amini.
https://reviews.llvm.org/D36780
Files:
lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
Index: lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
===================================================================
--- lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
+++ lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
@@ -60,11 +60,13 @@
// Opens a file. Path has to be resolved already.
// Newly created memory buffers are owned by this driver.
-MemoryBufferRef openFile(StringRef Path) {
+Optional<MemoryBufferRef> openFile(StringRef Path) {
ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MB = MemoryBuffer::getFile(Path);
- if (std::error_code EC = MB.getError())
+ if (std::error_code EC = MB.getError()) {
llvm::errs() << "fail openFile: " << EC.message() << "\n";
+ return None;
+ }
MemoryBufferRef MBRef = MB.get()->getMemBufferRef();
OwningMBs.push_back(std::move(MB.get())); // take ownership
@@ -115,11 +117,17 @@
for (auto *Arg : Args.filtered(OPT_UNKNOWN))
llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
- MemoryBufferRef MB;
- if (auto *Arg = Args.getLastArg(OPT_d))
+ Optional<MemoryBufferRef> MB;
+ if (auto *Arg = Args.getLastArg(OPT_d)) {
MB = openFile(Arg->getValue());
+ if (!MB)
+ return 1;
+ } else {
+ llvm::errs() << "no definition file specified\n";
+ return 1;
+ }
- if (!MB.getBufferSize()) {
+ if (!MB->getBufferSize()) {
llvm::errs() << "definition file empty\n";
return 1;
}
@@ -134,7 +142,7 @@
}
Expected<COFFModuleDefinition> Def =
- parseCOFFModuleDefinition(MB, Machine, true);
+ parseCOFFModuleDefinition(*MB, Machine, true);
if (!Def) {
llvm::errs() << "error parsing definition\n"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36780.111305.patch
Type: text/x-patch
Size: 1659 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170816/4dc088c0/attachment.bin>
More information about the llvm-commits
mailing list