Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 623d05f

Browse files
committed
[[ Bug 19672 ]] Add string params to error throws
`MCErrorThrowGeneric(nil)`, if called, will cause an assertion failure in debug mode (`MCAssertIsString`) and a crash in Release mode where the string is dereferenced. This patch adds suitable error strings.
1 parent 91428ec commit 623d05f

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

docs/notes/bugfix-19672.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Prevent crash on throwing certain errors

libfoundation/src/foundation-record.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ static bool __check_conformance(MCTypeInfoRef p_typeinfo, const MCValueRef *p_va
3131
}
3232

3333
if (x_offset + p_typeinfo -> record . field_count > p_value_count)
34-
return MCErrorThrowGeneric(nil);
34+
return MCErrorThrowGeneric(MCSTR("record does not conform to target type: not enough fields"));
3535

3636
for(uindex_t i = 0; i < p_typeinfo -> record . field_count; i++)
3737
if (MCTypeInfoConforms(MCValueGetTypeInfo(p_values[x_offset + i]), p_typeinfo -> record . fields[i] . type))
38-
return MCErrorThrowGeneric(nil);
38+
return MCErrorThrowGenericWithMessage(MCSTR("record field %{field} does not conform to target type %{type}"),
39+
"field",
40+
p_values[x_offset + i],
41+
"type",
42+
p_typeinfo->record.fields[i].type,
43+
nullptr);
3944

4045
return true;
4146
}

libfoundation/src/foundation-typeinfo.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,10 @@ bool MCNamedTypeInfoBind(MCTypeInfoRef self, MCTypeInfoRef p_target)
461461
MCAssert(MCTypeInfoIsNamed(self));
462462
__MCAssertIsTypeInfo(p_target);
463463
if (self -> named . typeinfo != nil)
464-
return MCErrorThrowGeneric(nil);
464+
return MCErrorThrowGenericWithMessage(MCSTR("Can't bind typeinfo %{name}: already bound to %{self}"),
465+
"name", p_target->named.name,
466+
"self", self->named.name,
467+
nullptr);
465468

466469
self -> named . typeinfo = MCValueRetain(p_target);
467470

@@ -474,7 +477,7 @@ bool MCNamedTypeInfoUnbind(MCTypeInfoRef self)
474477
MCAssert(MCTypeInfoIsNamed(self));
475478

476479
if (self -> named . typeinfo == nil)
477-
return MCErrorThrowGeneric(nil);
480+
return MCErrorThrowGeneric(MCSTR("Can't unbind typeinfo: not bound"));
478481

479482
MCValueRelease(self -> named . typeinfo);
480483
self -> named . typeinfo = nil;
@@ -488,7 +491,7 @@ bool MCNamedTypeInfoResolve(MCTypeInfoRef self, MCTypeInfoRef& r_bound_type)
488491
MCAssert(MCTypeInfoIsNamed(self));
489492

490493
if (self -> named . typeinfo == nil)
491-
return MCErrorThrowGeneric(nil);
494+
return MCErrorThrowGeneric(MCSTR("Can't resolve typeinfo: not bound"));
492495

493496
r_bound_type = self -> named . typeinfo;
494497

libscript/src/script-instance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ static bool MCScriptPrepareForeignFunction(MCScriptInstanceRef p_instance, MCScr
14041404
t_signature = p_instance -> module -> types[p_handler -> type] -> typeinfo;
14051405

14061406
if (!MCHandlerTypeInfoGetLayoutType(t_signature, t_abi, p_handler -> function_cif))
1407-
return MCErrorThrowGeneric(nil);
1407+
return MCErrorThrowGeneric(MCSTR("Failed to fetch handler layout type"));
14081408

14091409
if (!p_throw)
14101410
r_bound = true;

0 commit comments

Comments
 (0)