Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed variable's object value being deleted before marking the variable as uninitialised #304

Merged
merged 1 commit into from Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions source/MdFunc.cpp
Expand Up @@ -458,8 +458,7 @@ bool MdFunc::Call(ResultToken &aResultToken, ExprTokenType *aParam[], int aParam
{
// Although 0 or "" is a fairly conventional default, it might not be safe.
// For error-detection and to avoid unexpected behaviour, "unset" the var.
var->Assign();
var->MarkUninitialized();
var->Uninitialize();
}
}

Expand Down
6 changes: 4 additions & 2 deletions source/script_object.cpp
Expand Up @@ -2089,9 +2089,11 @@ ResultType Array::GetEnumItem(UINT &aIndex, Var *aVal, Var *aReserved, int aVarC
switch (item.symbol)
{
default:
aVal->AssignString(item.string, item.string.Length());
if (item.symbol == SYM_MISSING)
aVal->MarkUninitialized();
aVal->Uninitialize();
else
aVal->AssignString(item.string, item.string.Length());

break;
case SYM_INTEGER: aVal->Assign(item.n_int64); break;
case SYM_FLOAT: aVal->Assign(item.n_double); break;
Expand Down
8 changes: 2 additions & 6 deletions source/var.cpp
Expand Up @@ -43,8 +43,7 @@ ResultType Var::Assign(Var &aVar)

if (source_var.mAttrib & VAR_ATTRIB_UNINITIALIZED)
{
target_var.Assign();
target_var.MarkUninitialized();
target_var.Uninitialize();
return OK;
}

Expand Down Expand Up @@ -84,10 +83,7 @@ ResultType Var::Assign(ExprTokenType &aToken)
default:
ASSERT(!"Unhandled symbol");
case SYM_MISSING:
if (!Assign())
return FAIL;
MarkUninitialized();
return OK;
return Uninitialize() ? OK : FAIL;
}
// Since above didn't return, it can only be SYM_STRING.
return Assign(aToken.marker, aToken.marker_length);
Expand Down
16 changes: 15 additions & 1 deletion source/var.h
Expand Up @@ -926,9 +926,23 @@ class Var

void MarkUninitialized()
{
Var &var = *ResolveAlias();
Var& var = *ResolveAlias();
var.mAttrib |= VAR_ATTRIB_UNINITIALIZED;
}
ResultType Uninitialize()
{
Var& var = *ResolveAlias();
auto obj = (var.mAttrib & VAR_ATTRIB_IS_OBJECT) ? mObject : nullptr;
if (obj)
obj -> AddRef();
auto result = var.Assign();
if (result)
var.MarkUninitialized();
if (obj)
obj->Release();

return result;
}

}; // class Var
#pragma pack(pop) // Calling pack with no arguments restores the default value (which is 8, but "the alignment of a member will be on a boundary that is either a multiple of n or a multiple of the size of the member, whichever is smaller.")
Expand Down