From: Christopher Faulet Date: Tue, 27 May 2025 05:46:44 +0000 (+0200) Subject: BUG/MEDIUM: hlua: Fix getline() for TCP applets to work with applet's buffers X-Git-Tag: v3.0.11~9 X-Git-Url: http://git.haproxy.org/?a=commitdiff_plain;h=02854140dbe24c5cafc11f496fd2d7c4e25124ff;p=haproxy-3.0.git BUG/MEDIUM: hlua: Fix getline() for TCP applets to work with applet's buffers The commit e5e36ce09 ("BUG/MEDIUM: hlua/cli: Fix lua CLI commands to work with applet's buffers") fixed the TCP applets API to work with applets using its own buffers. Howver the getline() function was not updated. It could be an issue for anyone registering a CLI commands reading lines. This patch should be backported as far as 3.0. (cherry picked from commit c0ecef71d7ceaf404bc18241bdb878a4e72033b4) Signed-off-by: Christopher Faulet (cherry picked from commit 08c48cf43598a489f74d4a128f672545d1577d30) Signed-off-by: Christopher Faulet --- diff --git a/src/hlua.c b/src/hlua.c index b078d26..2828273 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -5246,7 +5246,35 @@ __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KC size_t len2; /* Read the maximum amount of data available. */ - ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); + if (luactx->appctx->flags & APPCTX_FL_INOUT_BUFS) { + size_t l; + int line_found = 0; + + ret = b_getblk_nc(&luactx->appctx->inbuf, &blk1, &len1, &blk2, &len2, 0, b_data(&luactx->appctx->inbuf)); + if (ret == 0 && se_fl_test(luactx->appctx->sedesc, SE_FL_SHW)) + ret = -1; + + if (ret >= 1) { + for (l = 0; l < len1 && blk1[l] != '\n'; l++); + if (l < len1 && blk1[l] == '\n') { + len1 = l + 1; + line_found = 1; + } + } + + if (!line_found && ret >= 2) { + for (l = 0; l < len2 && blk2[l] != '\n'; l++); + if (l < len2 && blk2[l] == '\n') { + len2 = l + 1; + line_found = 1; + } + } + + if (!line_found && !se_fl_test(luactx->appctx->sedesc, SE_FL_SHW)) + ret = 0; + } + else + ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); /* Data not yet available. return yield. */ if (ret == 0) { @@ -5268,8 +5296,11 @@ __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KC luaL_addlstring(&luactx->b, blk1, len1); luaL_addlstring(&luactx->b, blk2, len2); - /* Consume input channel output buffer data. */ - co_skip(sc_oc(sc), len1 + len2); + if (luactx->appctx->flags & APPCTX_FL_INOUT_BUFS) + b_del(&luactx->appctx->inbuf, len1 + len2); + else + co_skip(sc_oc(sc), len1 + len2); + luaL_pushresult(&luactx->b); return 1; }