From 57e06de95d9c881e79be312d5a309f57de10b27f Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 6 Nov 2025 18:44:26 +0100 Subject: [PATCH] lib_concat: fix zero lenght string case continue the loop, not return, it might just be an empty line, so returning might break some luas 0 lenght would make realloc allocate nothing at all, so just skip it --- src/lua_baselib.c | 77 ++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 779873617..f0bfaa535 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -63,27 +63,33 @@ boolean luaL_checkboolean(lua_State *L, int narg) { // String concatination static int lib_concat(lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int i; - char *r = NULL; - size_t rl = 0,sl; - lua_getglobal(L, "tostring"); - for (i=1; i<=n; i++) { - const char *s; - lua_pushvalue(L, -1); /* function to be called */ - lua_pushvalue(L, i); /* value to print */ - lua_call(L, 1, 1); - s = lua_tolstring(L, -1, &sl); /* get result */ - if (s == NULL) - return luaL_error(L, LUA_QL("tostring") " must return a string to " - LUA_QL("__add")); + int n = lua_gettop(L); /* number of arguments */ + int i; + char *r = NULL; + size_t rl = 0, sl; + lua_getglobal(L, "tostring"); + for (i=1; i<=n; i++) { + const char *s; + lua_pushvalue(L, -1); /* function to be called */ + lua_pushvalue(L, i); /* value to print */ + lua_call(L, 1, 1); + s = lua_tolstring(L, -1, &sl); /* get result */ + + if (sl == 0) { + lua_pop(L, 1); /* pop result */ + continue; + } + + if (s == NULL) + return luaL_error(L, LUA_QL("tostring") " must return a string to " + LUA_QL("__add")); r = Z_Realloc(r, rl+sl, PU_STATIC, NULL); memcpy(r+rl, s, sl); rl += sl; - lua_pop(L, 1); /* pop result */ - } - lua_pushlstring(L, r, rl); - Z_Free(r); + lua_pop(L, 1); /* pop result */ + } + lua_pushlstring(L, r, rl); + Z_Free(r); return 1; } @@ -91,23 +97,24 @@ static int lib_concat(lua_State *L) // Copied from base Lua code static int lib_print(lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int i; - //HUDSAFE - lua_getglobal(L, "tostring"); - for (i=1; i<=n; i++) { - const char *s; - lua_pushvalue(L, -1); /* function to be called */ - lua_pushvalue(L, i); /* value to print */ - lua_call(L, 1, 1); - s = lua_tostring(L, -1); /* get result */ - if (s == NULL) - return luaL_error(L, LUA_QL("tostring") " must return a string to " - LUA_QL("print")); - if (i>1) CONS_Printf("\n"); - CONS_Printf("%s", s); - lua_pop(L, 1); /* pop result */ - } + int n = lua_gettop(L); /* number of arguments */ + int i; + //HUDSAFE + lua_getglobal(L, "tostring"); + for (i=1; i<=n; i++) { + const char *s; + lua_pushvalue(L, -1); /* function to be called */ + lua_pushvalue(L, i); /* value to print */ + lua_call(L, 1, 1); + s = lua_tostring(L, -1); /* get result */ + + if (s == NULL) + return luaL_error(L, LUA_QL("tostring") " must return a string to " + LUA_QL("print")); + if (i>1) CONS_Printf("\n"); + CONS_Printf("%s", s); + lua_pop(L, 1); /* pop result */ + } CONS_Printf("\n"); return 0; }