Skip to content

Commit 9115ea4

Browse files
committed
libscript: Prevent null dereference on error when property has no getter
The `MCScriptGetTypeOfPropertyInModule()` function could deference a null pointer when attempting to determine the get type for the specified property. This function was only used in the `MCScriptThrowInvalidValueForPropertyError()` error dispatch helper, which in turn was only used from contexts where the VM had already resolved a specific type for the property and had _just_ failed to compare a value with it. This patch removes the `MCScriptGetTypeOfPropertyInModule()` function entirely, and refactors `MCScriptThrowInvalidValueForPropertyError()` to take the failed property type as an argument. By doing so it also corrects another bug: when the type required when setting a property is different from the type obtained by getting the property, the error message when failing to set the property would be misleading. Coverity-Id: 137924
1 parent ae3097b commit 9115ea4

File tree

4 files changed

+4
-37
lines changed

4 files changed

+4
-37
lines changed

libscript/src/script-error.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ MCScriptThrowPropertyUsedBeforeAssignedError(MCScriptInstanceRef p_instance,
5858
bool
5959
MCScriptThrowInvalidValueForPropertyError(MCScriptInstanceRef p_instance,
6060
MCScriptPropertyDefinition *p_property_def,
61+
MCTypeInfoRef p_property_type,
6162
MCValueRef p_provided_value)
6263
{
6364
return MCErrorCreateAndThrow(kMCScriptInvalidPropertyValueErrorTypeInfo,
@@ -67,8 +68,7 @@ MCScriptThrowInvalidValueForPropertyError(MCScriptInstanceRef p_instance,
6768
MCScriptGetNameOfDefinitionInModule(p_instance->module,
6869
p_property_def),
6970
"type",
70-
MCScriptGetTypeOfPropertyInModule(p_instance->module,
71-
p_property_def),
71+
p_property_type,
7272
"value",
7373
p_provided_value,
7474
nil);

libscript/src/script-instance.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ MCScriptSetPropertyInInstance(MCScriptInstanceRef self,
415415
{
416416
return MCScriptThrowInvalidValueForPropertyError(self,
417417
t_property_def,
418+
t_property_type,
418419
p_value);
419420
}
420421

libscript/src/script-module.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,40 +1096,6 @@ MCScriptGetNameOfDefinitionInModule(MCScriptModuleRef self,
10961096
return kMCEmptyName;
10971097
}
10981098

1099-
MCTypeInfoRef
1100-
MCScriptGetTypeOfPropertyInModule(MCScriptModuleRef self,
1101-
MCScriptPropertyDefinition *p_property_def)
1102-
{
1103-
MCScriptDefinition *t_getter =
1104-
p_property_def->getter != 0 ? self->definitions[p_property_def->getter - 1] : nil;
1105-
1106-
switch(t_getter->kind)
1107-
{
1108-
case kMCScriptDefinitionKindVariable:
1109-
{
1110-
return __MCScriptGetTypeWithIndexInModule(self,
1111-
static_cast<MCScriptVariableDefinition*>(t_getter)->type);
1112-
}
1113-
break;
1114-
1115-
case kMCScriptDefinitionKindHandler:
1116-
{
1117-
return MCScriptGetTypeOfReturnValueInModule(self,
1118-
static_cast<MCScriptCommonHandlerDefinition*>(t_getter));
1119-
}
1120-
break;
1121-
1122-
default:
1123-
{
1124-
/* LOAD CHECK */
1125-
__MCScriptUnreachable__("property getter is not a variable or handler");
1126-
}
1127-
break;
1128-
}
1129-
1130-
return kMCNullTypeInfo;
1131-
}
1132-
11331099
MCNameRef
11341100
MCScriptGetNameOfParameterInModule(MCScriptModuleRef self,
11351101
MCScriptCommonHandlerDefinition *p_handler_def,

libscript/src/script-private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ MCNameRef MCScriptGetNameOfLocalVariableInModule(MCScriptModuleRef module, MCScr
406406
MCNameRef MCScriptGetNameOfGlobalVariableInModule(MCScriptModuleRef module, MCScriptVariableDefinition *definition);
407407
MCNameRef MCScriptGetNameOfParameterInModule(MCScriptModuleRef module, MCScriptCommonHandlerDefinition *definition, uindex_t index);
408408

409-
MCTypeInfoRef MCScriptGetTypeOfPropertyInModule(MCScriptModuleRef module, MCScriptPropertyDefinition *definition);
410409
MCTypeInfoRef MCScriptGetTypeOfLocalVariableInModule(MCScriptModuleRef module, MCScriptHandlerDefinition *definition, uindex_t index);
411410
MCTypeInfoRef MCScriptGetTypeOfGlobalVariableInModule(MCScriptModuleRef module, MCScriptVariableDefinition *definition);
412411
MCTypeInfoRef MCScriptGetTypeOfParameterInModule(MCScriptModuleRef module, MCScriptCommonHandlerDefinition *definition, uindex_t index);
@@ -486,6 +485,7 @@ MCScriptThrowPropertyUsedBeforeAssignedError(MCScriptInstanceRef instance,
486485
bool
487486
MCScriptThrowInvalidValueForPropertyError(MCScriptInstanceRef instance,
488487
MCScriptPropertyDefinition *property_def,
488+
MCTypeInfoRef property_type,
489489
MCValueRef provided_value);
490490

491491
bool

0 commit comments

Comments
 (0)