Skip to content

Commit

Permalink
Fix (potential?) memory leaks from getVariant() (SquirrelVM)
Browse files Browse the repository at this point in the history
Free values before assignment.
  • Loading branch information
z33ky committed Nov 12, 2024
1 parent 82f664a commit 11167f6
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sp/src/vscript/vscript_squirrel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ bool getVariant(HSQUIRRELVM vm, SQInteger idx, ScriptVariant_t& variant)
{
case OT_NULL:
{
variant.Free();
variant.m_flags = 0;
// TODO: Should this be (HSCRIPT)nullptr
variant.m_type = FIELD_VOID;
Expand All @@ -1217,6 +1218,7 @@ bool getVariant(HSQUIRRELVM vm, SQInteger idx, ScriptVariant_t& variant)
{
return false;
}
variant.Free();
variant = (int)val;
return true;
}
Expand All @@ -1227,6 +1229,7 @@ bool getVariant(HSQUIRRELVM vm, SQInteger idx, ScriptVariant_t& variant)
{
return false;
}
variant.Free();
variant = (float)val;
return true;
}
Expand All @@ -1237,6 +1240,7 @@ bool getVariant(HSQUIRRELVM vm, SQInteger idx, ScriptVariant_t& variant)
{
return false;
}
variant.Free();
variant = val ? true : false;
return true;
}
Expand All @@ -1248,6 +1252,7 @@ bool getVariant(HSQUIRRELVM vm, SQInteger idx, ScriptVariant_t& variant)
{
return false;
}
variant.Free();
char* buffer = (char*)malloc(size + 1);
V_memcpy(buffer, val, size);
buffer[size] = 0;
Expand All @@ -1263,6 +1268,7 @@ bool getVariant(HSQUIRRELVM vm, SQInteger idx, ScriptVariant_t& variant)
tag == TYPETAG_VECTOR &&
SQ_SUCCEEDED(sq_getinstanceup(vm, idx, (SQUserPointer*)&v, TYPETAG_VECTOR)))
{
variant.Free();
variant = (Vector*)malloc(sizeof(Vector));
new ((Vector*)variant.m_pVector) Vector(*v);
variant.m_flags |= SV_FREE;
Expand All @@ -1272,6 +1278,7 @@ bool getVariant(HSQUIRRELVM vm, SQInteger idx, ScriptVariant_t& variant)
}
default:
{
variant.Free();
HSQOBJECT* obj = new HSQOBJECT;
sq_resetobject(obj);
sq_getstackobj(vm, idx, obj);
Expand Down

0 comments on commit 11167f6

Please sign in to comment.