From bee21af2a16707183752cbda5aa0c29ed80e7ddc Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Thu, 13 Mar 2025 12:55:45 +0000 Subject: [PATCH] libutil: Fix Pos::getSourcePath Previous implementation didn't actually check if std::get_if returned a nullptr: std::optional getSourcePath() const { return *std::get_if(&origin); } (cherry picked from commit 50123f2a566bd9157ef6ed64d95799473e5d8670) --- src/libutil/position.cc | 7 +++++++ src/libutil/position.hh | 4 +--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libutil/position.cc b/src/libutil/position.cc index 946f167b6..275985c8c 100644 --- a/src/libutil/position.cc +++ b/src/libutil/position.cc @@ -66,6 +66,13 @@ std::optional Pos::getSource() const }, origin); } +std::optional Pos::getSourcePath() const +{ + if (auto * path = std::get_if(&origin)) + return *path; + return std::nullopt; +} + void Pos::print(std::ostream & out, bool showOrigin) const { if (showOrigin) { diff --git a/src/libutil/position.hh b/src/libutil/position.hh index 2ac68d15a..07e261c4c 100644 --- a/src/libutil/position.hh +++ b/src/libutil/position.hh @@ -70,9 +70,7 @@ struct Pos /** * Get the SourcePath, if the source was loaded from a file. */ - std::optional getSourcePath() const { - return *std::get_if(&origin); - } + std::optional getSourcePath() const; struct LinesIterator { using difference_type = size_t;