datapack: free all elements on clear (#1251)

* Fixed memory leak

When a pack was cleared or destroyed the String and Raw types could cause memory leaks. This happens when "position" is sitting at the end of the vector and can never get past the "if (pos >= elements.length())" statement. This means there is a memory leak in any plugin that clears/destroys a pack with strings and doesn't set the position to length-1 or less beforehand.

* datapack: Fix delete op on CDataPackType::Raw.

Co-authored-by: Kyle Sanderson <kyle.leet@gmail.com>
This commit is contained in:
Andrew 2020-04-27 21:09:13 -04:00 committed by GitHub
parent d42c304a55
commit d044b13ce4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -65,12 +65,13 @@ CDataPack::Free(CDataPack *pack)
void CDataPack::Initialize()
{
position = 0;
do
{
} while (this->RemoveItem());
elements.clear();
position = 0;
}
void CDataPack::ResetSize()
@ -214,6 +215,7 @@ bool CDataPack::RemoveItem(size_t pos)
{
pos = position;
}
if (pos >= elements.length())
{
return false;
@ -228,7 +230,7 @@ bool CDataPack::RemoveItem(size_t pos)
{
case CDataPackType::Raw:
{
delete elements[pos].pData.vval;
delete [] elements[pos].pData.vval;
break;
}