fix(decoration): fix crash when on_lines decor provider modifies marktree

If a "on_lines" callback changes the structure of the marktree, the
iterator (which is used for an entire window viewport) might now
point to invalid memory. Restore the iterator to the beginning of the
line in this case.

fixes #29484
This commit is contained in:
bfredl
2025-01-03 12:36:36 +01:00
parent fa298fd2f4
commit 33ff546b50
6 changed files with 55 additions and 7 deletions

View File

@@ -303,12 +303,24 @@ static void decor_free_inner(DecorVirtText *vt, uint32_t first_idx)
}
}
/// Check if we are in a callback while drawing, which might invalidate the marktree iterator.
///
/// This should be called whenever a structural modification has been done to a
/// marktree in a public API function (i e any change which adds or deletes marks).
void decor_state_invalidate(buf_T *buf)
{
if (decor_state.win && decor_state.win->w_buffer == buf) {
decor_state.itr_valid = false;
}
}
void decor_check_to_be_deleted(void)
{
assert(!decor_state.running_decor_provider);
decor_free_inner(to_free_virt, to_free_sh);
to_free_virt = NULL;
to_free_sh = DECOR_ID_INVALID;
decor_state.win = NULL;
}
void decor_state_free(DecorState *state)
@@ -447,6 +459,8 @@ bool decor_redraw_start(win_T *wp, int top_row, DecorState *state)
{
buf_T *buf = wp->w_buffer;
state->top_row = top_row;
state->itr_valid = true;
if (!marktree_itr_get_overlap(buf->b_marktree, top_row, 0, state->itr)) {
return false;
}
@@ -489,7 +503,11 @@ bool decor_redraw_line(win_T *wp, int row, DecorState *state)
if (state->row == -1) {
decor_redraw_start(wp, row, state);
} else if (!state->itr_valid) {
marktree_itr_get(wp->w_buffer->b_marktree, row, 0, state->itr);
state->itr_valid = true;
}
state->row = row;
state->col_until = -1;
state->eol_col = -1;