Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[sb2] Catch lua errors. Contributes to JB#52528
Instead of using `lua_call` use `lua_pcall` to exit gracefully in case we
hit any lua errors.
  • Loading branch information
Thaodan committed Feb 11, 2021
1 parent 652a506 commit 9cce77a
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions scratchbox2/sb2d/sb2d.c
Expand Up @@ -66,21 +66,39 @@ static void load_and_execute_lua_file(const char *filename)

switch(luaL_loadfile(sb2d_lua, filename)) {
case LUA_ERRFILE:
fprintf(stderr, "Error loading %s\n", filename);
exit(1);
fprintf(stderr, "Error loading %s\n", filename);
exit(EXIT_FAILURE);
case LUA_ERRSYNTAX:
errmsg = lua_tostring(sb2d_lua, -1);
errmsg = lua_tostring(sb2d_lua, -1);
fprintf(stderr, "Syntax error in %s (%s)\n", filename,
(errmsg?errmsg:""));
exit(1);
exit(EXIT_FAILURE);
case LUA_ERRMEM:
fprintf(stderr, "Memory allocation error while "
"loading %s\n", filename);
exit(1);
default:
;
"loading %s\n", filename);
exit(EXIT_FAILURE);
case LUA_OK:
break;
default:
fprintf(stderr, "Unknown LUA error "
"loading %s\n", filename);
exit(EXIT_FAILURE);
}

switch(lua_pcall(sb2d_lua, 0, 0, 0)) {
case LUA_ERRRUN:
fprintf(stderr, "Runtime-Error loading %s (%s)\n",
filename, lua_tostring(sb2d_lua, -1));
exit(EXIT_FAILURE);
case LUA_ERRMEM:
fprintf(stderr, "Memory allocation error while "
"loading %s (%s) \n",
filename, lua_tostring(sb2d_lua, -1));
exit(EXIT_FAILURE);
case LUA_OK:
return;
}
lua_call(sb2d_lua, 0, 0);
exit(1);
}

/* Lua calls this at panic: */
Expand Down

0 comments on commit 9cce77a

Please sign in to comment.