nano

nano with my custom patches
git clone git://bsandro.tech/nano
Log | Files | Refs | README | LICENSE

commit c695d49a866f4c1a1a7a237d0a57970fb89b2801
parent 3cd3ba0397bec9ce1ccc52e42b32557645bccdc7
Author: Benno Schulenberg <bensberg@telfort.nl>
Date:   Sat, 13 Apr 2024 12:10:22 +0200

editing: adjust the mark before trimming redundant blanks

When an entirely blank line is trimmed (when --autoindent is active and
Enter is pressed while the cursor is at the end of the current indent),
the mark needs be adjusted *before* 'current_x' is zeroed, otherwise the
mark gets moved too much to the right, which causes the region to become
bigger than what the user intended, or leads to accessing unallocated
memory.

This fixes https://savannah.gnu.org/bugs/?65586.
The issue was reported by `correctmost`.

Bug existed since version 2.8.1, commit 005ee8ed.

Diffstat:
Msrc/text.c | 14+++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/text.c b/src/text.c @@ -878,6 +878,13 @@ void do_enter(void) strcpy(&newnode->data[extra], openfile->current->data + openfile->current_x); #ifndef NANO_TINY + /* Adjust the mark if it is on the current line after the cursor. */ + if (openfile->mark == openfile->current && + openfile->mark_x > openfile->current_x) { + openfile->mark = newnode; + openfile->mark_x += extra - openfile->current_x; + } + if (ISSET(AUTOINDENT)) { /* Copy the whitespace from the sample line to the new one. */ strncpy(newnode->data, sampleline->data, extra); @@ -892,13 +899,6 @@ void do_enter(void) #ifndef NANO_TINY add_undo(ENTER, NULL); - - /* Adjust the mark if it was on the current line after the cursor. */ - if (openfile->mark == openfile->current && - openfile->mark_x > openfile->current_x) { - openfile->mark = newnode; - openfile->mark_x += extra - openfile->current_x; - } #endif /* Insert the newly created line after the current one and renumber. */