# HG changeset patch # User Matthew Wild # Date 1597933339 -3600 # Node ID d7a403060946695e7fd37ad59271712be287c279 # Parent 8ed6c5bdbb21e7564c844a371ea6a8c36df81a9d util.dbuffer: Fix traceback when :collapse() is called on empty buffer diff -r 8ed6c5bdbb21 -r d7a403060946 spec/util_dbuffer_spec.lua --- 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); diff -r 8ed6c5bdbb21 -r d7a403060946 util/dbuffer.lua --- 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