From 364e569945c044fd18c70ee1bc851364534aef97 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 Jun 2020 16:12:01 -0300 Subject: Avoid calling 'fprintf' with NULL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Avoid undefined behavior in calls like «fprintf("%s", NULL)». ('lua_writestringerror' is implemented as 'fprintf', and 'lua_tostring' can return NULL if object is not a string.) --- lauxlib.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lauxlib.c') diff --git a/lauxlib.c b/lauxlib.c index e6d74168..e3d9be37 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -995,8 +995,10 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { static int panic (lua_State *L) { + const char *msg = lua_tostring(L, -1); + if (msg == NULL) msg = "error object is not a string"; lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)); + msg); return 0; /* return to Lua to abort */ } -- cgit v1.2.3-55-g6feb