Freeing the full filename in all cases.

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

Patch by Mike Frysinger.


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5563 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
This commit is contained in:
Benno Schulenberg 2016-01-15 13:17:44 +00:00
parent 63370954bd
commit 0ee7729666
2 changed files with 10 additions and 2 deletions

View File

@ -1,3 +1,6 @@
2016-01-15 Mike Frysinger <vapier@gentoo.org>
* src/files.c (open_file): Free the full filename in all cases.
2016-01-14 Benno Schulenberg <bensberg@justemail.net> 2016-01-14 Benno Schulenberg <bensberg@justemail.net>
* doc/nanorc.sample.in: Remove a reference to an obsolete file. * doc/nanorc.sample.in: Remove a reference to an obsolete file.
Reported by Mike Frysinger. Reported by Mike Frysinger.

View File

@ -922,15 +922,17 @@ int open_file(const char *filename, bool newfie, bool quiet, FILE **f)
* permissions, just try the relative one. */ * permissions, just try the relative one. */
if (full_filename == NULL || (stat(full_filename, &fileinfo) == -1 && if (full_filename == NULL || (stat(full_filename, &fileinfo) == -1 &&
stat(filename, &fileinfo2) != -1)) stat(filename, &fileinfo2) != -1))
full_filename = mallocstrcpy(NULL, filename); full_filename = mallocstrcpy(full_filename, filename);
if (stat(full_filename, &fileinfo) == -1) { 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 /* Well, maybe we can open the file even if the OS says it's
* not there. */ * not there. */
if ((fd = open(filename, O_RDONLY)) != -1) { if ((fd = open(filename, O_RDONLY)) != -1) {
if (!quiet) if (!quiet)
statusbar(_("Reading File")); statusbar(_("Reading File"));
free(full_filename);
return 0; return 0;
} }
@ -944,6 +946,8 @@ int open_file(const char *filename, bool newfie, bool quiet, FILE **f)
return -1; return -1;
} else if (S_ISDIR(fileinfo.st_mode) || S_ISCHR(fileinfo.st_mode) || } else if (S_ISDIR(fileinfo.st_mode) || S_ISCHR(fileinfo.st_mode) ||
S_ISBLK(fileinfo.st_mode)) { S_ISBLK(fileinfo.st_mode)) {
free(full_filename);
/* Don't open directories, character files, or block files. /* Don't open directories, character files, or block files.
* Sorry, /dev/sndstat! */ * Sorry, /dev/sndstat! */
statusbar(S_ISDIR(fileinfo.st_mode) ? statusbar(S_ISDIR(fileinfo.st_mode) ?
@ -952,6 +956,7 @@ int open_file(const char *filename, bool newfie, bool quiet, FILE **f)
beep(); beep();
return -1; return -1;
} else if ((fd = open(full_filename, O_RDONLY)) == -1) { } else if ((fd = open(full_filename, O_RDONLY)) == -1) {
free(full_filename);
statusbar(_("Error reading %s: %s"), filename, strerror(errno)); statusbar(_("Error reading %s: %s"), filename, strerror(errno));
beep(); beep();
return -1; return -1;