From fe82e7d09cbc3464b85acee79f3c5e741f65d423 Mon Sep 17 00:00:00 2001 From: MCJack123 Date: Sun, 31 Oct 2021 16:32:14 -0400 Subject: [PATCH] Fixed string.format with substrings --- src/lapi.c | 9 +++++---- src/lstrlib.c | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/lapi.c b/src/lapi.c index cb46379..51badd6 100644 --- a/src/lapi.c +++ b/src/lapi.c @@ -350,12 +350,13 @@ LUA_API int lua_toboolean (lua_State *L, int idx) { LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { StkId o = index2adr(L, idx); - if (ttissubstr(o) && len != NULL) { - /* if using lua_tostring, we need to convert to normal string so the cstring ends with \0 */ - /* otherwise, we can keep the substring intact */ + /*if (ttissubstr(o) && len != NULL) { + / * ~~if using lua_tostring, we need to convert to normal string so the cstring ends with \0 * / + / * otherwise, we can keep the substring intact~~ * / + / * EDIT: this breaks Lua spec - we're always doing this * / *len = ssvalue(o)->len; return getstr(ssvalue(o)->str) + ssvalue(o)->offset; - } + }*/ if (!ttisstring(o)) { lua_lock(L); /* `luaV_tostring' may create a new string */ if (!luaV_tostring(L, o)) { /* conversion failed? */ diff --git a/src/lstrlib.c b/src/lstrlib.c index fa6fad8..e20d2e3 100644 --- a/src/lstrlib.c +++ b/src/lstrlib.c @@ -850,7 +850,7 @@ static int str_format (lua_State *L) { continue; /* skip the `addsize' at the end */ } else { - sprintf(buff, form, s); + snprintf(buff, MAX_ITEM, form, s); lua_pop(L, 1); break; }