[llvm-reduce] Exit when input module is malformed
authorLangston Barrett <langston.barrett@gmail.com>
Tue, 25 May 2021 16:56:28 +0000 (09:56 -0700)
committerArthur Eubanks <aeubanks@google.com>
Tue, 25 May 2021 17:01:12 +0000 (10:01 -0700)
The parseInputFile function returns an empty unique_ptr to signal an
error, like when the input file doesn't exist, or is malformed. In this
case, the tool should exit immediately rather than segfault by
dereferencing the unique_ptr later.

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D102891

llvm/test/tools/llvm-reduce/fail-file-open.test [new file with mode: 0644]
llvm/tools/llvm-reduce/llvm-reduce.cpp

diff --git a/llvm/test/tools/llvm-reduce/fail-file-open.test b/llvm/test/tools/llvm-reduce/fail-file-open.test
new file mode 100644 (file)
index 0000000..1136e1d
--- /dev/null
@@ -0,0 +1,5 @@
+# RUN: not llvm-reduce --test=echo %s.NotAFileInTestingDir 2>&1 | FileCheck %s
+
+This file will not be read. An invalid file path is fed to llvm-reduce.
+
+# CHECK: llvm-reduce: {{.*}}.NotAFileInTestingDir: error:
\ No newline at end of file
index 5a00ef0..614ca45 100644 (file)
@@ -116,6 +116,10 @@ int main(int Argc, char **Argv) {
   std::unique_ptr<Module> OriginalProgram =
       parseInputFile(InputFilename, Context);
 
+  if (!OriginalProgram) {
+    return 1;
+  }
+
   // Initialize test environment
   TestRunner Tester(TestFilename, TestArguments);
   Tester.setProgram(std::move(OriginalProgram));