Skip to content

Commit bbdf354

Browse files
committed
[[ LCB ]] Improve MCScriptCreateModuleFromStream errors.
MCScriptCreateModuleFromStream now throws more specific (generic) errors when a problem occurs.
1 parent 0ae0416 commit bbdf354

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

libscript/src/script-module.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,18 @@ bool MCScriptCreateModuleFromStream(MCStreamRef stream, MCScriptModuleRef& r_mod
425425
if (!MCStreamRead(stream, t_header, 4))
426426
return false;
427427

428+
// If this fails, then it definitely isn't a LiveCode module.
428429
if (t_header[0] != 'L' ||
429-
t_header[1] != 'C' ||
430-
t_header[2] != (kMCScriptCurrentModuleVersion & 0xFF) ||
430+
t_header[1] != 'C')
431+
return MCErrorThrowGeneric(MCSTR("not a module"));
432+
433+
// If this fails, then it is a potentially a LiveCode module, but in a format
434+
// we do not support.
435+
if (t_header[2] != (kMCScriptCurrentModuleVersion & 0xFF) ||
431436
t_header[3] != ((kMCScriptCurrentModuleVersion >> 8) & 0xFF))
432-
return false;
437+
return MCErrorThrowGeneric(MCSTR("module format not supported"));
433438

439+
// If this fails then we've run out of memory (oh well - not much to be done!).
434440
MCScriptModule *t_module;
435441
if (!MCScriptCreateObject(kMCScriptObjectKindModule, sizeof(MCScriptModule), (MCScriptObject*&)t_module))
436442
return false;
@@ -439,6 +445,12 @@ bool MCScriptCreateModuleFromStream(MCStreamRef stream, MCScriptModuleRef& r_mod
439445
if (!MCPickleRead(stream, kMCScriptModulePickleInfo, t_module))
440446
{
441447
MCScriptDestroyObject(t_module);
448+
449+
// If there is a pending error then its out of memory, otherwise report
450+
// that we haven't been able to unpickle.
451+
if (!MCErrorIsPending())
452+
return MCErrorThrowGeneric(MCSTR("error reading module"));
453+
442454
return false;
443455
}
444456

@@ -447,7 +459,7 @@ bool MCScriptCreateModuleFromStream(MCStreamRef stream, MCScriptModuleRef& r_mod
447459
if (MCNameIsEqualTo(t_other_module -> name, t_module -> name))
448460
{
449461
MCScriptDestroyObject(t_module);
450-
return false;
462+
return MCErrorThrowGeneric(MCSTR("module with that name already loaded"));
451463
}
452464

453465
// Link our module into the global module list.

0 commit comments

Comments
 (0)