summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2018-01-24 19:45:00 +0100
committerJo-Philipp Wich <jo@mein.io>2018-01-24 20:19:59 +0100
commitdb86175c2d90ba640b158e8eebd7409227544c4b (patch)
tree7bb6ccbedfbdbee93d41ebf23260569e90362f26
parenta235636a2687fafb9c474e4b134a59ff66425c92 (diff)
lua: honour size argument in recv() function
The existing implementation incorrectly attempted to read the entire stdin instead of fetching at most the given amount of bytes. While we're at it, also make the size argument optional and let it default to Luas internal buffer size. Suggested-by: Bryan Mayland <bmayland+lede@capnbry.net> Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--lua.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/lua.c b/lua.c
index 3efe22b..abae88c 100644
--- a/lua.c
+++ b/lua.c
@@ -46,13 +46,14 @@ static int uh_lua_recv(lua_State *L)
int len;
int r;
- len = luaL_checknumber(L, 1);
+ len = luaL_optnumber(L, 1, LUAL_BUFFERSIZE);
luaL_buffinit(L, &B);
while(len > 0) {
char *buf;
buf = luaL_prepbuffer(&B);
- r = read(STDIN_FILENO, buf, LUAL_BUFFERSIZE);
+ r = read(STDIN_FILENO, buf,
+ len < LUAL_BUFFERSIZE ? len : LUAL_BUFFERSIZE);
if (r < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN) {
pfd.revents = 0;
@@ -72,6 +73,7 @@ static int uh_lua_recv(lua_State *L)
luaL_addsize(&B, r);
data_len += r;
+ len -= r;
if (r != LUAL_BUFFERSIZE)
break;
}