util.ringbuffer: Ensure unsigned chars are always returned from :byte()
authorMatthew Wild <mwild1@gmail.com>
Wed, 24 Jun 2020 12:34:20 +0100
changeset 10957 c3b3ac63f4c3
parent 10956 05d218aae3d1
child 10958 fc310727adfb
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
spec/util_ringbuffer_spec.lua
util-src/ringbuffer.c
--- a/spec/util_ringbuffer_spec.lua	Tue Jun 23 17:59:24 2020 +0200
+++ b/spec/util_ringbuffer_spec.lua	Wed Jun 24 12:34:20 2020 +0100
@@ -92,5 +92,12 @@
 				end
 			end
 		end);
+
+		it("works with characters > 127", function ()
+			local b = rb.new();
+			b:write(string.char(0, 140));
+			local r = { b:byte(1, 2) };
+			assert.same({ 0, 140 }, r);
+		end);
 	end);
 end);
--- a/util-src/ringbuffer.c	Tue Jun 23 17:59:24 2020 +0200
+++ b/util-src/ringbuffer.c	Wed Jun 24 12:34:20 2020 +0100
@@ -262,15 +262,15 @@
 	if(calc_splice_positions(b, start, end, &wrapped_start, &wrapped_end)) {
 		if(wrapped_end <= wrapped_start) {
 			for(i = wrapped_start; i < (long)b->alen; i++) {
-				lua_pushinteger(L, b->buffer[i]);
+				lua_pushinteger(L, (unsigned char)b->buffer[i]);
 			}
 			for(i = 0; i < wrapped_end; i++) {
-				lua_pushinteger(L, b->buffer[i]);
+				lua_pushinteger(L, (unsigned char)b->buffer[i]);
 			}
 			return wrapped_end + (b->alen - wrapped_start);
 		} else {
 			for(i = wrapped_start; i < wrapped_end; i++) {
-				lua_pushinteger(L, b->buffer[i]);
+				lua_pushinteger(L, (unsigned char)b->buffer[i]);
 			}
 			return wrapped_end - wrapped_start;
 		}