# HG changeset patch # User Kim Alvefur # Date 1590768702 -7200 # Node ID c6465fb3c839174d0e655011eece503db10f8783 # Parent 37df1e757f02cdbec815b8540e5b76a15a1ea79c util.ringbuffer: Prevent creation of zero-size buffer diff -r 37df1e757f02 -r c6465fb3c839 spec/util_ringbuffer_spec.lua --- a/spec/util_ringbuffer_spec.lua Fri May 29 17:53:00 2020 +0200 +++ b/spec/util_ringbuffer_spec.lua Fri May 29 18:11:42 2020 +0200 @@ -7,6 +7,11 @@ 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 -r 37df1e757f02 -r c6465fb3c839 util-src/ringbuffer.c --- a/util-src/ringbuffer.c Fri May 29 17:53:00 2020 +0200 +++ b/util-src/ringbuffer.c Fri May 29 18:11:42 2020 +0200 @@ -198,6 +198,7 @@ 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;