editing: prevent the pointer for the top row from becoming dangling

When undoing several actions, it is possible for the line at the top
of the screen to be removed, leaving 'edittop' pointing to a structure
that has been freed.  Soon after, 'edittop' is referenced to determine
whether the cursor is offscreen...  Prevent this invalid reference by
stepping 'edittop' one line back in that special case.  This changes
the normal centering behavior of Undo when the cursor goes offscreen,
but... so be it.

When a single node is deleted, it is always possible to step one line
back, because a buffer contains always at least one line (even though
maybe empty), so if the current line could be deleted, there must be
one before it (when at the top of the screen).

This fixes https://savannah.gnu.org/bugs/?60436.

Bug existed since version 2.3.3, commit 60815461,
since undoing does not always center the cursor.
master
Benno Schulenberg 2021-04-22 19:28:34 +02:00
parent cf0820549b
commit 588022ab8c
1 changed files with 3 additions and 0 deletions

View File

@ -99,6 +99,9 @@ void splice_node(linestruct *afterthis, linestruct *newnode)
/* Free the data structures in the given node. */
void delete_node(linestruct *line)
{
/* If the first line on the screen gets deleted, step one back. */
if (line == openfile->edittop)
openfile->edittop = line->prev;
#ifdef ENABLE_WRAPPING
/* If the spill-over line for hard-wrapping is deleted... */
if (line == openfile->spillage_line)