From f5955b8d3e46fef5065a395ce3f7dade267fba1f Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 15 Mar 2016 15:06:07 +0000 Subject: [PATCH] [Fix] Restore described behaviour for some lua functions --- src/lua/lua_html.c | 10 ++++++++-- src/lua/lua_task.c | 20 +++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/lua/lua_html.c b/src/lua/lua_html.c index 57a46dfb2..7c5ca3cb9 100644 --- a/src/lua/lua_html.c +++ b/src/lua/lua_html.c @@ -249,13 +249,16 @@ lua_html_get_images (lua_State *L) if (hc != NULL) { lua_newtable (L); - if (hc->images) { + if (hc->images && hc->images->len > 0) { for (i = 0; i < hc->images->len; i ++) { img = g_ptr_array_index (hc->images, i); lua_html_push_image (L, img); lua_rawseti (L, -2, i + 1); } } + else { + lua_pushnil (L); + } } else { lua_pushnil (L); @@ -275,7 +278,7 @@ lua_html_get_blocks (lua_State *L) if (hc != NULL) { lua_newtable (L); - if (hc->blocks) { + if (hc->blocks && hc->blocks->len > 0) { for (i = 0; i < hc->blocks->len; i ++) { bl = g_ptr_array_index (hc->blocks, i); @@ -322,6 +325,9 @@ lua_html_get_blocks (lua_State *L) lua_rawseti (L, -2, i + 1); } } + else { + lua_pushnil (L); + } } else { lua_pushnil (L); diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index e5214a321..fa60184ee 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -1843,14 +1843,20 @@ lua_task_get_images (lua_State *L) if (task) { cur = task->images; - lua_newtable (L); + if (cur) { + lua_newtable (L); - while (cur) { - pimg = lua_newuserdata (L, sizeof (struct rspamd_image *)); - rspamd_lua_setclass (L, "rspamd{image}", -1); - *pimg = cur->data; - lua_rawseti (L, -2, i++); - cur = g_list_next (cur); + while (cur) { + pimg = lua_newuserdata (L, sizeof (struct rspamd_image *)); + rspamd_lua_setclass (L, "rspamd{image}", -1); + *pimg = cur->data; + lua_rawseti (L, -2, i++); + cur = g_list_next (cur); + } + } + else { + /* Return nil if there are no images to preserve compatibility */ + lua_pushnil (L); } } else { -- 2.39.5