fix(mpack): boundary values for negative integer encoding #37255

Problem:
libmpack encodes boundary values -129 and -32769 with wrong integer
sizes:
- -129 as int8 instead of int16
- -32769 as int16 instead of int32
because the boundary checks compare against the wrong values (e.g., lo
< 0xffffff7f instead of lo < 0xffffff80). This caused data corruption:
-129 would decode as 127.

Solution:
Fix off-by-one errors in the two's complement boundary constants:
0xffffff80 (-128, min int8) and 0xffff8000 (-32768, min int16).

Fixes #37202

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
benarcher2691
2026-03-21 22:07:33 +01:00
committed by GitHub
parent 4d3a67cd62
commit 2069be281c
4 changed files with 59 additions and 5 deletions

View File

@@ -1,7 +1,8 @@
local t = require('test.unit.testutil')
local cimport = t.cimport
local itp = t.gen_itp(it)
local lib = cimport('./src/nvim/msgpack_rpc/unpacker.h', './src/nvim/memory.h')
local lib =
cimport('./src/nvim/msgpack_rpc/unpacker.h', './src/nvim/memory.h', './src/mpack/conv.h')
local ffi = t.ffi
local eq = t.eq
local to_cstr = t.to_cstr
@@ -77,3 +78,31 @@ describe('msgpack', function()
end)
end)
end)
describe('libmpack', function()
describe('mpack_pack_number', function()
itp('token length at negative integer boundaries #37202', function()
-- tok.length is the byte count for the integer value (not including the msgpack type prefix byte).
-- int8 min: -128 → 1 byte
local tok = lib.mpack_pack_number(-128)
eq(lib.MPACK_TOKEN_SINT, tok.type)
eq(1, tok.length)
-- one past int8: -129 → 2 bytes (int16)
tok = lib.mpack_pack_number(-129)
eq(lib.MPACK_TOKEN_SINT, tok.type)
eq(2, tok.length)
-- int16 min: -32768 → 2 bytes
tok = lib.mpack_pack_number(-32768)
eq(lib.MPACK_TOKEN_SINT, tok.type)
eq(2, tok.length)
-- one past int16: -32769 → 4 bytes (int32)
tok = lib.mpack_pack_number(-32769)
eq(lib.MPACK_TOKEN_SINT, tok.type)
eq(4, tok.length)
end)
end)
end)