From a5b9e50f682383c6db068aa4067e3ac773aa7921 Mon Sep 17 00:00:00 2001 From: Cliff Burdick <30670611+cliffburdick@users.noreply.github.com> Date: Mon, 27 May 2024 22:49:19 -0700 Subject: [PATCH] fix: added check on iterator end position (#5129) * Added check on iterator end position * Always use assert without conditional check * Addressing code review comments * style: pre-commit fixes * Remove assert and throw * Changed style slightly --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/pybind11/detail/class.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index 0b9ea42d..73f30477 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -388,7 +388,11 @@ inline void clear_patients(PyObject *self) { auto *instance = reinterpret_cast(self); auto &internals = get_internals(); auto pos = internals.patients.find(self); - assert(pos != internals.patients.end()); + + if (pos == internals.patients.end()) { + pybind11_fail("FATAL: Internal consistency check failed: Invalid clear_patients() call."); + } + // Clearing the patients can cause more Python code to run, which // can invalidate the iterator. Extract the vector of patients // from the unordered_map first.