From c0ecef71d7ceaf404bc18241bdb878a4e72033b4 Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Tue, 27 May 2025 07:46:44 +0200 Subject: [PATCH] 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. --- src/hlua.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/hlua.c b/src/hlua.c index c971b15e5..6c61762e9 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -5307,7 +5307,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) { @@ -5329,8 +5357,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; } -- 2.47.2