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.
This commit is contained in:
Benno Schulenberg 2016-12-23 10:43:15 +01:00
parent 2fbb71d555
commit d49d4f7b56

View File

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