commit f9ca74c: [Minor] Improve handling of redis.null

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Aug 27 21:49:12 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-08-27 16:03:56 +0100
URL: https://github.com/rspamd/rspamd/commit/f9ca74c445e4f40e8abbae6ecbfbb6e6181b61ed

[Minor] Improve handling of redis.null

---
 src/lua/lua_redis.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/lua/lua_redis.c b/src/lua/lua_redis.c
index ab76c3114..9b26f5c7b 100644
--- a/src/lua/lua_redis.c
+++ b/src/lua/lua_redis.c
@@ -23,6 +23,7 @@
 #define REDIS_DEFAULT_TIMEOUT 1.0
 
 static const gchar *M = "rspamd lua redis";
+static void *redis_null;
 
 /***
  * @module rspamd_redis
@@ -344,8 +345,7 @@ lua_redis_push_reply (lua_State *L, const redisReply *r, gboolean text_data)
 		lua_pushinteger (L, r->integer);
 		break;
 	case REDIS_REPLY_NIL:
-		/* XXX: not the best approach */
-		lua_newuserdata (L, sizeof (gpointer));
+		lua_getfield (L, LUA_REGISTRYINDEX, "redis.null");
 		break;
 	case REDIS_REPLY_STRING:
 	case REDIS_REPLY_STATUS:
@@ -1665,6 +1665,18 @@ lua_load_redis (lua_State * L)
 
 	return 1;
 }
+
+static void
+lua_redis_null_mt (lua_State *L)
+{
+	luaL_newmetatable (L, "redis{null}");
+
+	lua_pushinteger (L, 0);
+	lua_setfield (L, -2, "cookie");
+
+	lua_pop (L, 1);
+}
+
 /**
  * Open redis library
  * @param L lua stack
@@ -1676,4 +1688,11 @@ luaopen_redis (lua_State * L)
 	rspamd_lua_new_class (L, "rspamd{redis}", redislib_m);
 	lua_pop (L, 1);
 	rspamd_lua_add_preload (L, "rspamd_redis", lua_load_redis);
+
+	/* Set null element */
+	lua_redis_null_mt (L);
+	redis_null = lua_newuserdata (L, 0);
+	luaL_getmetatable (L, "redis{null}");
+	lua_setmetatable (L, -2);
+	lua_setfield (L, LUA_REGISTRYINDEX, "redis.null");
 }


More information about the Commits mailing list