commit 4f41999: [Minor] Lua_util: Fix Lua 5.2 compatibility

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Apr 4 09:56:03 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-04-04 10:50:57 +0100
URL: https://github.com/rspamd/rspamd/commit/4f419994e31feeb67e4949f08e0cf8a20062acd4 (HEAD -> master)

[Minor] Lua_util: Fix Lua 5.2 compatibility
Issue: #2831

---
 src/lua/lua_util.c | 40 ++++++++++++++--------------------------
 1 file changed, 14 insertions(+), 26 deletions(-)

diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c
index 1a5f11c4f..25fdc1514 100644
--- a/src/lua/lua_util.c
+++ b/src/lua/lua_util.c
@@ -3114,7 +3114,12 @@ typedef enum KOption {
 	Knop        /* no-op (configuration or spaces) */
 } KOption;
 
-#if LUA_VERSION_NUM < 503
+#if LUA_VERSION_NUM <= 502
+#define lua_Unsigned size_t
+#endif
+
+#if LUA_VERSION_NUM < 502
+
 #define lua_Unsigned size_t
 
 typedef struct luaL_Buffer_53 {
@@ -3128,6 +3133,7 @@ typedef struct luaL_Buffer_53 {
 #define luaL_Buffer luaL_Buffer_53
 #define COMPAT53_PREFIX lua
 #undef COMPAT53_API
+
 #if defined(__GNUC__) || defined(__clang__)
 # define COMPAT53_API __attribute__((__unused__)) static
 #else
@@ -3176,19 +3182,14 @@ COMPAT53_API void
 luaL_buffinit (lua_State *L, luaL_Buffer_53 *B)
 {
 	/* make it crash if used via pointer to a 5.1-style luaL_Buffer */
-#if LUA_VERSION_NUM < 502
 	B->b.p = NULL;
 	B->b.L = NULL;
 	B->b.lvl = 0;
 	/* reuse the buffer from the 5.1-style luaL_Buffer though! */
 	B->ptr = B->b.buffer;
 	B->nelems = 0;
-#elif LUA_VERSION_NUM == 502
-        B->ptr = B->b.b;
-	B->nelems = B->b.n;
-#endif
 	B->capacity = LUAL_BUFFERSIZE;
-        B->L2 = L;
+	B->L2 = L;
 }
 
 
@@ -3204,12 +3205,9 @@ luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s)
 			luaL_error (B->L2, "buffer too large");
 		newptr = (char *) lua_newuserdata (B->L2, newcap);
 		memcpy(newptr, B->ptr, B->nelems);
-#if LUA_VERSION_NUM < 502
-		if (B->ptr != B->b.buffer)
-#elif LUA_VERSION_NUM == 502
-		if (B->ptr != B->b.b)
-#endif
+		if (B->ptr != B->b.buffer) {
 			lua_replace (B->L2, -2); /* remove old buffer */
+		}
 		B->ptr = newptr;
 		B->capacity = newcap;
 	}
@@ -3232,18 +3230,11 @@ luaL_addvalue (luaL_Buffer_53 *B)
 	const char *s = lua_tolstring (B->L2, -1, &len);
 	if (!s)
 		luaL_error (B->L2, "cannot convert value to string");
-#if LUA_VERSION_NUM < 502
-	if (B->ptr != B->b.buffer)
-#elif LUA_VERSION_NUM == 502
-	if (B->ptr != B->b.b)
-#endif
+	if (B->ptr != B->b.buffer) {
 		lua_insert (B->L2, -2); /* userdata buffer must be at stack top */
+	}
 	luaL_addlstring (B, s, len);
-#if LUA_VERSION_NUM < 502
 	lua_remove (B->L2, B->ptr != B->b.buffer ? -2 : -1);
-#elif LUA_VERSION_NUM == 502
-	lua_remove (B->L2, B->ptr != B->b.b ? -2 : -1);
-#endif
 }
 
 
@@ -3251,12 +3242,9 @@ COMPAT53_API void
 luaL_pushresult (luaL_Buffer_53 *B)
 {
 	lua_pushlstring (B->L2, B->ptr, B->nelems);
-#if LUA_VERSION_NUM < 502
-	if (B->ptr != B->b.buffer)
-#elif LUA_VERSION_NUM == 502
-	if (B->ptr != B->b.b)
-#endif
+	if (B->ptr != B->b.buffer) {
 		lua_replace (B->L2, -2); /* remove userdata buffer */
+	}
 }
 
 #endif


More information about the Commits mailing list