summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2021-08-21 16:33:35 +0200
committerAki <please@ignore.pl>2021-08-21 16:33:35 +0200
commitdf60d9f0684876fa74b882b086ca1205581f99cb (patch)
tree95f45c6bb770c7cefdcaaa2bde6311a7a2c88704
parent4c76943de7912996cc6e380a44da9e8f3ea32503 (diff)
downloadplop-df60d9f0684876fa74b882b086ca1205581f99cb.zip
plop-df60d9f0684876fa74b882b086ca1205581f99cb.tar.gz
plop-df60d9f0684876fa74b882b086ca1205581f99cb.tar.bz2
Moved error handling out of buffer_grow
-rw-r--r--buffer.c20
-rw-r--r--buffer.h2
-rw-r--r--stream.c7
3 files changed, 19 insertions, 10 deletions
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)
diff --git a/buffer.h b/buffer.h
index d3153c7..f80c22c 100644
--- a/buffer.h
+++ b/buffer.h
@@ -11,7 +11,7 @@ struct buffer
int allocated;
};
-void buffer_grow(lua_State *, struct buffer *);
+int buffer_grow(struct buffer *);
int buffer_read_more(lua_State *, const int, struct buffer *, const int, lua_KContext);
int buffer_prepare_at_least(lua_State *, const int, struct buffer *, const int, lua_KContext);
int buffer_until(struct buffer *, const char *, const int);
diff --git a/stream.c b/stream.c
index f7c75e0..9cf475e 100644
--- a/stream.c
+++ b/stream.c
@@ -225,7 +225,12 @@ int stream_write(lua_State * L)
while (free_space < (int) data_length)
{
- buffer_grow(L, &s->out);
+ const int res = buffer_grow(&s->out);
+ if (-1 == res)
+ {
+ lua_pushstring(L, strerror(errno));
+ return lua_error(L);
+ }
free_space = s->out.allocated - s->out.length;
}