aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2018-03-22 15:04:29 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2018-03-22 15:32:33 +0000
commitc9e0716e3e736c22a966a954ba2429eebda96a0a (patch)
treeb34616095342c1be48c240b2b503b2386bd91ded /src/lua
parent5fada4c4ad1f6b87a356d829677c73f61c18e7dc (diff)
downloadrspamd-c9e0716e3e736c22a966a954ba2429eebda96a0a.tar.gz
rspamd-c9e0716e3e736c22a966a954ba2429eebda96a0a.zip
[Fix] Try to fix various Lua stack issues
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/lua_common.c4
-rw-r--r--src/lua/lua_config.c5
-rw-r--r--src/lua/lua_logger.c15
-rw-r--r--src/lua/lua_tcp.c23
4 files changed, 30 insertions, 17 deletions
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c
index f484c02b9..a1d3f27bc 100644
--- a/src/lua/lua_common.c
+++ b/src/lua/lua_common.c
@@ -1297,12 +1297,12 @@ rspamd_lua_run_postloads (lua_State *L, struct rspamd_config *cfg,
tb = lua_touserdata (L, -1);
msg_err_config ("error executing post load code: %v", tb);
g_string_free (tb, TRUE);
- lua_pop (L, 2);
+ lua_settop (L, err_idx - 1);
return FALSE;
}
- lua_pop (L, 1); /* Error function */
+ lua_settop (L, err_idx - 1);
}
return TRUE;
diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c
index 6b1c9f490..9a2cab536 100644
--- a/src/lua/lua_config.c
+++ b/src/lua/lua_config.c
@@ -1041,7 +1041,6 @@ lua_watcher_callback (gpointer session_data, gpointer ud)
if (tb) {
g_string_free (tb, TRUE);
- lua_pop (L, 1);
}
}
else {
@@ -1118,12 +1117,10 @@ lua_watcher_callback (gpointer session_data, gpointer ud)
}
}
}
-
- lua_pop (L, nresults);
}
}
- lua_pop (L, 1); /* Error function */
+ lua_settop (L, err_idx - 1);
}
static void
diff --git a/src/lua/lua_logger.c b/src/lua/lua_logger.c
index 65461a655..2378ea59b 100644
--- a/src/lua/lua_logger.c
+++ b/src/lua/lua_logger.c
@@ -317,9 +317,11 @@ lua_logger_out_boolean (lua_State *L, gint pos, gchar *outbuf, gsize len)
static gsize
lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len)
{
- gint r;
+ gint r, top;
const gchar *str = NULL;
+ top = lua_gettop (L);
+
if (!lua_getmetatable (L, pos)) {
return 0;
}
@@ -328,7 +330,8 @@ lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len)
lua_gettable (L, -2);
if (!lua_istable (L, -1)) {
- lua_pop (L, 2);
+ lua_settop (L, top);
+
return 0;
}
@@ -339,7 +342,8 @@ lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len)
lua_pushvalue (L, pos);
if (lua_pcall (L, 1, 1, 0) != 0) {
- lua_pop (L, 3);
+ lua_settop (L, top);
+
return 0;
}
@@ -350,7 +354,8 @@ lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len)
lua_gettable (L, -2);
if (!lua_isstring (L, -1)) {
- lua_pop (L, 3);
+ lua_settop (L, top);
+
return 0;
}
@@ -358,7 +363,7 @@ lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len)
}
r = rspamd_snprintf (outbuf, len + 1, "%s(%p)", str, lua_touserdata (L, pos));
- lua_pop (L, 3);
+ lua_settop (L, top);
return r;
}
diff --git a/src/lua/lua_tcp.c b/src/lua/lua_tcp.c
index dcaa3bc57..3bfadf8cb 100644
--- a/src/lua/lua_tcp.c
+++ b/src/lua/lua_tcp.c
@@ -323,6 +323,12 @@ lua_tcp_maybe_free (struct lua_tcp_cbdata *cbd)
}
}
+#ifdef __GNUC__
+static void
+lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal,
+ const char *err, ...) __attribute__ ((format(printf, 3, 4)));
+#endif
+
static void
lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal,
const char *err, ...)
@@ -330,7 +336,7 @@ lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal,
va_list ap, ap_copy;
struct lua_tcp_cbdata **pcbd;
struct lua_tcp_handler *hdl;
- gint cbref;
+ gint cbref, top;
va_start (ap, err);
@@ -338,7 +344,6 @@ lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal,
hdl = g_queue_peek_head (cbd->handlers);
if (hdl == NULL) {
- va_end (ap_copy);
break;
}
@@ -350,6 +355,7 @@ lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal,
}
if (cbref != -1) {
+ top = lua_gettop (cbd->L);
lua_rawgeti (cbd->L, LUA_REGISTRYINDEX, cbref);
/* Error message */
@@ -367,9 +373,10 @@ lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal,
if (lua_pcall (cbd->L, 3, 0, 0) != 0) {
msg_info ("callback call failed: %s", lua_tostring (cbd->L, -1));
- lua_pop (cbd->L, 1);
}
+ lua_settop (cbd->L, top);
+
REF_RELEASE (cbd);
}
@@ -391,7 +398,7 @@ lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len)
struct rspamd_lua_text *t;
struct lua_tcp_cbdata **pcbd;
struct lua_tcp_handler *hdl;
- gint cbref, arg_cnt;
+ gint cbref, arg_cnt, top;
hdl = g_queue_peek_head (cbd->handlers);
@@ -405,6 +412,7 @@ lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len)
}
if (cbref != -1) {
+ top = lua_gettop (cbd->L);
lua_rawgeti (cbd->L, LUA_REGISTRYINDEX, cbref);
/* Error */
lua_pushnil (cbd->L);
@@ -430,9 +438,9 @@ lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len)
if (lua_pcall (cbd->L, arg_cnt, 0, 0) != 0) {
msg_info ("callback call failed: %s", lua_tostring (cbd->L, -1));
- lua_pop (cbd->L, 1);
}
+ lua_settop (cbd->L, top);
REF_RELEASE (cbd);
}
}
@@ -686,7 +694,9 @@ lua_tcp_handler (int fd, short what, gpointer ud)
if (cbd->connect_cb != -1) {
struct lua_tcp_cbdata **pcbd;
+ gint top;
+ top = lua_gettop (cbd->L);
lua_rawgeti (cbd->L, LUA_REGISTRYINDEX, cbd->connect_cb);
pcbd = lua_newuserdata (cbd->L, sizeof (*pcbd));
*pcbd = cbd;
@@ -695,9 +705,10 @@ lua_tcp_handler (int fd, short what, gpointer ud)
if (lua_pcall (cbd->L, 1, 0, 0) != 0) {
msg_info ("callback call failed: %s", lua_tostring (cbd->L, -1));
- lua_pop (cbd->L, 1);
}
+ lua_settop (cbd->L, top);
+
REF_RELEASE (cbd);
}
}