input: make bracketed paste more robust against loss of closing sequence

A bracketed paste should be one large batch (or several batches)
of bytes, not single bytes coming in one by one.  When the latter
happens, assume the end-of-paste sequence got swallowed somehow,
stop the bracketed paste and display a warning, while discarding
the first byte that came in alone.

This change was inspired by this report from Doug Smythies:
  https://lists.gnu.org/archive/html/nano-devel/2025-02/msg00001.html
This commit is contained in:
Benno Schulenberg 2025-02-25 17:24:25 +01:00
parent 555a987844
commit 41c1b9623e
2 changed files with 12 additions and 5 deletions

View File

@ -1444,9 +1444,13 @@ void suck_up_input_and_paste_it(void)
cutbuffer = line;
while (bracketed_paste) {
size_t were_waiting = waiting_keycodes();
int input = get_kbinput(midwin, BLIND);
if (input == '\r' || input == '\n') {
/* If key codes come singly, something is wrong. */
if (were_waiting == 0 && waiting_keycodes() == 0)
break;
else if (input == '\r' || input == '\n') {
line->next = make_new_node(line);
line = line->next;
line->data = copy_of("");
@ -1457,7 +1461,7 @@ void suck_up_input_and_paste_it(void)
line->data[index++] = (char)input;
line->data[index] = '\0';
} else if (input != BRACKETED_PASTE_MARKER)
beep();
break;
}
if (ISSET(VIEW_MODE))
@ -1465,6 +1469,11 @@ void suck_up_input_and_paste_it(void)
else
paste_text();
if (bracketed_paste) {
statusline(ALERT, _("Flawed paste"));
bracketed_paste = FALSE;
}
free_lines(cutbuffer);
cutbuffer = was_cutbuffer;
}

View File

@ -742,10 +742,8 @@ int convert_CSI_sequence(const int *seq, size_t length, int *consumed)
}
if (trailer != '~') {
/* Broken -- assume a truncated end-of-paste sequence. */
bracketed_paste = FALSE;
*consumed = length;
return ERR;
return FOREIGN_SEQUENCE;
}
if (seq[2] == '0') {