]> granicus.if.org Git - clang/commitdiff
[Frontend] Return an error on bad inputs to PrecompiledPreabmle
authorIlya Biryukov <ibiryukov@google.com>
Wed, 22 May 2019 12:50:01 +0000 (12:50 +0000)
committerIlya Biryukov <ibiryukov@google.com>
Wed, 22 May 2019 12:50:01 +0000 (12:50 +0000)
Summary:
Instead of failing with assertions. Fixes a crash found by oss-fuzz:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=12865

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: cfe-commits

Tags: #clang

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@361376 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Frontend/PrecompiledPreamble.h
lib/Frontend/ASTUnit.cpp
lib/Frontend/PrecompiledPreamble.cpp

index b1d55d80635513ec5e1b4785be8c0d5a0935a5b9..1a8a64951ec496f0f7d2643820d6e59083809194 100644 (file)
@@ -291,7 +291,8 @@ enum class BuildPreambleError {
   CouldntCreateTempFile = 1,
   CouldntCreateTargetInfo,
   BeginSourceFileFailed,
-  CouldntEmitPCH
+  CouldntEmitPCH,
+  BadInputs
 };
 
 class BuildPreambleErrorCategory final : public std::error_category {
index 95d1bf27d7b22e668fe56b834f4013cb77c36d66..c4085b699e09de36fbf43ddb7310c401dca27c62 100644 (file)
@@ -1368,6 +1368,7 @@ ASTUnit::getMainBufferWithPrecompiledPreamble(
       case BuildPreambleError::CouldntCreateTargetInfo:
       case BuildPreambleError::BeginSourceFileFailed:
       case BuildPreambleError::CouldntEmitPCH:
+      case BuildPreambleError::BadInputs:
         // These erros are more likely to repeat, retry after some period.
         PreambleRebuildCountdown = DefaultPreambleRebuildInterval;
         return nullptr;
index 25f128fe11ae78a648967ac2a3d833f9710aed10..1fe8bfcb7459400eec097e6f01b1abef9a40efd7 100644 (file)
@@ -299,14 +299,13 @@ llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
   // created. This complexity should be lifted elsewhere.
   Clang->getTarget().adjust(Clang->getLangOpts());
 
-  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
-         "Invocation must have exactly one source file!");
-  assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
-             InputKind::Source &&
-         "FIXME: AST inputs not yet supported here!");
-  assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
-             InputKind::LLVM_IR &&
-         "IR inputs not support here!");
+  if (Clang->getFrontendOpts().Inputs.size() != 1 ||
+      Clang->getFrontendOpts().Inputs[0].getKind().getFormat() !=
+          InputKind::Source ||
+      Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() ==
+          InputKind::LLVM_IR) {
+    return BuildPreambleError::BadInputs;
+  }
 
   // Clear out old caches and data.
   Diagnostics.Reset();
@@ -784,6 +783,8 @@ std::string BuildPreambleErrorCategory::message(int condition) const {
     return "BeginSourceFile() return an error";
   case BuildPreambleError::CouldntEmitPCH:
     return "Could not emit PCH";
+  case BuildPreambleError::BadInputs:
+    return "Command line arguments must contain exactly one source file";
   }
   llvm_unreachable("unexpected BuildPreambleError");
 }