From a07ebc58919fcbd8b98b159399b33fce2df9f65f Mon Sep 17 00:00:00 2001
From: Federico Montesino Pouzols <federico.montesino-pouzols@stfc.ac.uk>
Date: Wed, 14 Oct 2015 08:27:13 +0100
Subject: [PATCH] check Increment and next() retvals, 1292909, 1295474, re
 #13951

---
 .../src/MDHistoWorkspaceIterator.cpp          | 20 +++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/Framework/DataObjects/src/MDHistoWorkspaceIterator.cpp b/Framework/DataObjects/src/MDHistoWorkspaceIterator.cpp
index 9965a7d0507..87ad5e25ccd 100644
--- a/Framework/DataObjects/src/MDHistoWorkspaceIterator.cpp
+++ b/Framework/DataObjects/src/MDHistoWorkspaceIterator.cpp
@@ -199,8 +199,17 @@ void MDHistoWorkspaceIterator::init(
     for (size_t d = 0; d < m_nd; d++)
       m_center[d] = m_origin[d] + 0.5f * m_binWidth[d];
     // Skip on if the first point is NOT contained
-    if (!m_function->isPointContained(m_center))
-      next();
+    if (!m_function->isPointContained(m_center)) {
+      bool didNext = next();
+      if (!didNext && this->valid()) {
+        throw std::runtime_error(
+            "Inconsistency found initializing "
+            "MDHistoWorkspace iterator: this iterator should be valid, but "
+            "when tried to skip the "
+            "first point (not contained) could not iterate to "
+            "next point.");
+      }
+    }
   }
 
   // --- Calculate index permutations for neighbour finding face touching ---
@@ -293,9 +302,11 @@ bool MDHistoWorkspaceIterator::valid() const { return (m_pos < m_max); }
 /// @return true if you can continue iterating
 bool MDHistoWorkspaceIterator::next() {
   if (m_function) {
+    bool allIncremented = false;
     do {
       m_pos++;
-      Utils::NestedForLoop::Increment(m_nd, m_index, m_indexMax);
+      allIncremented =
+          Utils::NestedForLoop::Increment(m_nd, m_index, m_indexMax);
       // Calculate the center
       for (size_t d = 0; d < m_nd; d++) {
         m_center[d] =
@@ -304,7 +315,8 @@ bool MDHistoWorkspaceIterator::next() {
       }
       //        std::cout<<std::endl;
       // Keep incrementing until you are in the implicit function
-    } while (!m_function->isPointContained(m_center) && m_pos < m_max);
+    } while (!allIncremented && !m_function->isPointContained(m_center) &&
+             m_pos < m_max);
   } else {
     ++m_pos;
   }
-- 
GitLab