BUG/MINOR: hlua: Fix Channel:data() and Channel:line() to respect documentation
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 12 May 2025 14:12:13 +0000 (16:12 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Tue, 20 May 2025 16:11:47 +0000 (18:11 +0200)
When the channel API was revisted, the both functions above was added. An
offset can be passed as argument. However, this parameter could be reported
to be out of range if there was not enough input data was received yet. It
is an issue, especially with a tcp rule, because more data could be
received. If an error is reported too early, this prevent the rule to be
reevaluated later. In fact, an error should only be reported if the offset
is part of the output data.

Another issue is about the conditions to report 'nil' instead of an empty
string. 'nil' was reported when no data was found. But it is not aligned
with the documentation. 'nil' must only be returned if no more data cannot
be received and there is no input data at all.

This patch should fix the issue #2716. It should be backported as far as 2.6.

(cherry picked from commit a5de0e15959a241afc9afb39f1b02a2517894f7b)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit ab8b159b39983e969510bc4b5902984d80f18f62)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>

src/hlua.c

index 0671632..25306d6 100644 (file)
@@ -3870,7 +3870,7 @@ __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KCon
        struct channel *chn;
        struct filter *filter;
        size_t input, output;
-       int offset, len;
+       int offset, len = 0;
 
        chn = MAY_LJMP(hlua_checkchannel(L, 1));
 
@@ -3887,11 +3887,14 @@ __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KCon
                if (offset < 0)
                        offset = MAX(0, (int)input + offset);
                offset += output;
-               if (offset < output || offset > input + output) {
+               if (offset < output) {
                        lua_pushfstring(L, "offset out of range.");
                        WILL_LJMP(lua_error(L));
                }
        }
+       if (offset >= output+input)
+               goto wait_more_data;
+
        len = output + input - offset;
        if (lua_gettop(L) == 3) {
                len = MAY_LJMP(luaL_checkinteger(L, 3));
@@ -3909,17 +3912,23 @@ __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KCon
         * is no data or not enough data was received.
         */
        if (!len || offset + len > output + input) {
+         wait_more_data:
                if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
                        /* Yield waiting for more data, as requested */
                        MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0));
                }
 
-               /* Return 'nil' if there is no data and the channel can't receive more data */
-               if (!len) {
+               if (!input) {
+                       /* Return 'nil' if there is no data and the channel can't receive more data */
                        lua_pushnil(L);
                        return -1;
                }
 
+               if (!len) {
+                       lua_pushstring(L, "");
+                       return 1;
+               }
+
                /* Otherwise, return all data */
                len = output + input - offset;
        }
@@ -3945,7 +3954,7 @@ __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KCon
        struct channel *chn;
        struct filter *filter;
        size_t l, input, output;
-       int offset, len;
+       int offset, len = 0;
 
        chn = MAY_LJMP(hlua_checkchannel(L, 1));
        output = co_data(chn);
@@ -3961,11 +3970,13 @@ __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KCon
                if (offset < 0)
                        offset = MAX(0, (int)input + offset);
                offset += output;
-               if (offset < output || offset > input + output) {
+               if (offset < output) {
                        lua_pushfstring(L, "offset out of range.");
                        WILL_LJMP(lua_error(L));
                }
        }
+       if (offset > output + input)
+               goto wait_more_data;
 
        len = output + input - offset;
        if (lua_gettop(L) == 3) {
@@ -3993,17 +4004,23 @@ __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KCon
         * specified or not enough data was received.
         */
        if (lua_gettop(L) != 3 ||  offset + len > output + input) {
+         wait_more_data:
                if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
                        /* Yield waiting for more data */
                        MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0));
                }
 
-               /* Return 'nil' if there is no data and the channel can't receive more data */
-               if (!len) {
+               if (!input) {
+                       /* Return 'nil' if there is no data and the channel can't receive more data */
                        lua_pushnil(L);
                        return -1;
                }
 
+               if (!len) {
+                       lua_pushstring(L, "");
+                       return 1;
+               }
+
                /* Otherwise, return all data */
                len = output + input - offset;
        }