Plugging a couple of memory leaks.

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5598 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
This commit is contained in:
Benno Schulenberg 2016-01-29 16:58:02 +00:00
parent f8ed6bb099
commit 41f08534d9
2 changed files with 24 additions and 7 deletions

View File

@ -4,6 +4,7 @@
* src/nano.c (main): For second or later files on the command line, * src/nano.c (main): For second or later files on the command line,
only check the position history when option 'positionlog' is set. only check the position history when option 'positionlog' is set.
This fixes a bug that was unconsciously reported by Mike Frysinger. 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> 2016-01-26 Benno Schulenberg <bensberg@justemail.net>
* src/files.c (update_poshistory): Do not add directories to the * src/files.c (update_poshistory): Do not add directories to the

View File

@ -306,7 +306,7 @@ int do_lockfile(const char *filename)
char *lockfilename = charalloc(locknamesize); char *lockfilename = charalloc(locknamesize);
static char lockprog[11], lockuser[17]; static char lockprog[11], lockuser[17];
struct stat fileinfo; struct stat fileinfo;
int lockfd, lockpid; int lockfd, lockpid, retval = -1;
snprintf(lockfilename, locknamesize, "%s/%s%s%s", dirname(namecopy1), snprintf(lockfilename, locknamesize, "%s/%s%s%s", dirname(namecopy1),
locking_prefix, basename(namecopy2), locking_suffix); locking_prefix, basename(namecopy2), locking_suffix);
@ -318,14 +318,16 @@ int do_lockfile(const char *filename)
if (stat(lockfilename, &fileinfo) != -1) { if (stat(lockfilename, &fileinfo) != -1) {
ssize_t readtot = 0; ssize_t readtot = 0;
ssize_t readamt = 0; ssize_t readamt = 0;
char *lockbuf = charalloc(8192); char *lockbuf, *promptstr;
char *promptstr = charalloc(128);
int ans; int ans;
if ((lockfd = open(lockfilename, O_RDONLY)) < 0) { if ((lockfd = open(lockfilename, O_RDONLY)) < 0) {
statusbar(_("Error opening lock file %s: %s"), statusbar(_("Error opening lock file %s: %s"),
lockfilename, strerror(errno)); lockfilename, strerror(errno));
return -1; goto free_the_name;
} }
lockbuf = charalloc(8192);
do { do {
readamt = read(lockfd, &lockbuf[readtot], BUFSIZ); readamt = read(lockfd, &lockbuf[readtot], BUFSIZ);
readtot += readamt; readtot += readamt;
@ -334,26 +336,40 @@ int do_lockfile(const char *filename)
if (readtot < 48) { if (readtot < 48) {
statusbar(_("Error reading lock file %s: Not enough data read"), statusbar(_("Error reading lock file %s: Not enough data read"),
lockfilename); lockfilename);
return -1; free(lockbuf);
goto free_the_name;
} }
strncpy(lockprog, &lockbuf[2], 10); strncpy(lockprog, &lockbuf[2], 10);
lockpid = (unsigned char)lockbuf[25] * 256 + (unsigned char)lockbuf[24]; lockpid = (unsigned char)lockbuf[25] * 256 + (unsigned char)lockbuf[24];
strncpy(lockuser, &lockbuf[28], 16); strncpy(lockuser, &lockbuf[28], 16);
free(lockbuf);
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "lockpid = %d\n", lockpid); fprintf(stderr, "lockpid = %d\n", lockpid);
fprintf(stderr, "program name which created this lock file should be %s\n", lockprog); 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); fprintf(stderr, "user which created this lock file should be %s\n", lockuser);
#endif #endif
promptstr = charalloc(128);
/* TRANSLATORS: The second %s is the name of the user, the third that of the editor. */ /* 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?"), sprintf(promptstr, _("File %s is being edited (by %s with %s, PID %d); continue?"),
filename, lockuser, lockprog, lockpid); filename, lockuser, lockprog, lockpid);
ans = do_yesno_prompt(FALSE, promptstr); ans = do_yesno_prompt(FALSE, promptstr);
free(promptstr);
if (ans < 1) { if (ans < 1) {
blank_statusbar(); 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 */ #endif /* !NANO_TINY */