From 0bb1474ce668f50c568e7751ac553cbf0acdba77 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 29 May 2020 18:11:42 +0200 Subject: util.ringbuffer: Prevent creation of zero-size buffer --- spec/util_ringbuffer_spec.lua | 5 +++++ util-src/ringbuffer.c | 1 + 2 files changed, 6 insertions(+) diff --git a/spec/util_ringbuffer_spec.lua b/spec/util_ringbuffer_spec.lua index 5d63b24b..ccb5493a 100644 --- a/spec/util_ringbuffer_spec.lua +++ b/spec/util_ringbuffer_spec.lua @@ -7,6 +7,11 @@ describe("util.ringbuffer", function () it("can be created", function () assert.truthy(rb.new()); end); + it("won't create an empty buffer", function () + assert.has_error(function () + rb.new(0); + end); + end); end); describe(":write", function () local b = rb.new(); diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c index 3e17cdf5..f4a51cc9 100644 --- a/util-src/ringbuffer.c +++ b/util-src/ringbuffer.c @@ -198,6 +198,7 @@ static int rb_free(lua_State *L) { static int rb_new(lua_State *L) { size_t size = luaL_optinteger(L, 1, sysconf(_SC_PAGESIZE)); + luaL_argcheck(L, size > 0, 1, "positive integer expected"); ringbuffer *b = lua_newuserdata(L, sizeof(ringbuffer) + size); b->rpos = 0; -- cgit v1.2.3