From bc439e21cd262af69ae5988b3c0067e87d9b9230 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 Aug 2009 13:23:19 -0300 Subject: avoid an unprotected call to 'lua_tostring' which theoretically may cause a panicked exit --- lua.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lua.c b/lua.c index 1885364f..f52ded8e 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.173 2009/06/18 18:59:58 roberto Exp roberto $ +** $Id: lua.c,v 1.174 2009/07/15 17:35:20 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -75,6 +75,18 @@ static int report (lua_State *L, int status) { } +/* the next function is called unprotected, so it must avoid errors */ +static void finalreport (lua_State *L, int status) { + if (status != LUA_OK) { + const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1) + : NULL; + if (msg == NULL) msg = "(error object is not a string)"; + l_message(progname, msg); + lua_pop(L, 1); + } +} + + static int traceback (lua_State *L) { const char *msg = lua_tostring(L, 1); if (msg) @@ -383,7 +395,7 @@ int main (int argc, char **argv) { s.argc = argc; s.argv = argv; status = lua_cpcall(L, &pmain, &s); - report(L, status); + finalreport(L, status); lua_close(L); return (s.ok && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; } -- cgit v1.2.3-55-g6feb