Changeset

10899:8048255ae61e

util.ringbuffer: Prevent creation of buffer with negative size Previously this would have been (unsigned)-1 which is a large positive integer.
author Kim Alvefur <zash@zash.se>
date Thu, 04 Jun 2020 16:11:08 +0200
parents 10898:c6465fb3c839
children 10900:9e6d979dd603
files spec/util_ringbuffer_spec.lua util-src/ringbuffer.c
diffstat 2 files changed, 6 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_ringbuffer_spec.lua	Fri May 29 18:11:42 2020 +0200
+++ b/spec/util_ringbuffer_spec.lua	Thu Jun 04 16:11:08 2020 +0200
@@ -12,6 +12,11 @@
 				rb.new(0);
 			end);
 		end);
+		it("won't create a negatively sized buffer", function ()
+			assert.has_error(function ()
+				rb.new(-1);
+			end);
+		end);
 	end);
 	describe(":write", function ()
 		local b = rb.new();
--- a/util-src/ringbuffer.c	Fri May 29 18:11:42 2020 +0200
+++ b/util-src/ringbuffer.c	Thu Jun 04 16:11:08 2020 +0200
@@ -197,7 +197,7 @@
 }
 
 static int rb_new(lua_State *L) {
-	size_t size = luaL_optinteger(L, 1, sysconf(_SC_PAGESIZE));
+	lua_Integer 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);