From 842a83f09caa2ebd4bc03e0076420148ac07c808 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 24 Nov 2023 16:08:55 -0300 Subject: Panic functions should not raise errors The standard panic function was using 'lua_tostring', which may raise a memory-allocation error if error value is a number. --- lauxlib.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'lauxlib.c') diff --git a/lauxlib.c b/lauxlib.c index 4ca6c654..1c9082e6 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1025,9 +1025,14 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { } +/* +** Standard panic funcion just prints an error message. The test +** with 'lua_type' avoids possible memory errors in 'lua_tostring'. +*/ static int panic (lua_State *L) { - const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "error object is not a string"; + const char *msg = (lua_type(L, -1) == LUA_TSTRING) + ? lua_tostring(L, -1) + : "error object is not a string"; lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", msg); return 0; /* return to Lua to abort */ -- cgit v1.2.3-55-g6feb