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
This commit is contained in:
Alug 2025-11-06 18:44:26 +01:00 committed by NepDisk
parent d0f0744dcf
commit 57e06de95d

View file

@ -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;
}