]> git.wh0rd.org Git - nano.git/commitdiff
Plugging a couple of memory leaks.
authorBenno Schulenberg <bensberg@justemail.net>
Fri, 29 Jan 2016 16:58:02 +0000 (16:58 +0000)
committerBenno Schulenberg <bensberg@justemail.net>
Fri, 29 Jan 2016 16:58:02 +0000 (16:58 +0000)
git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5598 35c25a1d-7b9e-4130-9fde-d3aeb78583b8

ChangeLog
src/files.c

index 752649747bd57047938727a3d2054ce4b2214ccb..457ba7f356aab4ebd92785287613ae8d9002d1c5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,7 @@
        * src/nano.c (main): For second or later files on the command line,
        only check the position history when option 'positionlog' is set.
        This fixes a bug that was unconsciously reported by Mike Frysinger.
+       * src/files.c (do_lockfile): Plug a couple of memory leaks.
 
 2016-01-26  Benno Schulenberg  <bensberg@justemail.net>
        * src/files.c (update_poshistory): Do not add directories to the
index 0a4b4de8a98dbe3acb4015eff4dbde463966d60c..6df25ba00aa747b64c85f5826fbf9a9fa3aafcc4 100644 (file)
@@ -306,7 +306,7 @@ int do_lockfile(const char *filename)
     char *lockfilename = charalloc(locknamesize);
     static char lockprog[11], lockuser[17];
     struct stat fileinfo;
-    int lockfd, lockpid;
+    int lockfd, lockpid, retval = -1;
 
     snprintf(lockfilename, locknamesize, "%s/%s%s%s", dirname(namecopy1),
                locking_prefix, basename(namecopy2), locking_suffix);
@@ -318,14 +318,16 @@ int do_lockfile(const char *filename)
     if (stat(lockfilename, &fileinfo) != -1) {
        ssize_t readtot = 0;
        ssize_t readamt = 0;
-       char *lockbuf = charalloc(8192);
-       char *promptstr = charalloc(128);
+       char *lockbuf, *promptstr;
        int ans;
+
        if ((lockfd = open(lockfilename, O_RDONLY)) < 0) {
            statusbar(_("Error opening lock file %s: %s"),
                        lockfilename, strerror(errno));
-           return -1;
+           goto free_the_name;
        }
+
+       lockbuf = charalloc(8192);
        do {
            readamt = read(lockfd, &lockbuf[readtot], BUFSIZ);
            readtot += readamt;
@@ -334,26 +336,40 @@ int do_lockfile(const char *filename)
        if (readtot < 48) {
            statusbar(_("Error reading lock file %s: Not enough data read"),
                        lockfilename);
-           return -1;
+           free(lockbuf);
+           goto free_the_name;
        }
+
        strncpy(lockprog, &lockbuf[2], 10);
        lockpid = (unsigned char)lockbuf[25] * 256 + (unsigned char)lockbuf[24];
        strncpy(lockuser, &lockbuf[28], 16);
+       free(lockbuf);
+
 #ifdef DEBUG
        fprintf(stderr, "lockpid = %d\n", lockpid);
        fprintf(stderr, "program name which created this lock file should be %s\n", lockprog);
        fprintf(stderr, "user which created this lock file should be %s\n", lockuser);
 #endif
+       promptstr = charalloc(128);
        /* TRANSLATORS: The second %s is the name of the user, the third that of the editor. */
        sprintf(promptstr, _("File %s is being edited (by %s with %s, PID %d); continue?"),
                        filename, lockuser, lockprog, lockpid);
        ans = do_yesno_prompt(FALSE, promptstr);
+       free(promptstr);
+
        if (ans < 1) {
            blank_statusbar();
-           return -1;
+           goto free_the_name;
        }
     }
-    return write_lockfile(lockfilename, filename, FALSE);
+
+    retval = write_lockfile(lockfilename, filename, FALSE);
+
+  free_the_name:
+    if (retval < 1)
+       free(lockfilename);
+
+    return retval;
 }
 #endif /* !NANO_TINY */