From c84a7697339a43c01f00d2f944e86b3efbb6c93c Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 14 Jul 2021 10:06:04 +0200 Subject: [PATCH] memory: free any allocated strings, also in the emergency code path --- src/nano.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/nano.c b/src/nano.c index 4f053dc8..495df930 100644 --- a/src/nano.c +++ b/src/nano.c @@ -311,15 +311,16 @@ void do_exit(void) /* Save the current buffer under the given name (or under "nano." * for a nameless buffer). If needed, the name is modified to be unique. */ -void emergency_save(char *plainname) +void emergency_save(const char *filename) { + char *plainname, *targetname; bool saved = FALSE; - char *targetname; - if (*plainname == '\0') { - plainname = nrealloc(plainname, 28); + if (*filename == '\0') { + plainname = nmalloc(28); sprintf(plainname, "nano.%u", getpid()); - } + } else + plainname = copy_of(filename); targetname = get_next_filename(plainname, ".save"); @@ -345,6 +346,7 @@ void emergency_save(char *plainname) #endif free(targetname); + free(plainname); } /* Die gracefully -- by restoring the terminal state and saving any buffers