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>
This commit is contained in:
Cliff Burdick 2024-05-27 22:49:19 -07:00 committed by GitHub
parent ce08e37042
commit a5b9e50f68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -388,7 +388,11 @@ inline void clear_patients(PyObject *self) {
auto *instance = reinterpret_cast<detail::instance *>(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.