2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 09:57:41 +00:00

journal_fdopen: free journal_path and close journal before setting

Fixes a potential resource leak that currently cannot happen.
Quiets a warning from Infer.
This commit is contained in:
Todd C. Miller 2022-11-22 11:15:21 -07:00
parent a37ecb2666
commit a0d9963fe6

View File

@ -70,6 +70,7 @@ journal_fdopen(int fd, const char *journal_path,
{ {
debug_decl(journal_fdopen, SUDO_DEBUG_UTIL); debug_decl(journal_fdopen, SUDO_DEBUG_UTIL);
free(closure->journal_path);
closure->journal_path = strdup(journal_path); closure->journal_path = strdup(journal_path);
if (closure->journal_path == NULL) { if (closure->journal_path == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
@ -77,6 +78,8 @@ journal_fdopen(int fd, const char *journal_path,
} }
/* Defer fdopen() until last--it cannot be undone. */ /* Defer fdopen() until last--it cannot be undone. */
if (closure->journal != NULL)
fclose(closure->journal);
if ((closure->journal = fdopen(fd, "r+")) == NULL) { if ((closure->journal = fdopen(fd, "r+")) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO, sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"unable to fdopen journal file %s", journal_path); "unable to fdopen journal file %s", journal_path);