history: search items *can* contain newlines -- encoded NUL bytes

Decode 0x0A bytes to 0x00 when saving the search history, and encode
them again when reading the file back in, to prevent nano from hanging
or aborting when encountering 0x00 on a line by itself.
master
Benno Schulenberg 2016-12-23 10:43:15 +01:00
parent 2fbb71d555
commit d49d4f7b56
1 changed files with 7 additions and 2 deletions

View File

@ -2961,9 +2961,11 @@ void load_history(void)
while ((read = getline(&line, &buf_len, hist)) > 0) { while ((read = getline(&line, &buf_len, hist)) > 0) {
line[--read] = '\0'; line[--read] = '\0';
if (read > 0) if (read > 0) {
/* Encode any embedded NUL as 0x0A. */
unsunder(line, read);
update_history(history, line); update_history(history, line);
else } else
history = &replace_history; history = &replace_history;
} }
@ -2986,6 +2988,9 @@ bool writehist(FILE *hist, const filestruct *head)
for (item = head; item != NULL; item = item->next) { for (item = head; item != NULL; item = item->next) {
size_t length = strlen(item->data); size_t length = strlen(item->data);
/* Decode 0x0A bytes as embedded NULs. */
sunder(item->data);
if (fwrite(item->data, sizeof(char), length, hist) < length) if (fwrite(item->data, sizeof(char), length, hist) < length)
return FALSE; return FALSE;
if (putc('\n', hist) == EOF) if (putc('\n', hist) == EOF)