BUG/MINOR: hlua: Don't rely on top of the stack when using Lua buffers
When the lua buffers are used, a variable number of stack slots may be
used. Thus we cannot assume that we know where the top of the stack is. It
was not an issue for lua < 5.4.3 (at least for small buffers). But
'socket:receive()' now fails with lua 5.4.3 because a light userdata is
systematically pushed on the top of the stack when a buffer is initialized.
To fix the bug, in hlua_socket_receive(), we save the index of the top of
the stack before creating the buffer. This way, we can check the number of
arguments, regardless anything was pushed on the stack or not.
Note that the other buffer usages seem to be safe.
This patch should solve the issue #1240. It should be backport to all stable
branches.
(cherry picked from commit c31b2008728babbc8738b88b593f2be40324369b)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit af3d8c10b75df33e341d355644a66bf125876fc6)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit efdb2620e8e895193a2095906e536e4fec99870f)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/src/hlua.c b/src/hlua.c
index 8f88f6f..ac7708e 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -1966,7 +1966,7 @@
{
int wanted = HLSR_READ_LINE;
const char *pattern;
- int type;
+ int lastarg, type;
char *error;
size_t len;
struct hlua_socket *socket;
@@ -2011,11 +2011,16 @@
if (lua_gettop(L) != 2)
lua_replace(L, 2);
+ /* Save index of the top of the stack because since buffers are used, it
+ * may change
+ */
+ lastarg = lua_gettop(L);
+
/* init buffer, and fill it with prefix. */
luaL_buffinit(L, &socket->b);
/* Check prefix. */
- if (lua_gettop(L) >= 3) {
+ if (lastarg >= 3) {
if (lua_type(L, 3) != LUA_TSTRING)
WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
pattern = lua_tolstring(L, 3, &len);