From 94adc98ef5c8a6f45506fea934f0d33220639053 Mon Sep 17 00:00:00 2001 From: SCP-076 <84674379+SCP-076@users.noreply.github.com> Date: Sun, 26 Apr 2026 01:11:28 +0800 Subject: [PATCH] Fix out-of-bounds crash in PluginIterator_Next (#2445) * Fix OOB dereference in PluginIterator methodmap (#1880) This prevents the native from returning true when the iterator has already reached the end() of the list, which previously led to a crash when accessing the Plugin property. * Add defense-in-depth safeguards to CMMPluginIterator * Update PluginIterator.Next() @error documentation Reflect the new NativeError thrown when calling Next() on an exhausted iterator. --- core/logic/smn_core.cpp | 16 ++++++++++------ plugins/include/sourcemod.inc | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/core/logic/smn_core.cpp b/core/logic/smn_core.cpp index 6db6e766..6c68a118 100644 --- a/core/logic/smn_core.cpp +++ b/core/logic/smn_core.cpp @@ -151,6 +151,8 @@ public: } virtual IPlugin *GetPlugin() override { + if (m_current == m_list.end()) + return nullptr; return *m_current; } virtual void NextPlugin() override @@ -161,7 +163,8 @@ public: return; } - m_current++; + if (m_current != m_list.end()) + m_current++; } virtual void Release() override { @@ -171,7 +174,7 @@ public: public: virtual void OnPluginDestroyed(IPlugin *plugin) override { - if (*m_current == plugin) + if (m_current != m_list.end() && *m_current == plugin) m_current = m_list.erase(m_current); else m_list.remove(static_cast(plugin)); @@ -401,12 +404,13 @@ static cell_t PluginIterator_Next(IPluginContext *pContext, const cell_t *params { return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err); } - + if(!pIter->MorePlugins()) - return 0; - + return pContext->ThrowNativeError("PluginIterator %x is exhausted.", hndl); + pIter->NextPlugin(); - return 1; + + return pIter->MorePlugins() ? 1 : 0; } static cell_t PluginIterator_Plugin_get(IPluginContext *pContext, const cell_t *params) diff --git a/plugins/include/sourcemod.inc b/plugins/include/sourcemod.inc index 8e748c85..d8c93313 100644 --- a/plugins/include/sourcemod.inc +++ b/plugins/include/sourcemod.inc @@ -262,7 +262,7 @@ methodmap PluginIterator < Handle // Advances the iterator. Returns whether there are more plugins available in the iterator. // // @return True on more plugins, false otherwise. - // @error Invalid Handle. + // @error Invalid Handle, or called when the iterator is already exhausted. public native bool Next(); // Returns the current plugin in the iterator.