newkey(8): fix 'tmpname' memory leak (always) and input file descriptor leak
when output file cannot be opened PR: 201732 Reported by: David Binderman <dcb314@hotmail.com> MFC after: 1 week
This commit is contained in:
parent
00c4df53b6
commit
245a7edb05
@ -266,11 +266,14 @@ localupdate(char *name, char *filename, u_int op, u_int keylen __unused,
|
|||||||
sprintf(tmpname, "%s.tmp", filename);
|
sprintf(tmpname, "%s.tmp", filename);
|
||||||
rf = fopen(filename, "r");
|
rf = fopen(filename, "r");
|
||||||
if (rf == NULL) {
|
if (rf == NULL) {
|
||||||
return (ERR_READ);
|
err = ERR_READ;
|
||||||
|
goto cleanup;
|
||||||
}
|
}
|
||||||
wf = fopen(tmpname, "w");
|
wf = fopen(tmpname, "w");
|
||||||
if (wf == NULL) {
|
if (wf == NULL) {
|
||||||
return (ERR_WRITE);
|
fclose(rf);
|
||||||
|
err = ERR_WRITE;
|
||||||
|
goto cleanup;
|
||||||
}
|
}
|
||||||
err = -1;
|
err = -1;
|
||||||
while (fgets(line, sizeof (line), rf)) {
|
while (fgets(line, sizeof (line), rf)) {
|
||||||
@ -310,13 +313,18 @@ localupdate(char *name, char *filename, u_int op, u_int keylen __unused,
|
|||||||
fclose(rf);
|
fclose(rf);
|
||||||
if (err == 0) {
|
if (err == 0) {
|
||||||
if (rename(tmpname, filename) < 0) {
|
if (rename(tmpname, filename) < 0) {
|
||||||
return (ERR_DBASE);
|
err = ERR_DBASE;
|
||||||
|
goto cleanup;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (unlink(tmpname) < 0) {
|
if (unlink(tmpname) < 0) {
|
||||||
return (ERR_DBASE);
|
err = ERR_DBASE;
|
||||||
|
goto cleanup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
free(tmpname);
|
||||||
return (err);
|
return (err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user