Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not leak file descriptors in backup_crontab #184

Merged
merged 1 commit into from
Jun 28, 2024

Conversation

khardix
Copy link
Contributor

@khardix khardix commented Jun 26, 2024

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.

src/crontab.c Outdated
Comment on lines 503 to 504
FILE *crontab_file = NULL;
FILE *backup_file = NULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initialization should not be necessary as we never call fclose with these FILE * variables uninitialized. Also calling fclose on NULL is afaik not allowed so please check whether this isn't the case.

There are missing fclose calls on lines 566 and 593 though, could you please add them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also calling fclose on NULL is afaik not allowed so please check whether this isn't the case.

TIL; I assumed that passing NULL resumed in no-op, but this is apparently not the case according to standard. Which also means that the cleanup section is wrong – if opening of the backup file fails, fclose(backup_file) is called with backup_file == NULL. I'll add guard checks for that.

The initialization should not be necessary as we never call fclose with these FILE * variables uninitialized.

True; I'll drop them then.

There are missing fclose calls on lines 566 and 593 though, could you please add them?

I'll take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are missing fclose calls on lines 566 and 593 though, could you please add them?

Well, the exit() calls there kill the process at that point; any open files will be closed by the OS. Are the calls really necessary?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not critically but then the same thing can be said about the other missing fclose() calls being added by this PR as the crontab process is short lived so missing some fcloses() on error cases would do no real harm.

src/crontab.c Outdated
printf("Backup of %s's previous crontab saved to %s\n", User, backup_path);

return 0;
cleanup:
if (backup_file) (void) fclose(backup_file);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use backup_file != NULL and put the statement on a separate line.

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.
@khardix
Copy link
Contributor Author

khardix commented Jun 28, 2024

Updated with the requested changes.

@t8m t8m merged commit b3a54d0 into cronie-crond:master Jun 28, 2024
4 checks passed
@t8m
Copy link
Member

t8m commented Jun 28, 2024

Merged. Thank you for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants