BUG/MINOR: hlua: Fix Channel.line and Channel.data behavior regarding the doc

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.
This commit is contained in:
Christopher Faulet 2023-01-10 15:29:54 +01:00
parent 5f36bfe42e
commit 0ae2e63d85

View 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;
}