]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: hlua: Fix Channel.line and Channel.data behavior regarding the doc
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 10 Jan 2023 14:29:54 +0000 (15:29 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 11 Jan 2023 09:31:28 +0000 (10:31 +0100)
These both functions are buggy and don't respect the documentation. They
must wait for more data, if possible.

For Channel.data(), it must happen if not enough data was received orf if no
length was specified and no data was received. The first case is properly
handled but not the second one. An empty string is return instead. In
addition, if there is no data and the channel can't receive more data, 'nil'
value must be returned.

In the same spirit, for Channel.line(), we must try to wait for more data
when no line is found if not enough data was received or if no length was
specified. Here again, only the first case is properly handled. And for this
function too, 'nil' value must be returned if there is no data and the
channel can't receive more data.

This patch is related to the issue #1993. It must be backported as far as
2.5.

src/hlua.c

index 722fb88d0a95521bbcb87c519ebf845a0ccdd4ac..e5c6c93b67bdfa0d6e77fda6a8e77403eb1d44b9 100644 (file)
@@ -3242,11 +3242,22 @@ __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KCon
                }
        }
 
-       if (offset + len > output + input) {
+       /* Wait for more data if possible if no length was specified and there
+        * is no data or not enough data was received.
+        */
+       if (!len || offset + len > output + input) {
                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) {
+                       lua_pushnil(L);
+                       return -1;
+               }
+
+               /* Otherwise, return all data */
                len = output + input - offset;
        }
 
@@ -3315,11 +3326,22 @@ __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KCon
                }
        }
 
-       if (offset + len > output + input) {
+       /* Wait for more data if possible if no line is found and no length was
+        * specified or not enough data was received.
+        */
+       if (lua_gettop(L) != 3 ||  offset + len > output + input) {
                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) {
+                       lua_pushnil(L);
+                       return -1;
+               }
+
+               /* Otherwise, return all data */
                len = output + input - offset;
        }