From b7eb9a9e941ea5e8bae7ca652fb6f16993eba30c Mon Sep 17 00:00:00 2001
From: Lars Henriksen <LarsHenriksen@get2net.dk>
Date: Wed, 11 Dec 2019 09:40:24 +0100
Subject: Fix empty string in updatestring()

If the update results in an empty string (return value GETSTRING_RET), the
original string remains whereas it should be empty.

Signed-off-by: Lars Henriksen <LarsHenriksen@get2net.dk>
Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
---
 src/getstring.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/getstring.c b/src/getstring.c
index 186598d..526e9ba 100644
--- a/src/getstring.c
+++ b/src/getstring.c
@@ -280,7 +280,13 @@ enum getstr getstring(WINDOW * win, char *str, int l, int x, int y)
 	return st.len == 0 ? GETSTRING_RET : GETSTRING_VALID;
 }
 
-/* Update an already existing string. */
+/*
+ * Safe edit of a dynamically allocated string.
+ * A copy of the original is edited. The internal buffer size
+ * is the only limit on the length of the edited string. After editing
+ * additional memory is allocated for the original if needed. The original
+ * remains intact if editing is interrupted.
+ */
 int updatestring(WINDOW * win, char **str, int x, int y)
 {
 	int len = strlen(*str);
@@ -299,8 +305,8 @@ int updatestring(WINDOW * win, char **str, int x, int y)
 		*str = mem_realloc(*str, len + 1, 1);
 		EXIT_IF(*str == NULL, _("out of memory"));
 		memcpy(*str, buf, len + 1);
-	}
-
+	} else if (ret == GETSTRING_RET)
+		**str = '\0';
 	mem_free(buf);
 	return ret;
 }
-- 
cgit v1.2.3-70-g09d2