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