backup_crontab() leaked file descriptors if retval!=0. We now always close
opened file descriptors before returning from the function. We do not do this
before calling exit(3), as all resources are freed by the system anyway.
---
src/crontab.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/src/crontab.c b/src/crontab.c
index 5318e71..fd97b50 100644
--- a/src/crontab.c
+++ b/src/crontab.c
@@ -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;
@@ -587,6 +587,11 @@ static int backup_crontab(const char *crontab_path) {
exit(ERROR_EXIT);
}
+ if (crontab_file)
+ (void) fclose(crontab_file);
+ if (backup_file)
+ (void) fclose(backup_file);
+
if (retval != 0)
return retval;
@@ -594,9 +599,6 @@ static int backup_crontab(const char *crontab_path) {
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;
--
2.30.2
Show replies by date