fix(statusline): overwriting stl_items with nvim_eval_statusline() {-item #32265

Problem:
When an evaluation {-item calls `nvim_eval_statusline()`, that nested
call may overwrite the same memory used for `stl_items`.

Solution:
Make `curitem` static and use it to compute an offset to avoid
overwriting `stl_items` in nested calls to `build_stl_str_hl()`. Move
miscellaneous statusline tests into `describe()` block.
This commit is contained in:
luukvbaal 2025-02-03 00:09:43 +01:00 committed by GitHub
parent 77be44563a
commit 87e806186c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 276 additions and 274 deletions

View File

@ -927,6 +927,7 @@ int build_stl_str_hl(win_T *wp, char *out, size_t outlen, char *fmt, OptIndex op
static stl_hlrec_t *stl_hltab = NULL; static stl_hlrec_t *stl_hltab = NULL;
static StlClickRecord *stl_tabtab = NULL; static StlClickRecord *stl_tabtab = NULL;
static int *stl_separator_locations = NULL; static int *stl_separator_locations = NULL;
static int curitem = 0;
#define TMPLEN 70 #define TMPLEN 70
char buf_tmp[TMPLEN]; char buf_tmp[TMPLEN];
@ -1013,7 +1014,11 @@ int build_stl_str_hl(win_T *wp, char *out, size_t outlen, char *fmt, OptIndex op
int groupdepth = 0; int groupdepth = 0;
int evaldepth = 0; int evaldepth = 0;
int curitem = 0; // nvim_eval_statusline() can be called from inside a {-expression item so
// this may be a recursive call. Keep track of the start index into "stl_items".
// During post-processing only treat items filled in a certain recursion level.
int evalstart = curitem;
bool prevchar_isflag = true; bool prevchar_isflag = true;
bool prevchar_isitem = false; bool prevchar_isitem = false;
@ -1949,7 +1954,9 @@ stcsign:
} }
*out_p = NUL; *out_p = NUL;
int itemcnt = curitem; // Subtract offset from `itemcnt` and restore `curitem` to previous recursion level.
int itemcnt = curitem - evalstart;
curitem = evalstart;
// Free the format buffer if we allocated it internally // Free the format buffer if we allocated it internally
if (usefmt != fmt) { if (usefmt != fmt) {
@ -1975,7 +1982,7 @@ stcsign:
trunc_p = stl_items[0].start; trunc_p = stl_items[0].start;
item_idx = 0; item_idx = 0;
for (int i = 0; i < itemcnt; i++) { for (int i = evalstart; i < itemcnt + evalstart; i++) {
if (stl_items[i].type == Trunc) { if (stl_items[i].type == Trunc) {
// Truncate at %< stl_items. // Truncate at %< stl_items.
trunc_p = stl_items[i].start; trunc_p = stl_items[i].start;
@ -2005,9 +2012,9 @@ stcsign:
// Ignore any items in the statusline that occur after // Ignore any items in the statusline that occur after
// the truncation point // the truncation point
for (int i = 0; i < itemcnt; i++) { for (int i = evalstart; i < itemcnt + evalstart; i++) {
if (stl_items[i].start > trunc_p) { if (stl_items[i].start > trunc_p) {
for (int j = i; j < itemcnt; j++) { for (int j = i; j < itemcnt + evalstart; j++) {
if (stl_items[j].type == ClickFunc) { if (stl_items[j].type == ClickFunc) {
XFREE_CLEAR(stl_items[j].cmd); XFREE_CLEAR(stl_items[j].cmd);
} }
@ -2046,7 +2053,7 @@ stcsign:
// the truncation marker `<` is not counted. // the truncation marker `<` is not counted.
int item_offset = trunc_len - 1; int item_offset = trunc_len - 1;
for (int i = item_idx; i < itemcnt; i++) { for (int i = item_idx; i < itemcnt + evalstart; i++) {
// Items starting at or after the end of the truncated section need // Items starting at or after the end of the truncated section need
// to be moved backwards. // to be moved backwards.
if (stl_items[i].start >= trunc_end_p) { if (stl_items[i].start >= trunc_end_p) {
@ -2079,7 +2086,7 @@ stcsign:
// Find how many separators there are, which we will use when // Find how many separators there are, which we will use when
// figuring out how many groups there are. // figuring out how many groups there are.
int num_separators = 0; int num_separators = 0;
for (int i = 0; i < itemcnt; i++) { for (int i = evalstart; i < itemcnt + evalstart; i++) {
if (stl_items[i].type == Separate) { if (stl_items[i].type == Separate) {
// Create an array of the start location for each separator mark. // Create an array of the start location for each separator mark.
stl_separator_locations[num_separators] = i; stl_separator_locations[num_separators] = i;
@ -2104,7 +2111,7 @@ stcsign:
} }
for (int item_idx = stl_separator_locations[l] + 1; for (int item_idx = stl_separator_locations[l] + 1;
item_idx < itemcnt; item_idx < itemcnt + evalstart;
item_idx++) { item_idx++) {
stl_items[item_idx].start += dislocation; stl_items[item_idx].start += dislocation;
} }
@ -2118,7 +2125,7 @@ stcsign:
if (hltab != NULL) { if (hltab != NULL) {
*hltab = stl_hltab; *hltab = stl_hltab;
stl_hlrec_t *sp = stl_hltab; stl_hlrec_t *sp = stl_hltab;
for (int l = 0; l < itemcnt; l++) { for (int l = evalstart; l < itemcnt + evalstart; l++) {
if (stl_items[l].type == Highlight if (stl_items[l].type == Highlight
|| stl_items[l].type == HighlightFold || stl_items[l].type == HighlightSign) { || stl_items[l].type == HighlightFold || stl_items[l].type == HighlightSign) {
sp->start = stl_items[l].start; sp->start = stl_items[l].start;
@ -2139,7 +2146,7 @@ stcsign:
if (tabtab != NULL) { if (tabtab != NULL) {
*tabtab = stl_tabtab; *tabtab = stl_tabtab;
StlClickRecord *cur_tab_rec = stl_tabtab; StlClickRecord *cur_tab_rec = stl_tabtab;
for (int l = 0; l < itemcnt; l++) { for (int l = evalstart; l < itemcnt + evalstart; l++) {
if (stl_items[l].type == TabPage) { if (stl_items[l].type == TabPage) {
cur_tab_rec->start = stl_items[l].start; cur_tab_rec->start = stl_items[l].start;
if (stl_items[l].minwid == 0) { if (stl_items[l].minwid == 0) {

View File

@ -507,268 +507,263 @@ describe('global statusline', function()
end) end)
end) end)
it('statusline does not crash if it has Arabic characters #19447', function() describe('statusline', function()
clear() local screen
api.nvim_set_option_value('statusline', 'غً', {}) before_each(function()
api.nvim_set_option_value('laststatus', 2, {}) clear()
command('redraw!') screen = Screen.new(40, 8)
assert_alive() screen:add_extra_attr_ids {
end) [100] = { bold = true, reverse = true, foreground = Screen.colors.Blue },
[101] = { reverse = true, bold = true, foreground = Screen.colors.SlateBlue },
it('statusline is redrawn with :resize from <Cmd> mapping #19629', function() }
clear() end)
local screen = Screen.new(40, 8)
exec([[ it('does not crash if it has Arabic characters #19447', function()
set laststatus=2 api.nvim_set_option_value('statusline', 'غً', {})
nnoremap <Up> <cmd>resize -1<CR> api.nvim_set_option_value('laststatus', 2, {})
nnoremap <Down> <cmd>resize +1<CR> command('redraw!')
]]) assert_alive()
feed('<Up>') end)
screen:expect([[
^ | it('is redrawn with :resize from <Cmd> mapping #19629', function()
{1:~ }|*4 exec([[
{3:[No Name] }| set laststatus=2
|*2 nnoremap <Up> <cmd>resize -1<CR>
]]) nnoremap <Down> <cmd>resize +1<CR>
feed('<Down>') ]])
screen:expect([[ feed('<Up>')
^ | screen:expect([[
{1:~ }|*5 ^ |
{3:[No Name] }| {1:~ }|*4
| {3:[No Name] }|
]]) |*2
end) ]])
feed('<Down>')
it('showcmdloc=statusline does not show if statusline is too narrow', function() screen:expect([[
clear() ^ |
local screen = Screen.new(40, 8) {1:~ }|*5
command('set showcmd') {3:[No Name] }|
command('set showcmdloc=statusline') |
command('1vsplit') ]])
screen:expect([[ end)
^ |
{1:~}{1:~ }|*5 it('does not contain showmcd with showcmdloc=statusline when too narrow', function()
{3:< }{2:[No Name] }| command('set showcmd')
| command('set showcmdloc=statusline')
]]) command('1vsplit')
feed('1234') screen:expect([[
screen:expect_unchanged() ^ |
end) {1:~}{1:~ }|*5
{3:< }{2:[No Name] }|
it('K_EVENT does not trigger a statusline redraw unnecessarily', function() |
clear() ]])
local _ = Screen.new(40, 8) feed('1234')
-- does not redraw on vim.schedule (#17937) screen:expect_unchanged()
command([[ end)
set laststatus=2
let g:counter = 0 it('does not redraw unnecessarily after K_EVENT', function()
func Status() -- does not redraw on vim.schedule (#17937)
let g:counter += 1 command([[
lua vim.schedule(function() end) set laststatus=2
return g:counter let g:counter = 0
endfunc func Status()
set statusline=%!Status() let g:counter += 1
]]) lua vim.schedule(function() end)
sleep(50) return g:counter
eq(1, eval('g:counter < 50'), 'g:counter=' .. eval('g:counter')) endfunc
-- also in insert mode set statusline=%!Status()
feed('i') ]])
sleep(50) sleep(50)
eq(1, eval('g:counter < 50'), 'g:counter=' .. eval('g:counter')) eq(1, eval('g:counter < 50'), 'g:counter=' .. eval('g:counter'))
-- does not redraw on timer call (#14303) -- also in insert mode
command([[ feed('i')
let g:counter = 0 sleep(50)
func Timer(timer) eq(1, eval('g:counter < 50'), 'g:counter=' .. eval('g:counter'))
endfunc -- does not redraw on timer call (#14303)
call timer_start(1, 'Timer', {'repeat': 100}) command([[
]]) let g:counter = 0
sleep(50) func Timer(timer)
eq(1, eval('g:counter < 50'), 'g:counter=' .. eval('g:counter')) endfunc
end) call timer_start(1, 'Timer', {'repeat': 100})
]])
it('statusline is redrawn on various state changes', function() sleep(50)
clear() eq(1, eval('g:counter < 50'), 'g:counter=' .. eval('g:counter'))
local screen = Screen.new(40, 4) end)
-- recording state change #22683 it('is redrawn on various state changes', function()
command('set ls=2 stl=%{repeat(reg_recording(),5)}') -- recording state change #22683
screen:expect([[ command('set ls=2 stl=%{repeat(reg_recording(),5)}')
^ | local s1 = [[
{1:~ }| ^ |
{3: }| {1:~ }|*5
| {3: }|
]]) |
feed('qQ') ]]
screen:expect([[ screen:expect(s1)
^ | feed('qQ')
{1:~ }| screen:expect([[
{3:QQQQQ }| ^ |
{5:recording @Q} | {1:~ }|*5
]]) {3:QQQQQ }|
feed('q') {5:recording @Q} |
screen:expect([[ ]])
^ | feed('q')
{1:~ }| screen:expect(s1)
{3: }|
| -- Visual mode change #23932
]]) command('set ls=2 stl=%{mode(1)}')
local s2 = [[
-- Visual mode change #23932 ^ |
command('set ls=2 stl=%{mode(1)}') {1:~ }|*5
screen:expect([[ {3:n }|
^ | |
{1:~ }| ]]
{3:n }| screen:expect(s2)
| feed('v')
]]) screen:expect([[
feed('v') ^ |
screen:expect([[ {1:~ }|*5
^ | {3:v }|
{1:~ }| {5:-- VISUAL --} |
{3:v }| ]])
{5:-- VISUAL --} | feed('V')
]]) screen:expect([[
feed('V') ^ |
screen:expect([[ {1:~ }|*5
^ | {3:V }|
{1:~ }| {5:-- VISUAL LINE --} |
{3:V }| ]])
{5:-- VISUAL LINE --} | feed('<C-V>')
]]) screen:expect([[
feed('<C-V>') ^ |
screen:expect([[ {1:~ }|*5
^ | {3:^V }|
{1:~ }| {5:-- VISUAL BLOCK --} |
{3:^V }| ]])
{5:-- VISUAL BLOCK --} | feed('<Esc>')
]]) screen:expect(s2)
feed('<Esc>') end)
screen:expect([[
^ | it('ruler is redrawn in cmdline with redrawstatus #22804', function()
{1:~ }| command([[
{3:n }| let g:n = 'initial value'
| set ls=1 ru ruf=%{g:n}
]]) redraw
end) let g:n = 'other value'
redrawstatus
it('ruler is redrawn in cmdline with redrawstatus #22804', function() ]])
clear() screen:expect([[
local screen = Screen.new(40, 2) ^ |
command([[ {1:~ }|*6
let g:n = 'initial value' other value |
set ls=1 ru ruf=%{g:n} ]])
redraw end)
let g:n = 'other value'
redrawstatus it('hidden moves ruler to cmdline', function()
]]) -- Use long ruler to check 'ruler' with 'rulerformat' set has correct width.
screen:expect([[ command [[
^ | set ruler rulerformat=%{winnr()}longlonglong ls=0 winwidth=10
other value | split
]]) wincmd b
end) vsplit
wincmd t
it('shows correct ruler in cmdline with no statusline', function() wincmd |
clear() mode
local screen = Screen.new(30, 8) ]]
-- Use long ruler to check 'ruler' with 'rulerformat' set has correct width. -- Window 1 is current. It has a statusline, so cmdline should show the
command [[ -- last window's ruler, which has no statusline.
set ruler rulerformat=%{winnr()}longlonglong ls=0 winwidth=10 command '1wincmd w'
split screen:expect([[
wincmd b ^ |
vsplit {1:~ }|*2
wincmd t {3:[No Name] 1longlonglong }|
wincmd | |
mode {1:~ }{1:~ }|*2
]] 3longlonglong |
-- Window 1 is current. It has a statusline, so cmdline should show the ]])
-- last window's ruler, which has no statusline. -- Window 2 is current. It has no statusline, so cmdline should show its
command '1wincmd w' -- ruler instead.
screen:expect [[ command '2wincmd w'
^ | screen:expect([[
{1:~ }|*2 |
{3:[No Name] 1longlonglong }| {1:~ }|*2
| {2:[No Name] 1longlonglong }|
{1:~ }{1:~ }|*2 ^ |
3longlonglong | {1:~ }{1:~ }|*2
]] 2longlonglong |
-- Window 2 is current. It has no statusline, so cmdline should show its ]])
-- ruler instead. -- Window 3 is current. Cmdline should again show its ruler.
command '2wincmd w' command '3wincmd w'
screen:expect [[ screen:expect([[
| |
{1:~ }|*2 {1:~ }|*2
{2:[No Name] 1longlonglong }| {2:[No Name] 1longlonglong }|
^ | ^ |
{1:~ }{1:~ }|*2 {1:~ }{1:~ }|*2
2longlonglong | 3longlonglong |
]] ]])
-- Window 3 is current. Cmdline should again show its ruler. end)
command '3wincmd w'
screen:expect [[ it('uses "stl" and "stlnc" fillchars even if they are the same #19803', function()
| command('hi clear StatusLine')
{1:~ }|*2 command('hi clear StatusLineNC')
{2:[No Name] 1longlonglong }| command('vsplit')
^ | screen:expect([[
{1:~ }{1:~ }|*2 ^ |
3longlonglong | {1:~ }{1:~ }|*5
]] [No Name] [No Name] |
end) |
]])
it('uses "stl" and "stlnc" fillchars even if they are the same #19803', function() end)
clear()
local screen = Screen.new(53, 4) it('showcmdloc=statusline works with vertical splits', function()
command('hi clear StatusLine') command('rightbelow vsplit')
command('hi clear StatusLineNC') command('set showcmd showcmdloc=statusline')
command('vsplit') feed('1234')
screen:expect { screen:expect([[
grid = [[ ^ |
^ | {1:~ }{1:~ }|*5
{1:~ }{1:~ }| {2:[No Name] }{3:[No Name] 1234 }|
[No Name] [No Name] | |
| ]])
]], feed('<Esc>')
} command('set laststatus=3')
end) feed('1234')
screen:expect([[
it('showcmdloc=statusline works with vertical splits', function() ^ |
clear() {1:~ }{1:~ }|*5
local screen = Screen.new(53, 4) {3:[No Name] 1234 }|
command('rightbelow vsplit') |
command('set showcmd showcmdloc=statusline') ]])
feed('1234') end)
screen:expect([[
^ | it('keymap is shown with vertical splits #27269', function()
{1:~ }{1:~ }| command('setlocal keymap=dvorak')
{2:[No Name] }{3:[No Name] 1234 }| command('rightbelow vsplit')
| screen:expect([[
]]) ^ |
feed('<Esc>') {1:~ }{1:~ }|*5
command('set laststatus=3') {2:[No Name] <en-dv> }{3:[No Name] <en-dv> }|
feed('1234') |
screen:expect([[ ]])
^ |
{1:~ }{1:~ }| command('set laststatus=3')
{3:[No Name] 1234 }| screen:expect([[
| ^ |
]]) {1:~ }{1:~ }|*5
end) {3:[No Name] <en-dv> }|
|
it('keymap is shown with vertical splits #27269', function() ]])
clear() end)
local screen = Screen.new(53, 4)
command('setlocal keymap=dvorak') it("nested call from nvim_eval_statusline() doesn't overwrite items #32259", function()
command('rightbelow vsplit') exec_lua('vim.o.laststatus = 2')
screen:expect([[ exec_lua([[vim.o.statusline = '%#Special#B:%{nvim_eval_statusline("%f", []).str}']])
^ | screen:expect([[
{1:~ }{1:~ }| ^ |
{2:[No Name] <en-dv> }{3:[No Name] <en-dv> }| {1:~ }|*5
| {101:B:[No Name] }|
]]) |
command('set laststatus=3') ]])
screen:expect([[ end)
^ |
{1:~ }{1:~ }|
{3:[No Name] <en-dv> }|
|
]])
end) end)