Skip to content

Commit a5de0e1

Browse files
committed
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.
1 parent e049bd0 commit a5de0e1

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/hlua.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3932,7 +3932,7 @@ __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KCon
39323932
struct channel *chn;
39333933
struct filter *filter;
39343934
size_t input, output;
3935-
int offset, len;
3935+
int offset, len = 0;
39363936

39373937
chn = MAY_LJMP(hlua_checkchannel(L, 1));
39383938

@@ -3949,11 +3949,14 @@ __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KCon
39493949
if (offset < 0)
39503950
offset = MAX(0, (int)input + offset);
39513951
offset += output;
3952-
if (offset < output || offset > input + output) {
3952+
if (offset < output) {
39533953
lua_pushfstring(L, "offset out of range.");
39543954
WILL_LJMP(lua_error(L));
39553955
}
39563956
}
3957+
if (offset >= output+input)
3958+
goto wait_more_data;
3959+
39573960
len = output + input - offset;
39583961
if (lua_gettop(L) == 3) {
39593962
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
39713974
* is no data or not enough data was received.
39723975
*/
39733976
if (!len || offset + len > output + input) {
3977+
wait_more_data:
39743978
if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
39753979
/* Yield waiting for more data, as requested */
39763980
MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0));
39773981
}
39783982

3979-
/* Return 'nil' if there is no data and the channel can't receive more data */
3980-
if (!len) {
3983+
if (!input) {
3984+
/* Return 'nil' if there is no data and the channel can't receive more data */
39813985
lua_pushnil(L);
39823986
return -1;
39833987
}
39843988

3989+
if (!len) {
3990+
lua_pushstring(L, "");
3991+
return 1;
3992+
}
3993+
39853994
/* Otherwise, return all data */
39863995
len = output + input - offset;
39873996
}
@@ -4007,7 +4016,7 @@ __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KCon
40074016
struct channel *chn;
40084017
struct filter *filter;
40094018
size_t l, input, output;
4010-
int offset, len;
4019+
int offset, len = 0;
40114020

40124021
chn = MAY_LJMP(hlua_checkchannel(L, 1));
40134022
output = co_data(chn);
@@ -4023,11 +4032,13 @@ __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KCon
40234032
if (offset < 0)
40244033
offset = MAX(0, (int)input + offset);
40254034
offset += output;
4026-
if (offset < output || offset > input + output) {
4035+
if (offset < output) {
40274036
lua_pushfstring(L, "offset out of range.");
40284037
WILL_LJMP(lua_error(L));
40294038
}
40304039
}
4040+
if (offset > output + input)
4041+
goto wait_more_data;
40314042

40324043
len = output + input - offset;
40334044
if (lua_gettop(L) == 3) {
@@ -4055,17 +4066,23 @@ __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KCon
40554066
* specified or not enough data was received.
40564067
*/
40574068
if (lua_gettop(L) != 3 || offset + len > output + input) {
4069+
wait_more_data:
40584070
if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
40594071
/* Yield waiting for more data */
40604072
MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0));
40614073
}
40624074

4063-
/* Return 'nil' if there is no data and the channel can't receive more data */
4064-
if (!len) {
4075+
if (!input) {
4076+
/* Return 'nil' if there is no data and the channel can't receive more data */
40654077
lua_pushnil(L);
40664078
return -1;
40674079
}
40684080

4081+
if (!len) {
4082+
lua_pushstring(L, "");
4083+
return 1;
4084+
}
4085+
40694086
/* Otherwise, return all data */
40704087
len = output + input - offset;
40714088
}

0 commit comments

Comments
 (0)