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 <[email protected]>
This commit is contained in:
Andrew
2020-04-27 18:09:13 -07:00
committed by GitHub
co-authored by Kyle Sanderson
parent d42c304a55
commit d044b13ce4
+4 -2
View File
@@ -65,12 +65,13 @@ CDataPack::Free(CDataPack *pack)
void CDataPack::Initialize() void CDataPack::Initialize()
{ {
position = 0;
do do
{ {
} while (this->RemoveItem()); } while (this->RemoveItem());
elements.clear(); elements.clear();
position = 0;
} }
void CDataPack::ResetSize() void CDataPack::ResetSize()
@@ -214,6 +215,7 @@ bool CDataPack::RemoveItem(size_t pos)
{ {
pos = position; pos = position;
} }
if (pos >= elements.length()) if (pos >= elements.length())
{ {
return false; return false;
@@ -228,7 +230,7 @@ bool CDataPack::RemoveItem(size_t pos)
{ {
case CDataPackType::Raw: case CDataPackType::Raw:
{ {
delete elements[pos].pData.vval; delete [] elements[pos].pData.vval;
break; break;
} }