[PATCH] Avoid resource leaks
by Kevin Bube
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
1 year, 3 months
[PATCH] Remove unreachable code
by Kevin Bube
6dfc81ea910bf3 introduced a shortcut in allowed(). The first part of the
&& conditionals can never be true since then. Remove the unreachable
code.
---
src/security.c | 13 -------------
1 file changed, 13 deletions(-)
diff --git a/src/security.c b/src/security.c
index d61fb2a..7d0ad9a 100644
--- a/src/security.c
+++ b/src/security.c
@@ -710,7 +710,6 @@ int allowed(const char *username, const char *allow_file,
const char *deny_file) {
FILE *fp;
int isallowed;
- char buf[128];
if (getuid() == 0)
return TRUE;
@@ -718,22 +717,10 @@ int allowed(const char *username, const char *allow_file,
if ((fp = fopen(allow_file, "r")) != NULL) {
isallowed = in_file(username, fp, FALSE);
fclose(fp);
- if ((getuid() == 0) && (!isallowed)) {
- snprintf(buf, sizeof (buf),
- "root used -u for user %s not in cron.allow", username);
- log_it("crontab", getpid(), "warning", buf, 0);
- isallowed = TRUE;
- }
}
else if ((fp = fopen(deny_file, "r")) != NULL) {
isallowed = !in_file(username, fp, FALSE);
fclose(fp);
- if ((getuid() == 0) && (!isallowed)) {
- snprintf(buf, sizeof (buf),
- "root used -u for user %s in cron.deny", username);
- log_it("crontab", getpid(), "warning", buf, 0);
- isallowed = TRUE;
- }
}
#ifdef WITH_AUDIT
if (isallowed == FALSE) {
--
2.30.2
1 year, 3 months