summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLars Henriksen <LarsHenriksen@get2net.dk>2019-12-11 09:40:24 +0100
committerLukas Fleischer <lfleischer@calcurse.org>2019-12-23 13:16:39 -0500
commitb7eb9a9e941ea5e8bae7ca652fb6f16993eba30c (patch)
tree76c35cca7891a8182a8adf2b13071af083a059b3 /src
parent9befae484d88aacf7057b15fa9ae339fb7c43418 (diff)
downloadcalcurse-b7eb9a9e941ea5e8bae7ca652fb6f16993eba30c.tar.gz
calcurse-b7eb9a9e941ea5e8bae7ca652fb6f16993eba30c.zip
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>
Diffstat (limited to 'src')
-rw-r--r--src/getstring.c12
1 files 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;
}