From 9527cc3fa081ded4280765b6d805bf1bcc6591b9 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Wed, 23 Nov 2022 14:48:12 -0500 Subject: [PATCH] avoid clearChildAttachments iterator invalidation (#12987) --- src/server/unit_sao.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/server/unit_sao.cpp b/src/server/unit_sao.cpp index 9a49b0f43..6fdefaad4 100644 --- a/src/server/unit_sao.cpp +++ b/src/server/unit_sao.cpp @@ -179,12 +179,16 @@ void UnitSAO::getAttachment(int *parent_id, std::string *bone, v3f *position, void UnitSAO::clearChildAttachments() { - for (int child_id : m_attachment_child_ids) { + // Cannot use for-loop here: setAttachment() modifies 'm_attachment_child_ids'! + while (!m_attachment_child_ids.empty()) { + int child_id = *m_attachment_child_ids.begin(); + // Child can be NULL if it was deleted earlier if (ServerActiveObject *child = m_env->getActiveObject(child_id)) child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0), false); + + removeAttachmentChild(child_id); } - m_attachment_child_ids.clear(); } void UnitSAO::clearParentAttachment()