From 28036966a6ba0b514985276f70d07c4429ad024b Mon Sep 17 00:00:00 2001 From: Headline Date: Tue, 28 May 2019 18:04:08 -0700 Subject: [PATCH] Fix Miscellaneous Regressions and UBs (#1022) --- core/logic/CellArray.h | 12 ++++++--- core/logic/ExtensionSys.cpp | 51 +++---------------------------------- core/logic/ShareSys.cpp | 10 +++----- core/logic/stringutil.cpp | 12 +++++++-- core/smn_entities.cpp | 4 +-- public/IExtensionSys.h | 22 ++++++++-------- 6 files changed, 39 insertions(+), 72 deletions(-) diff --git a/core/logic/CellArray.h b/core/logic/CellArray.h index c77bdc94..9e6795da 100644 --- a/core/logic/CellArray.h +++ b/core/logic/CellArray.h @@ -227,11 +227,17 @@ private: /* finally, allocate the new block */ if (m_Data) { - m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize); + cell_t *data = static_cast(realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize)); + if (!data) // allocation failure + { + return false; + } + + m_Data = data; } else { - m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize); + m_Data = static_cast(malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize)); } - return (m_Data != NULL); + return (m_Data != nullptr); } private: cell_t *m_Data; diff --git a/core/logic/ExtensionSys.cpp b/core/logic/ExtensionSys.cpp index 8ee392bd..4409a94a 100644 --- a/core/logic/ExtensionSys.cpp +++ b/core/logic/ExtensionSys.cpp @@ -416,65 +416,20 @@ void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface) m_ChildDeps.push_back(info); } +// note: dependency iteration deprecated since 1.10 ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface) { - List::iterator iter = m_Deps.begin(); - - if (iter == m_Deps.end()) - { - return NULL; - } - - if (pOwner) - { - *pOwner = (*iter).owner; - } - if (pInterface) - { - *pInterface = (*iter).iface; - } - - List::iterator *pIter = new List::iterator(iter); - - return (ITERATOR *)pIter; + return nullptr; } bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface) { - List::iterator *pIter = (List::iterator *)iter; - List::iterator _iter; - - if (_iter == m_Deps.end()) - { - return false; - } - - _iter++; - - if (pOwner) - { - *pOwner = (*_iter).owner; - } - if (pInterface) - { - *pInterface = (*_iter).iface; - } - - *pIter = _iter; - - if (_iter == m_Deps.end()) - { - return false; - } - - return true; + return false; } void CExtension::FreeDependencyIterator(ITERATOR *iter) { - List::iterator *pIter = (List::iterator *)iter; - delete pIter; } void CExtension::AddInterface(SMInterface *pInterface) diff --git a/core/logic/ShareSys.cpp b/core/logic/ShareSys.cpp index b172c8b5..3b4d4589 100644 --- a/core/logic/ShareSys.cpp +++ b/core/logic/ShareSys.cpp @@ -162,18 +162,16 @@ bool ShareSystem::RequestInterface(const char *iface_name, SMInterface **pIface) { /* See if the interface exists */ - List::iterator iter; SMInterface *iface; - IExtension *iface_owner; + IExtension *iface_owner = nullptr; bool found = false; - for (iter=m_Interfaces.begin(); iter!=m_Interfaces.end(); iter++) + for (auto iter = m_Interfaces.begin(); iter!=m_Interfaces.end(); iter++) { - IfaceInfo &info = (*iter); + IfaceInfo &info = *iter; iface = info.iface; if (strcmp(iface->GetInterfaceName(), iface_name) == 0) { - if (iface->GetInterfaceVersion() == iface_vers - || iface->IsVersionCompatible(iface_vers)) + if (iface->GetInterfaceVersion() == iface_vers || iface->IsVersionCompatible(iface_vers)) { iface_owner = info.owner; found = true; diff --git a/core/logic/stringutil.cpp b/core/logic/stringutil.cpp index d5be7697..d79e7771 100644 --- a/core/logic/stringutil.cpp +++ b/core/logic/stringutil.cpp @@ -381,7 +381,11 @@ public: { if (len > max_size) { - buffer = (char *)realloc(buffer, len); + auto *newbuffer = static_cast(realloc(buffer, len)); + if (!newbuffer) + return nullptr; + + buffer = newbuffer; max_size = len; } return buffer; @@ -420,7 +424,11 @@ cell_t InternalFormat(IPluginContext *pCtx, const cell_t *params, int start) { if (maxlen > sizeof(g_formatbuf)) { - __copy_buf = g_extrabuf.GetWithSize(maxlen); + char *tmpbuff = g_extrabuf.GetWithSize(maxlen); + if (!tmpbuff) + return pCtx->ThrowNativeError("Unable to allocate buffer with a size of \"%u\"", maxlen); + + __copy_buf = tmpbuff; } else { diff --git a/core/smn_entities.cpp b/core/smn_entities.cpp index 61da09d3..e9fc68c7 100644 --- a/core/smn_entities.cpp +++ b/core/smn_entities.cpp @@ -2595,7 +2595,7 @@ static cell_t GetEntityFlags(IPluginContext *pContext, const cell_t *params) for (int32_t i = 0; i < 32; i++) { - int32_t flag = (1<