[flang-commits] [flang] [flang] fix possible iterator underflow (PR #115754)

via flang-commits flang-commits at lists.llvm.org
Mon Nov 11 10:35:35 PST 2024


https://github.com/Originns created https://github.com/llvm/llvm-project/pull/115754

Replaced do-while with while loop in SemanticsContext::SearchScopeIndex(parser::CharBlock source) to prevent iterator underflow when upper_bound returns begin(). This ensures the iterator check happens before decrementing.

>From 4a841c6108e59366f0435e31ad08bd3f89accf90 Mon Sep 17 00:00:00 2001
From: Originns <68753984+Originns at users.noreply.github.com>
Date: Mon, 11 Nov 2024 19:34:21 +0100
Subject: [PATCH] [flang] fix possible iterator underflow

Replaced do-while with while loop in SemanticsContext::SearchScopeIndex(parser::CharBlock source) to prevent iterator underflow when upper_bound returns begin(). This ensures the iterator check happens before decrementing
---
 flang/lib/Semantics/semantics.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/flang/lib/Semantics/semantics.cpp b/flang/lib/Semantics/semantics.cpp
index 58dc1f218b56f4..dac781566f1a8e 100644
--- a/flang/lib/Semantics/semantics.cpp
+++ b/flang/lib/Semantics/semantics.cpp
@@ -415,12 +415,12 @@ auto SemanticsContext::SearchScopeIndex(parser::CharBlock source)
   if (!scopeIndex_.empty()) {
     auto iter{scopeIndex_.upper_bound(source)};
     auto begin{scopeIndex_.begin()};
-    do {
+    while (iter != begin) {
       --iter;
       if (iter->first.Contains(source)) {
         return iter;
       }
-    } while (iter != begin);
+    }
   }
   return scopeIndex_.end();
 }



More information about the flang-commits mailing list