[gamecode] Fail gracefully on invalid entity strings

Returning null (and printing some error messages) is far friendlier than
aborting.
This commit is contained in:
Bill Currie 2022-05-05 15:36:22 +09:00
parent 4a4e16399a
commit a31ff3153c
1 changed files with 22 additions and 8 deletions

View File

@ -299,17 +299,22 @@ ED_ConvertToPlist (script_t *script, int nohack, struct hashlink_s **hashlinks)
plitem_t *value; plitem_t *value;
char *token; char *token;
int anglehack; int anglehack;
const char *msg = "";
while (Script_GetToken (script, 1)) { while (Script_GetToken (script, 1)) {
token = script->token->str; token = script->token->str;
if (!strequal (token, "{")) if (!strequal (token, "{")) {
Sys_Error ("ED_ConvertToPlist: EOF without closing brace"); msg = "EOF without closing brace";
goto parse_error;
}
ent = PL_NewDictionary (hashlinks); ent = PL_NewDictionary (hashlinks);
while (1) { while (1) {
int n; int n;
if (!Script_GetToken (script, 1)) if (!Script_GetToken (script, 1)) {
Sys_Error ("ED_ConvertToPlist: EOF without closing brace"); msg = "EOF without closing brace";
goto parse_error;
}
token = script->token->str; token = script->token->str;
if (strequal (token, "}")) if (strequal (token, "}"))
break; break;
@ -327,12 +332,16 @@ ED_ConvertToPlist (script_t *script, int nohack, struct hashlink_s **hashlinks)
} else { } else {
key = PL_NewString (token); key = PL_NewString (token);
} }
if (!Script_TokenAvailable (script, 0)) if (!Script_TokenAvailable (script, 0)) {
Sys_Error ("ED_ConvertToPlist: EOL without value"); msg = "EOL without value";
goto parse_error;
}
Script_GetToken (script, 0); Script_GetToken (script, 0);
token = script->token->str; token = script->token->str;
if (strequal (token, "}")) if (strequal (token, "}")) {
Sys_Error ("ED_ConvertToPlist: closing brace without data"); msg = "closing brace without data";
goto parse_error;
}
if (anglehack) { if (anglehack) {
dsprintf (dstr, "0 %s 0", token); dsprintf (dstr, "0 %s 0", token);
value = PL_NewString (dstr->str); value = PL_NewString (dstr->str);
@ -346,6 +355,11 @@ ED_ConvertToPlist (script_t *script, int nohack, struct hashlink_s **hashlinks)
} }
dstring_delete (dstr); dstring_delete (dstr);
return plist; return plist;
parse_error:
Sys_Printf ("%s:%d: %s", script->file, script->line, msg);
dstring_delete (dstr);
PL_Free (plist);
return 0;
} }