From 232feec0b1e9c115bbb30da2293dc5bed3fd97b7 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Thu, 4 Sep 2025 00:17:12 -0400 Subject: [PATCH] cmCMakePath: support constructing `.end()` iterators Dereferencing the iterator must be avoided once it becomes the `.end()` value. Check for iterator validity before updating the `PathElement` member. --- Source/cmCMakePath.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Source/cmCMakePath.h b/Source/cmCMakePath.h index da92424d18..e681c81a16 100644 --- a/Source/cmCMakePath.h +++ b/Source/cmCMakePath.h @@ -664,12 +664,7 @@ public: iterator() = default; - iterator(iterator const& other) - : Iterator(other.Iterator) - , Path(other.Path) - , PathElement(*this->Iterator) - { - } + iterator(iterator const& other) = default; ~iterator() = default; @@ -678,7 +673,7 @@ public: if (this != &other) { this->Iterator = other.Iterator; this->Path = other.Path; - this->PathElement = *this->Iterator; + this->PathElement = other.PathElement; } return *this; @@ -691,7 +686,9 @@ public: iterator& operator++() { ++this->Iterator; - this->PathElement = *this->Iterator; + if (this->Path && this->Iterator != this->Path->Path.end()) { + this->PathElement = *this->Iterator; + } return *this; } @@ -706,7 +703,9 @@ public: iterator& operator--() { --this->Iterator; - this->PathElement = *this->Iterator; + if (this->Path && this->Iterator != this->Path->Path.end()) { + this->PathElement = *this->Iterator; + } return *this; } @@ -725,8 +724,10 @@ private: iterator(cmCMakePath const* path, cm::filesystem::path::iterator const& it) : Iterator(it) , Path(path) - , PathElement(*this->Iterator) { + if (this->Path && this->Iterator != this->Path->Path.end()) { + this->PathElement = *this->Iterator; + } } cm::filesystem::path::iterator Iterator;