robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
5.7 KiB133 linesraw
1local t = require('test.unit.testutil')
2local describe = t.describe
3local itp = t.gen_itp(t.it)
4
5local cimport = t.cimport
6local eq = t.eq
7local neq = t.neq
8local ffi = t.ffi
9
10local decode = cimport(
11  './src/nvim/eval/decode.h',
12  './src/nvim/eval/typval.h',
13  './src/nvim/globals.h',
14  './src/nvim/memory.h',
15  './src/nvim/message.h'
16)
17
18describe('json_decode_string()', function()
19  local char = function(c)
20    return ffi.gc(decode.xmemdup(c, 1), decode.xfree)
21  end
22
23  itp('does not overflow when running with `n…`, `t…`, `f…`', function()
24    local rettv = ffi.new('typval_T', { v_type = decode.VAR_UNKNOWN })
25    decode.emsg_silent = 1
26    -- This will not crash, but if `len` argument will be ignored it will parse
27    -- `null` as `null` and if not it will parse `null` as `n`.
28    eq(0, decode.json_decode_string('null', 1, rettv))
29    eq(decode.VAR_UNKNOWN, rettv.v_type)
30    eq(0, decode.json_decode_string('true', 1, rettv))
31    eq(decode.VAR_UNKNOWN, rettv.v_type)
32    eq(0, decode.json_decode_string('false', 1, rettv))
33    eq(decode.VAR_UNKNOWN, rettv.v_type)
34    eq(0, decode.json_decode_string('null', 2, rettv))
35    eq(decode.VAR_UNKNOWN, rettv.v_type)
36    eq(0, decode.json_decode_string('true', 2, rettv))
37    eq(decode.VAR_UNKNOWN, rettv.v_type)
38    eq(0, decode.json_decode_string('false', 2, rettv))
39    eq(decode.VAR_UNKNOWN, rettv.v_type)
40    eq(0, decode.json_decode_string('null', 3, rettv))
41    eq(decode.VAR_UNKNOWN, rettv.v_type)
42    eq(0, decode.json_decode_string('true', 3, rettv))
43    eq(decode.VAR_UNKNOWN, rettv.v_type)
44    eq(0, decode.json_decode_string('false', 3, rettv))
45    eq(decode.VAR_UNKNOWN, rettv.v_type)
46    eq(0, decode.json_decode_string('false', 4, rettv))
47    eq(decode.VAR_UNKNOWN, rettv.v_type)
48  end)
49
50  itp('does not overflow and crash when running with `n`, `t`, `f`', function()
51    local rettv = ffi.new('typval_T', { v_type = decode.VAR_UNKNOWN })
52    decode.emsg_silent = 1
53    eq(0, decode.json_decode_string(char('n'), 1, rettv))
54    eq(decode.VAR_UNKNOWN, rettv.v_type)
55    eq(0, decode.json_decode_string(char('t'), 1, rettv))
56    eq(decode.VAR_UNKNOWN, rettv.v_type)
57    eq(0, decode.json_decode_string(char('f'), 1, rettv))
58    eq(decode.VAR_UNKNOWN, rettv.v_type)
59  end)
60
61  itp('does not overflow when running with `"…`', function()
62    local rettv = ffi.new('typval_T', { v_type = decode.VAR_UNKNOWN })
63    decode.emsg_silent = 1
64    eq(0, decode.json_decode_string('"t"', 2, rettv))
65    eq(decode.VAR_UNKNOWN, rettv.v_type)
66    eq(0, decode.json_decode_string('""', 1, rettv))
67    eq(decode.VAR_UNKNOWN, rettv.v_type)
68  end)
69
70  local check_failure = function(s, len, msg)
71    local rettv = ffi.new('typval_T', { v_type = decode.VAR_UNKNOWN })
72    eq(0, decode.json_decode_string(s, len, rettv))
73    eq(decode.VAR_UNKNOWN, rettv.v_type)
74    neq(nil, decode.msg_hist_last)
75    eq(msg, ffi.string(decode.msg_hist_last.msg.items[0].text.data))
76  end
77
78  itp('does not overflow in error messages', function()
79    collectgarbage('restart')
80    check_failure(']test', 1, 'E474: No container to close: ]')
81    check_failure('[}test', 2, 'E474: Closing list with curly bracket: }')
82    check_failure('{]test', 2, 'E474: Closing dictionary with square bracket: ]')
83    check_failure('[1,]test', 4, 'E474: Trailing comma: ]')
84    check_failure('{"1":}test', 6, 'E474: Expected value after colon: }')
85    check_failure('{"1"}test', 5, 'E474: Expected value: }')
86    check_failure(',test', 1, 'E474: Comma not inside container: ,')
87    check_failure('[1,,1]test', 6, 'E474: Duplicate comma: ,1]')
88    check_failure('{"1":,}test', 7, 'E474: Comma after colon: ,}')
89    check_failure('{"1",}test', 6, 'E474: Using comma in place of colon: ,}')
90    check_failure('{,}test', 3, 'E474: Leading comma: ,}')
91    check_failure('[,]test', 3, 'E474: Leading comma: ,]')
92    check_failure(':test', 1, 'E474: Colon not inside container: :')
93    check_failure('[:]test', 3, 'E474: Using colon not in dictionary: :]')
94    check_failure('{:}test', 3, 'E474: Unexpected colon: :}')
95    check_failure('{"1"::1}test', 8, 'E474: Duplicate colon: :1}')
96    check_failure('ntest', 1, 'E474: Expected null: n')
97    check_failure('ttest', 1, 'E474: Expected true: t')
98    check_failure('ftest', 1, 'E474: Expected false: f')
99    check_failure('"\\test', 2, 'E474: Unfinished escape sequence: "\\')
100    check_failure('"\\u"test', 4, 'E474: Unfinished unicode escape sequence: "\\u"')
101    check_failure('"\\uXXXX"est', 8, 'E474: Expected four hex digits after \\u: \\uXXXX"')
102    check_failure('"\\?"test', 4, 'E474: Unknown escape sequence: \\?"')
103    check_failure(
104      '"\t"test',
105      3,
106      'E474: ASCII control characters cannot be present inside string: \t"'
107    )
108    check_failure('"\194"test', 3, 'E474: Only UTF-8 strings allowed: \194"')
109    check_failure(
110      '"\252\144\128\128\128\128"test',
111      8,
112      'E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \252\144\128\128\128\128"'
113    )
114    check_failure('"test', 1, 'E474: Expected string end: "')
115    check_failure('-test', 1, 'E474: Missing number after minus sign: -')
116    check_failure('-1.test', 3, 'E474: Missing number after decimal dot: -1.')
117    check_failure('-1.0etest', 5, 'E474: Missing exponent: -1.0e')
118    check_failure('?test', 1, 'E474: Unidentified byte: ?')
119    check_failure('1?test', 2, 'E474: Trailing characters: ?')
120    check_failure('[1test', 2, 'E474: Unexpected end of input: [1')
121  end)
122
123  itp('does not overflow with `-`', function()
124    check_failure('-0', 1, 'E474: Missing number after minus sign: -')
125  end)
126
127  itp('does not overflow and crash when running with `"`', function()
128    local rettv = ffi.new('typval_T', { v_type = decode.VAR_UNKNOWN })
129    decode.emsg_silent = 1
130    eq(0, decode.json_decode_string(char('"'), 1, rettv))
131    eq(decode.VAR_UNKNOWN, rettv.v_type)
132  end)
133end)