aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2020-08-20 15:22:19 +0100
committerMatthew Wild <mwild1@gmail.com>2020-08-20 15:22:19 +0100
commit41f1cb95d3d18a0203edf3bb2f75458d42d56629 (patch)
tree853e0fbe967c83dc9cd8c2f95403d9c0af8d3129
parentd0835ef1cf1eb4bff19a9444c510ffa5574259c6 (diff)
downloadprosody-41f1cb95d3d18a0203edf3bb2f75458d42d56629.tar.gz
prosody-41f1cb95d3d18a0203edf3bb2f75458d42d56629.zip
util.dbuffer: Fix traceback when :collapse() is called on empty buffer
-rw-r--r--spec/util_dbuffer_spec.lua12
-rw-r--r--util/dbuffer.lua2
2 files changed, 13 insertions, 1 deletions
diff --git a/spec/util_dbuffer_spec.lua b/spec/util_dbuffer_spec.lua
index c4fa22b2..b7f80dc9 100644
--- a/spec/util_dbuffer_spec.lua
+++ b/spec/util_dbuffer_spec.lua
@@ -46,6 +46,13 @@ describe("util.dbuffer", function ()
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 @@ describe("util.dbuffer", function ()
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 --git a/util/dbuffer.lua b/util/dbuffer.lua
index 63dca750..9ec40eb9 100644
--- a/util/dbuffer.lua
+++ b/util/dbuffer.lua
@@ -142,7 +142,7 @@ function dbuffer_methods:collapse(bytes)
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