Changeset

10898:c6465fb3c839

util.ringbuffer: Prevent creation of zero-size buffer
author Kim Alvefur <zash@zash.se>
date Fri, 29 May 2020 18:11:42 +0200
parents 10897:37df1e757f02
children 10899:8048255ae61e
files spec/util_ringbuffer_spec.lua util-src/ringbuffer.c
diffstat 2 files changed, 6 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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();
--- 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;