robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
3.3 KiB109 linesraw
1local t = require('test.unit.testutil')
2local describe = t.describe
3local cimport = t.cimport
4local itp = t.gen_itp(t.it)
5local lib =
6  cimport('./src/nvim/msgpack_rpc/unpacker.h', './src/nvim/memory.h', './src/mpack/conv.h')
7local ffi = t.ffi
8local eq = t.eq
9local to_cstr = t.to_cstr
10
11--- @class Unpacker
12--- @field read_ptr ffi.cdata*
13--- @field read_size number
14
15--- @alias Unpacker* table<number, Unpacker>
16--- @return Unpacker* unpacker `unpacker[0]` to dereference
17local function make_unpacker()
18  return ffi.gc(ffi.cast('Unpacker*', lib.xcalloc(1, ffi.sizeof('Unpacker'))), function(unpacker)
19    lib.unpacker_teardown(unpacker)
20    lib.xfree(unpacker)
21  end)
22end
23
24--- @param unpacker Unpacker*
25--- @param data string
26--- @param size number? *default: data:len()*
27local function unpacker_goto(unpacker, data, size)
28  unpacker[0].read_ptr = to_cstr(data)
29  unpacker[0].read_size = size or data:len()
30end
31
32--- @param unpacker Unpacker*
33--- @return boolean
34local function unpacker_advance(unpacker)
35  return lib.unpacker_advance(unpacker)
36end
37
38describe('msgpack', function()
39  describe('unpacker', function()
40    itp(
41      'does not crash when paused between `cells` and `wrap` params of `grid_line` #25184',
42      function()
43        -- [kMessageTypeNotification, "redraw", [
44        --   ["grid_line",
45        --     [2, 0, 0, [[" " , 0, 77]], false]
46        --   ]
47        -- ]]
48        local payload =
49          '\x93\x02\xa6\x72\x65\x64\x72\x61\x77\x91\x92\xa9\x67\x72\x69\x64\x5f\x6c\x69\x6e\x65\x95\x02\x00\x00\x91\x93\xa1\x20\x00\x4d\xc2'
50
51        local unpacker = make_unpacker()
52        lib.unpacker_init(unpacker)
53
54        unpacker_goto(unpacker, payload, payload:len() - 1)
55        local finished = unpacker_advance(unpacker)
56        eq(false, finished)
57
58        unpacker[0].read_size = unpacker[0].read_size + 1
59        finished = unpacker_advance(unpacker)
60        eq(true, finished)
61      end
62    )
63
64    itp('does not crash when parsing grid_line event with 0 `cells` #25184', function()
65      local unpacker = make_unpacker()
66      lib.unpacker_init(unpacker)
67
68      unpacker_goto(
69        unpacker,
70        -- [kMessageTypeNotification, "redraw", [
71        --   ["grid_line",
72        --     [2, 0, 0, [], false]
73        --   ]
74        -- ]]
75        '\x93\x02\xa6\x72\x65\x64\x72\x61\x77\x91\x92\xa9\x67\x72\x69\x64\x5f\x6c\x69\x6e\x65\x95\x02\x00\x00\x90\xc2'
76      )
77      local finished = unpacker_advance(unpacker)
78      eq(true, finished)
79    end)
80  end)
81end)
82
83describe('libmpack', function()
84  describe('mpack_pack_number', function()
85    itp('token length at negative integer boundaries #37202', function()
86      -- tok.length is the byte count for the integer value (not including the msgpack type prefix byte).
87
88      -- int8 min: -128 → 1 byte
89      local tok = lib.mpack_pack_number(-128)
90      eq(lib.MPACK_TOKEN_SINT, tok.type)
91      eq(1, tok.length)
92
93      -- one past int8: -129 → 2 bytes (int16)
94      tok = lib.mpack_pack_number(-129)
95      eq(lib.MPACK_TOKEN_SINT, tok.type)
96      eq(2, tok.length)
97
98      -- int16 min: -32768 → 2 bytes
99      tok = lib.mpack_pack_number(-32768)
100      eq(lib.MPACK_TOKEN_SINT, tok.type)
101      eq(2, tok.length)
102
103      -- one past int16: -32769 → 4 bytes (int32)
104      tok = lib.mpack_pack_number(-32769)
105      eq(lib.MPACK_TOKEN_SINT, tok.type)
106      eq(4, tok.length)
107    end)
108  end)
109end)