From 1844472502fa8e56d90e861d45f85d8fd8957b48 Mon Sep 17 00:00:00 2001 From: Philipp Janda Date: Sun, 21 Nov 2021 14:43:00 +0100 Subject: Convert lua_pushlstring from a macro to a function This should avoid issues with multiple evaluations of the arguments, which should be very rare, but could in principle happen. --- c-api/compat-5.3.c | 8 ++++++++ c-api/compat-5.3.h | 9 +++------ 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'c-api') diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c index 42b0a4b..64592d6 100644 --- a/c-api/compat-5.3.c +++ b/c-api/compat-5.3.c @@ -715,6 +715,14 @@ void luaL_pushresult (luaL_Buffer_53 *B) { #if defined( LUA_VERSION_NUM ) && LUA_VERSION_NUM <= 502 +COMPAT53_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { +#undef lua_pushlstring + lua_pushlstring(L, len > 0 ? s : "", len); +#define lua_pushlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _pushlstring_53) + return lua_tostring(L, -1); +} + + COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i) { index = lua_absindex(L, index); lua_pushinteger(L, i); diff --git a/c-api/compat-5.3.h b/c-api/compat-5.3.h index b730a4b..6f66dad 100644 --- a/c-api/compat-5.3.h +++ b/c-api/compat-5.3.h @@ -142,9 +142,6 @@ COMPAT53_API void lua_len (lua_State *L, int i); #define lua_pushstring(L, s) \ (lua_pushstring((L), (s)), lua_tostring((L), -1)) -#define lua_pushlstring(L, s, len) \ - ((((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))), lua_tostring((L), -1)) - #ifndef luaL_newlibtable # define luaL_newlibtable(L, l) \ (lua_createtable((L), 0, sizeof((l))/sizeof(*(l))-1)) @@ -290,6 +287,9 @@ typedef int (*lua_KFunction)(lua_State *L, int status, lua_KContext ctx); #define lua_gettable(L, i) \ (lua_gettable((L), (i)), lua_type((L), -1)) +#define lua_pushlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _pushlstring_53) +COMPAT53_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len); + #define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti) COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i); @@ -352,9 +352,6 @@ COMPAT53_API void luaL_requiref (lua_State *L, const char *modname, #define lua_getuservalue(L, i) \ (lua_getuservalue((L), (i)), lua_type((L), -1)) -#define lua_pushlstring(L, s, len) \ - (((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))) - #define lua_rawgetp(L, i, p) \ (lua_rawgetp((L), (i), (p)), lua_type((L), -1)) -- cgit v1.2.3-55-g6feb