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 545ff3f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/crontab.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,8 @@ static int backup_crontab(const char *crontab_path) {
const char *env_value;
char backup_dir[MAX_FNAME], backup_path[MAX_FNAME];
int ch = '\0';
FILE *crontab_file;
FILE *backup_file;
FILE *crontab_file = NULL;
FILE *backup_file = NULL;
struct stat sb;
int retval = 0;
mode_t old_umask;
Expand Down 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);

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

cleanup:
(void) fclose(crontab_file);
(void) fclose(backup_file);

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

return 0;
return retval;
}

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

0 comments on commit 545ff3f

Please sign in to comment.