From d7ea6068e5ad0a75dfd8e6eb09d8f0d26df755fc Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Fri, 18 Feb 2022 11:15:08 -0800 Subject: [PATCH] [flang] Avoid crash case in provenance mapping When a contiguous range of a cooked character stream is being mapped to a range of source provenance, the code was assuming that the "end()" position of the input range -- being the character immediately after the range -- would also follow the range's source provenance. This isn't always the case. Modify the code to work with the true last character of the input range (at end()-1) and to also cope with cases when that last position truly maps to an earlier provenance, which can happen when the prescanner has inserted a space into the cooked character stream. Differential Revision: https://reviews.llvm.org/D121124 --- flang/lib/Parser/provenance.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/flang/lib/Parser/provenance.cpp b/flang/lib/Parser/provenance.cpp index 27cc1ebf84f8..e2364d3f8a56 100644 --- a/flang/lib/Parser/provenance.cpp +++ b/flang/lib/Parser/provenance.cpp @@ -435,11 +435,15 @@ std::optional CookedSource::GetProvenanceRange( return std::nullopt; } ProvenanceRange first{provenanceMap_.Map(cookedRange.begin() - &data_[0])}; - if (cookedRange.size() <= first.size()) { + if (cookedRange.size() <= first.size()) { // always true when empty return first.Prefix(cookedRange.size()); } - ProvenanceRange last{provenanceMap_.Map(cookedRange.end() - &data_[0])}; - return {ProvenanceRange{first.start(), last.start() - first.start()}}; + ProvenanceRange last{provenanceMap_.Map(cookedRange.end() - 1 - &data_[0])}; + if (first.start() <= last.start()) { + return {ProvenanceRange{first.start(), last.start() - first.start() + 1}}; + } else { + return std::nullopt; + } } std::optional CookedSource::GetCharBlock(