From b3be7b741386bbbf076e87859cb1e3d990316f5c Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 25 Sep 2023 11:06:15 +0800 Subject: [PATCH] fix(api): handle NUL in nvim_err_write() and nvim_out_write() (#25354) --- src/nvim/api/vim.c | 2 ++ test/functional/api/vim_spec.lua | 50 +++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 7a5c7cc181..78396edef5 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -1709,6 +1709,8 @@ static void write_msg(String message, bool to_err, bool writeln) msg_didout = true; \ kv_drop(line_buf, kv_size(line_buf)); \ kv_resize(line_buf, LINE_BUFFER_MIN_SIZE); \ + } else if (c == NUL) { \ + kv_push(line_buf, NL); \ } else { \ kv_push(line_buf, c); \ } diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index d82e5f66c9..09495fbaac 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -2053,6 +2053,19 @@ describe('API', function() end) describe('nvim_out_write', function() + local screen + + before_each(function() + screen = Screen.new(40, 8) + screen:attach() + screen:set_default_attr_ids({ + [0] = {bold = true, foreground = Screen.colors.Blue}, + [1] = {bold = true, foreground = Screen.colors.SeaGreen}, + [2] = {bold = true, reverse = true}, + [3] = {foreground = Screen.colors.Blue}, + }) + end) + it('prints long messages correctly #20534', function() exec([[ set more @@ -2073,14 +2086,7 @@ describe('API', function() eq('\naaa\n' .. ('a'):rep(5002) .. '\naaa', meths.get_var('out')) end) - it('blank line in message works', function() - local screen = Screen.new(40, 8) - screen:attach() - screen:set_default_attr_ids({ - [0] = {bold = true, foreground = Screen.colors.Blue}, - [1] = {bold = true, foreground = Screen.colors.SeaGreen}, - [2] = {bold = true, reverse = true}, - }) + it('blank line in message', function() feed([[:call nvim_out_write("\na\n")]]) screen:expect{grid=[[ | @@ -2105,6 +2111,20 @@ describe('API', function() {1:Press ENTER or type command to continue}^ | ]]} end) + + it('NUL bytes in message', function() + feed([[:lua vim.api.nvim_out_write('aaa\0bbb\0\0ccc\nddd\0\0\0eee\n')]]) + screen:expect{grid=[[ + | + {0:~ }| + {0:~ }| + {0:~ }| + {2: }| + aaa{3:^@}bbb{3:^@^@}ccc | + ddd{3:^@^@^@}eee | + {1:Press ENTER or type command to continue}^ | + ]]} + end) end) describe('nvim_err_write', function() @@ -2193,6 +2213,20 @@ describe('API', function() ]]) feed('') -- exit the press ENTER screen end) + + it('NUL bytes in message', function() + nvim_async('err_write', 'aaa\0bbb\0\0ccc\nddd\0\0\0eee\n') + screen:expect{grid=[[ + | + {0:~ }| + {0:~ }| + {0:~ }| + {3: }| + {1:aaa^@bbb^@^@ccc} | + {1:ddd^@^@^@eee} | + {2:Press ENTER or type command to continue}^ | + ]]} + end) end) describe('nvim_err_writeln', function()