From b2bf127b542325e59229f98b518b4be8d49573e7 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 20 Jan 2020 17:14:13 +0000 Subject: [PATCH] [Minor] Lua_logger: Fix printing of sparse tables --- src/lua/lua_logger.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/lua/lua_logger.c b/src/lua/lua_logger.c index f76db8877..7902babb8 100644 --- a/src/lua/lua_logger.c +++ b/src/lua/lua_logger.c @@ -463,7 +463,7 @@ lua_logger_out_table (lua_State *L, gint pos, gchar *outbuf, gsize len, gsize remain = len, r; gboolean first = TRUE; gconstpointer self = NULL; - gint i, tpos; + gint i, tpos, last_seq = -1; if (!lua_istable (L, pos) || remain == 0) { return 0; @@ -498,6 +498,8 @@ lua_logger_out_table (lua_State *L, gint pos, gchar *outbuf, gsize len, break; } + last_seq = i; + if (!first) { r = rspamd_snprintf (d, remain + 1, ", "); MOVE_BUF(d, remain, r); @@ -524,7 +526,17 @@ lua_logger_out_table (lua_State *L, gint pos, gchar *outbuf, gsize len, /* 'key' is at index -2 and 'value' is at index -1 */ if (lua_type (L, -2) == LUA_TNUMBER) { - continue; + if (last_seq > 0) { + lua_pushvalue (L, -2); + + if (lua_tonumber (L, -1) <= last_seq + 1) { + lua_pop (L, 1); + /* Already seen */ + continue; + } + + lua_pop (L, 1); + } } if (!first) { @@ -532,8 +544,11 @@ lua_logger_out_table (lua_State *L, gint pos, gchar *outbuf, gsize len, MOVE_BUF(d, remain, r); } + /* Preserve key */ + lua_pushvalue (L, -2); r = rspamd_snprintf (d, remain + 1, "[%s] = ", - lua_tostring (L, -2)); + lua_tostring (L, -1)); + lua_pop (L, 1); /* Remove key */ MOVE_BUF(d, remain, r); tpos = lua_gettop (L); -- 2.39.5