Changeset

11028:d7a403060946

util.dbuffer: Fix traceback when :collapse() is called on empty buffer
author Matthew Wild <mwild1@gmail.com>
date Thu, 20 Aug 2020 15:22:19 +0100
parents 11027:8ed6c5bdbb21
children 11029:5550fc5e83f3
files spec/util_dbuffer_spec.lua util/dbuffer.lua
diffstat 2 files changed, 13 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_dbuffer_spec.lua	Sun Aug 16 20:30:02 2020 +0200
+++ b/spec/util_dbuffer_spec.lua	Thu Aug 20 15:22:19 2020 +0100
@@ -46,6 +46,13 @@
 		end);
 	end);
 
+	describe(":collapse()", function ()
+		it("works on an empty buffer", function ()
+			local b = dbuffer.new();
+			b:collapse();
+		end);
+	end);
+
 	describe(":sub", function ()
 		-- Helper function to compare buffer:sub() with string:sub()
 		local s = "hello world";
@@ -106,5 +113,10 @@
 			local r = { b:byte(1, 2) };
 			assert.same({ 0, 140 }, r);
 		end);
+
+		it("works on an empty buffer", function ()
+			local b = dbuffer.new();
+			assert.equal("", b:sub(1,1));
+		end);
 	end);
 end);
--- a/util/dbuffer.lua	Sun Aug 16 20:30:02 2020 +0200
+++ b/util/dbuffer.lua	Thu Aug 20 15:22:19 2020 +0100
@@ -142,7 +142,7 @@
 
 	local front_chunk = self.items:peek();
 
-	if #front_chunk - self.front_consumed >= bytes then
+	if not front_chunk or #front_chunk - self.front_consumed >= bytes then
 		return;
 	end