From a5de0e15959a241afc9afb39f1b02a2517894f7b Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Mon, 12 May 2025 16:12:13 +0200 Subject: [PATCH] BUG/MINOR: hlua: Fix Channel:data() and Channel:line() to respect documentation 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. --- src/hlua.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/hlua.c b/src/hlua.c index cb18132ae..f5ea3f3bc 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -3932,7 +3932,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)); @@ -3949,11 +3949,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)); @@ -3971,17 +3974,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; } @@ -4007,7 +4016,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); @@ -4023,11 +4032,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) { @@ -4055,17 +4066,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; }