From d49d4f7b5604a24e5e724cb03454b6f31b036383 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Fri, 23 Dec 2016 10:43:15 +0100
Subject: [PATCH] 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.
---
 src/files.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/files.c b/src/files.c
index 2abda50e..b79238e5 100644
--- a/src/files.c
+++ b/src/files.c
@@ -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)
-- 
GitLab