Skip to content

Commit

Permalink
Do not leak file descriptors in backup_crontab
Browse files Browse the repository at this point in the history
Originally, if anything went wrong during the backup,
the early return caused the crontab_file and possibly backup_file
pointers to leak.

Issue found by static scanner.
  • Loading branch information
khardix committed Jun 26, 2024
1 parent 7417657 commit 1245bc1
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/crontab.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,18 +594,19 @@ static int backup_crontab(const char *crontab_path) {
}

if (retval != 0)
return retval;
goto cleanup;

if (EOF != ch)
while (EOF != (ch = get_char(crontab_file)))
putc(ch, backup_file);

(void) fclose(crontab_file);
(void) fclose(backup_file);

printf("Backup of %s's previous crontab saved to %s\n", User, backup_path);

return 0;
cleanup:
if (backup_file) (void) fclose(backup_file);
(void) fclose(crontab_file);

return retval;
}

static void check_error(const char *msg) {
Expand Down

0 comments on commit 1245bc1

Please sign in to comment.