Fix Miscellaneous Regressions and UBs (#1022)

This commit is contained in:
Headline 2019-05-28 18:04:08 -07:00 committed by GitHub
parent 2164f5191e
commit 28036966a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 72 deletions

View File

@ -227,11 +227,17 @@ private:
/* finally, allocate the new block */ /* finally, allocate the new block */
if (m_Data) if (m_Data)
{ {
m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize); cell_t *data = static_cast<cell_t*>(realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize));
if (!data) // allocation failure
{
return false;
}
m_Data = data;
} else { } else {
m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize); m_Data = static_cast<cell_t*>(malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize));
} }
return (m_Data != NULL); return (m_Data != nullptr);
} }
private: private:
cell_t *m_Data; cell_t *m_Data;

View File

@ -416,65 +416,20 @@ void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
m_ChildDeps.push_back(info); m_ChildDeps.push_back(info);
} }
// note: dependency iteration deprecated since 1.10
ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface) ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
{ {
List<IfaceInfo>::iterator iter = m_Deps.begin(); return nullptr;
if (iter == m_Deps.end())
{
return NULL;
}
if (pOwner)
{
*pOwner = (*iter).owner;
}
if (pInterface)
{
*pInterface = (*iter).iface;
}
List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
return (ITERATOR *)pIter;
} }
bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface) bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
{ {
List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter; return false;
List<IfaceInfo>::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;
} }
void CExtension::FreeDependencyIterator(ITERATOR *iter) void CExtension::FreeDependencyIterator(ITERATOR *iter)
{ {
List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
delete pIter;
} }
void CExtension::AddInterface(SMInterface *pInterface) void CExtension::AddInterface(SMInterface *pInterface)

View File

@ -162,18 +162,16 @@ bool ShareSystem::RequestInterface(const char *iface_name,
SMInterface **pIface) SMInterface **pIface)
{ {
/* See if the interface exists */ /* See if the interface exists */
List<IfaceInfo>::iterator iter;
SMInterface *iface; SMInterface *iface;
IExtension *iface_owner; IExtension *iface_owner = nullptr;
bool found = false; 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; iface = info.iface;
if (strcmp(iface->GetInterfaceName(), iface_name) == 0) if (strcmp(iface->GetInterfaceName(), iface_name) == 0)
{ {
if (iface->GetInterfaceVersion() == iface_vers if (iface->GetInterfaceVersion() == iface_vers || iface->IsVersionCompatible(iface_vers))
|| iface->IsVersionCompatible(iface_vers))
{ {
iface_owner = info.owner; iface_owner = info.owner;
found = true; found = true;

View File

@ -381,7 +381,11 @@ public:
{ {
if (len > max_size) if (len > max_size)
{ {
buffer = (char *)realloc(buffer, len); auto *newbuffer = static_cast<char *>(realloc(buffer, len));
if (!newbuffer)
return nullptr;
buffer = newbuffer;
max_size = len; max_size = len;
} }
return buffer; return buffer;
@ -420,7 +424,11 @@ cell_t InternalFormat(IPluginContext *pCtx, const cell_t *params, int start)
{ {
if (maxlen > sizeof(g_formatbuf)) 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 else
{ {

View File

@ -2595,7 +2595,7 @@ static cell_t GetEntityFlags(IPluginContext *pContext, const cell_t *params)
for (int32_t i = 0; i < 32; i++) for (int32_t i = 0; i < 32; i++)
{ {
int32_t flag = (1<<i); int32_t flag = (1U<<i);
if ((actual_flags & flag) == flag) if ((actual_flags & flag) == flag)
{ {
sm_flags |= SDKEntFlagToSMEntFlag(flag); sm_flags |= SDKEntFlagToSMEntFlag(flag);
@ -2641,7 +2641,7 @@ static cell_t SetEntityFlags(IPluginContext *pContext, const cell_t *params)
for (int32_t i = 0; i < 32; i++) for (int32_t i = 0; i < 32; i++)
{ {
int32_t flag = (1<<i); int32_t flag = (1U<<i);
if ((sm_flags & flag) == flag) if ((sm_flags & flag) == flag)
{ {
actual_flags |= SMEntFlagToSDKEntFlag(flag); actual_flags |= SMEntFlagToSDKEntFlag(flag);

View File

@ -84,28 +84,28 @@ namespace SourceMod
virtual IdentityToken_t *GetIdentity() =0; virtual IdentityToken_t *GetIdentity() =0;
/** /**
* @brief Retrieves the extension dependency list for this extension. * @brief Deprecated, do not use.
* *
* @param pOwner Optional pointer to store the first interface's owner. * @param pOwner Unused
* @param pInterface Optional pointer to store the first interface. * @param pInterface Unused
* @return An ITERATOR pointer for the results, or NULL if no results at all. * @return nullptr
*/ */
virtual ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface) =0; virtual ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface) =0;
/** /**
* @brief Finds the next dependency in the dependency list. * @brief Deprecated, do not use.
* *
* @param iter Pointer to iterator from FindFirstDependency. * @param iter Unused
* @param pOwner Optional pointer to store the interface's owner. * @param pOwner Unused
* @param pInterface Optional pointer to store the interface. * @param pInterface Unused
* @return True if there are more results after this, false otherwise. * @return false
*/ */
virtual bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface) =0; virtual bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface) =0;
/** /**
* @brief Frees an ITERATOR handle from FindFirstDependency. * @brief Deprecated, do not use.
* *
* @param iter Pointer to iterator to free. * @param iter Unused
*/ */
virtual void FreeDependencyIterator(ITERATOR *iter) =0; virtual void FreeDependencyIterator(ITERATOR *iter) =0;