From 64ea224aceb6cd4b1b83f7fc1cad23ecce2c5cf5 Mon Sep 17 00:00:00 2001 From: peter klausler Date: Tue, 3 Dec 2019 09:49:44 -0800 Subject: [PATCH] [flang] Fix bug flang-compiler/f18#843, bad recovery from failed opens of source files Original-commit: flang-compiler/f18@27d9db610fc7302b8da33e19a70c441a0ead6c7c Reviewed-on: https://github.com/flang-compiler/f18/pull/850 --- flang/lib/parser/parsing.cc | 1 + flang/lib/parser/provenance.cc | 7 +++++-- flang/lib/semantics/mod-file.cc | 10 +++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/flang/lib/parser/parsing.cc b/flang/lib/parser/parsing.cc index c93e2d3e7e65..30b405580596 100644 --- a/flang/lib/parser/parsing.cc +++ b/flang/lib/parser/parsing.cc @@ -49,6 +49,7 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) { messages_.Say(range, "%s"_err_en_US, fileError.str()); return sourceFile; } + CHECK(sourceFile); if (sourceFile->bytes() == 0) { ProvenanceRange range{allSources.AddCompilerInsertion(path)}; messages_.Say(range, "file is empty"_err_en_US); diff --git a/flang/lib/parser/provenance.cc b/flang/lib/parser/provenance.cc index d769dac846b0..279e6fa399a9 100644 --- a/flang/lib/parser/provenance.cc +++ b/flang/lib/parser/provenance.cc @@ -174,8 +174,11 @@ std::string AllSources::PopSearchPathDirectory() { const SourceFile *AllSources::Open(std::string path, std::stringstream *error) { std::unique_ptr source{std::make_unique(encoding_)}; - source->Open(LocateSourceFile(path, searchPath_), error); - return ownedSourceFiles_.emplace_back(std::move(source)).get(); + if (source->Open(LocateSourceFile(path, searchPath_), error)) { + return ownedSourceFiles_.emplace_back(std::move(source)).get(); + } else { + return nullptr; + } } const SourceFile *AllSources::ReadStandardInput(std::stringstream *error) { diff --git a/flang/lib/semantics/mod-file.cc b/flang/lib/semantics/mod-file.cc index 4b2721546102..2d557e14e939 100644 --- a/flang/lib/semantics/mod-file.cc +++ b/flang/lib/semantics/mod-file.cc @@ -764,16 +764,16 @@ Scope *ModFileReader::Read(const SourceName &name, Scope *ancestor) { options.searchDirectories = context_.searchDirectories(); auto path{ModFileName(name, ancestorName, context_.moduleFileSuffix())}; const auto *sourceFile{parsing.Prescan(path, options)}; - if (!sourceFile) { - return nullptr; - } else if (parsing.messages().AnyFatalError()) { + if (parsing.messages().AnyFatalError()) { for (auto &msg : parsing.messages().messages()) { std::string str{msg.ToString()}; Say(name, ancestorName, parser::MessageFixedText{str.c_str(), str.size()}, - sourceFile->path()); + path); } return nullptr; - } else if (!VerifyHeader(sourceFile->content(), sourceFile->bytes())) { + } + CHECK(sourceFile); + if (!VerifyHeader(sourceFile->content(), sourceFile->bytes())) { Say(name, ancestorName, "File has invalid checksum: %s"_en_US, sourceFile->path()); return nullptr;