From df60d9f0684876fa74b882b086ca1205581f99cb Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 21 Aug 2021 16:33:35 +0200 Subject: Moved error handling out of buffer_grow --- buffer.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'buffer.c') diff --git a/buffer.c b/buffer.c index f7e4334..6835933 100644 --- a/buffer.c +++ b/buffer.c @@ -9,26 +9,25 @@ #include "stream.h" -void buffer_grow(lua_State * L, struct buffer * b) +int buffer_grow(struct buffer * b) { int allocated = b->allocated + 1024; if (8192 < allocated) { - lua_pushliteral(L, "Too large buffer"); - lua_error(L); + errno = ENOMEM; + return -1; } void * buffer = realloc(b->data, allocated); if (NULL == buffer) - { - lua_pushliteral(L, "Could not grow buffer"); - lua_error(L); - } + return -1; b->data = buffer; b->allocated = allocated; + + return allocated; } int buffer_read_more(lua_State * L, int fd, struct buffer * in, int minimum_length, lua_KContext ctx) @@ -37,7 +36,12 @@ int buffer_read_more(lua_State * L, int fd, struct buffer * in, int minimum_leng while (free_space < minimum_length) { - buffer_grow(L, in); + const int res = buffer_grow(in); + if (-1 == res) + { + lua_pushstring(L, strerror(errno)); + return lua_error(L); + } } if (0 < in->offset) -- cgit v1.1