nano-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Nano-devel] [PATCH] fix memory leaks when writing out new files


From: Mike Frysinger
Subject: [Nano-devel] [PATCH] fix memory leaks when writing out new files
Date: Thu, 14 Jan 2016 17:50:16 -0500

There's a bunch of return cases where we don't free the new full filename
which leads to leaks when writing out new files.  One way to reproduce:
$ rm -f foo
$ nano foo
<hit enter>
<ctrl+o to save>
<ctrl+x to exit>
-> memory leak
---
 src/files.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/files.c b/src/files.c
index 2daeaf9..cfc89b4 100644
--- a/src/files.c
+++ b/src/files.c
@@ -921,16 +921,20 @@ int open_file(const char *filename, bool newfie, bool 
quiet, FILE **f)
     /* Okay, if we can't stat the path due to a component's
      * permissions, just try the relative one. */
     if (full_filename == NULL || (stat(full_filename, &fileinfo) == -1 &&
-               stat(filename, &fileinfo2) != -1))
+               stat(filename, &fileinfo2) != -1)) {
+       free(full_filename);
        full_filename = mallocstrcpy(NULL, filename);
+    }
 
     if (stat(full_filename, &fileinfo) == -1) {
+       /* All cases below return. */
+       free(full_filename);
+
        /* Well, maybe we can open the file even if the OS says it's
         * not there. */
        if ((fd = open(filename, O_RDONLY)) != -1) {
            if (!quiet)
                statusbar(_("Reading File"));
-           free(full_filename);
            return 0;
        }
 
@@ -944,6 +948,8 @@ int open_file(const char *filename, bool newfie, bool 
quiet, FILE **f)
        return -1;
     } else if (S_ISDIR(fileinfo.st_mode) || S_ISCHR(fileinfo.st_mode) ||
                S_ISBLK(fileinfo.st_mode)) {
+       free(full_filename);
+
        /* Don't open directories, character files, or block files.
         * Sorry, /dev/sndstat! */
        statusbar(S_ISDIR(fileinfo.st_mode) ?
@@ -952,6 +958,7 @@ int open_file(const char *filename, bool newfie, bool 
quiet, FILE **f)
        beep();
        return -1;
     } else if ((fd = open(full_filename, O_RDONLY)) == -1) {
+       free(full_filename);
        statusbar(_("Error reading %s: %s"), filename, strerror(errno));
        beep();
        return -1;
-- 
2.6.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]