From dc026aab8e4ae0b2412c4d66b0776250721cd535 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Mon, 5 Oct 2026 21:31:18 -0400 Subject: [PATCH] http/hpack: fix integer encoding at the prefix maximum Signed-off-by: Waqas Hussain --- http/hpack.lua | 2 +- spec/hpack_spec.lua | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/http/hpack.lua b/http/hpack.lua index 09c3cfa8..dee35643 100644 --- a/http/hpack.lua +++ b/http/hpack.lua @@ -15,7 +15,7 @@ local function encode_integer(i, prefix_len, mask) assert(i >= 0 and i % 1 == 0) assert(prefix_len >= 0 and prefix_len <= 8 and prefix_len % 1 == 0) assert(mask >= 0 and mask <= 256 and mask % 1 == 0) - if i < 2^prefix_len then + if i < 2^prefix_len-1 then return schar(bor(mask, i)) else local prefix_mask = 2^prefix_len-1 diff --git a/spec/hpack_spec.lua b/spec/hpack_spec.lua index 65f59e44..adb83aa3 100644 --- a/spec/hpack_spec.lua +++ b/spec/hpack_spec.lua @@ -287,6 +287,27 @@ e9ae 82ae 43d3]]) end) end) +describe("Integer prefix boundaries", function() + local hpack = require "http.hpack" + it("encodes the prefix maximum with a zero continuation byte", function() + for prefix_len=1, 8 do + local maximum = 2^prefix_len-1 + assert.same(string.char(maximum) .. "\0", hpack.encode_integer(maximum, prefix_len, 0)) + end + end) + it("does not consume the next field at the prefix maximum", function() + for prefix_len=1, 8 do + local maximum = 2^prefix_len-1 + for _, value in ipairs({maximum-1, maximum, maximum+1}) do + local encoded = hpack.encode_integer(value, prefix_len, 0) + local decoded, next_pos = hpack.decode_integer(encoded .. "x", prefix_len) + assert.same(value, decoded) + assert.same(#encoded+1, next_pos) + end + end + end) +end) + describe("Partial input is returned with correct offset", function() local hpack = require "http.hpack" it("decodes integers without errors", function()