Blame SOURCES/0001-Address-issues-found-by-coverity-scan.patch

203597
From 09afe49c73cb495f32b96dce32728352c46ba865 Mon Sep 17 00:00:00 2001
203597
From: =?UTF-8?q?Jan=20Stan=C4=9Bk?= <jstanek@redhat.com>
203597
Date: Thu, 29 Apr 2021 16:03:05 +0200
203597
Subject: [PATCH] Address issues found by coverity scan
203597
MIME-Version: 1.0
203597
Content-Type: text/plain; charset=UTF-8
203597
Content-Transfer-Encoding: 8bit
203597
203597
Signed-off-by: Jan Staněk <jstanek@redhat.com>
203597
---
203597
 anacron/main.c   | 8 ++++++--
203597
 anacron/runjob.c | 2 ++
203597
 src/crontab.c    | 1 +
203597
 src/database.c   | 3 ++-
203597
 src/pw_dup.c     | 1 +
203597
 5 files changed, 12 insertions(+), 3 deletions(-)
203597
203597
diff --git a/anacron/main.c b/anacron/main.c
203597
index d092970..65f8fed 100644
203597
--- a/anacron/main.c
203597
+++ b/anacron/main.c
203597
@@ -44,8 +44,8 @@ int day_now;
203597
 int year, month, day_of_month;                 /* date anacron started */
203597
 
203597
 char *program_name;
203597
-char *anacrontab;
203597
-char *spooldir;
203597
+char *anacrontab = NULL;
203597
+char *spooldir = NULL;
203597
 int serialize, force, update_only, now,
203597
     no_daemon, quiet, testing_only;            /* command-line options */
203597
 char **job_args;                       	       /* vector of "job" command-line arguments */
203597
@@ -128,12 +128,14 @@ parse_opts(int argc, char *argv[])
203597
 	    quiet = 1;
203597
 	    break;
203597
 	case 't':
203597
+	    free(anacrontab);
203597
 	    anacrontab = strdup(optarg);
203597
 	    break;
203597
 	case 'T':
203597
 	    testing_only = 1;
203597
 	    break;
203597
 	case 'S':
203597
+	    free(spooldir);
203597
 	    spooldir = strdup(optarg);
203597
 	    break;
203597
 	case 'V':
203597
@@ -208,9 +210,11 @@ go_background(void)
203597
     /* stdin is already closed */
203597
 
203597
     if (fclose(stdout)) die_e("Can't close stdout");
203597
+    /* coverity[leaked_handle] – fd 1 closed automatically */
203597
     xopen(1, "/dev/null", O_WRONLY);
203597
 
203597
     if (fclose(stderr)) die_e("Can't close stderr");
203597
+    /* coverity[leaked_handle] – fd 2 closed automatically */
203597
     xopen(2, "/dev/null", O_WRONLY);
203597
 
203597
     pid = xfork();
203597
diff --git a/anacron/runjob.c b/anacron/runjob.c
203597
index 341351f..04d6904 100644
203597
--- a/anacron/runjob.c
203597
+++ b/anacron/runjob.c
203597
@@ -237,7 +237,9 @@ launch_mailer(job_rec *jr)
203597
 	xcloselog();
203597
 
203597
 	/* Ensure stdout/stderr are sane before exec-ing sendmail */
203597
+	/* coverity[leaked_handle] – STDOUT closed automatically */
203597
 	xclose(STDOUT_FILENO); xopen(STDOUT_FILENO, "/dev/null", O_WRONLY);
203597
+	/* coverity[leaked_handle] – STDERR closed automatically */
203597
 	xclose(STDERR_FILENO); xopen(STDERR_FILENO, "/dev/null", O_WRONLY);
203597
 	xclose(jr->output_fd);
203597
 
203597
diff --git a/src/crontab.c b/src/crontab.c
203597
index 240c112..41c8984 100644
203597
--- a/src/crontab.c
203597
+++ b/src/crontab.c
203597
@@ -872,6 +872,7 @@ static int replace_cmd(void) {
203597
 
203597
 	if ((error = check_syntax(tmp)) < 0) {
203597
 		fprintf(stderr, "Invalid crontab file, can't install.\n");
203597
+		fclose(tmp);
203597
 		goto done;
203597
 	}
203597
 
203597
diff --git a/src/database.c b/src/database.c
203597
index c1e4593..bff0256 100644
203597
--- a/src/database.c
203597
+++ b/src/database.c
203597
@@ -559,7 +559,8 @@ int load_database(cron_db * old_db) {
203597
 			if (not_a_crontab(dp))
203597
 				continue;
203597
 
203597
-			strncpy(fname, dp->d_name, NAME_MAX + 1);
203597
+			strncpy(fname, dp->d_name, NAME_MAX);
203597
+			fname[NAME_MAX] = '\0';
203597
 
203597
 			if (!glue_strings(tabname, sizeof tabname, SPOOL_DIR, fname, '/'))
203597
 				continue;	/* XXX log? */
203597
diff --git a/src/pw_dup.c b/src/pw_dup.c
203597
index ea787cd..c6f7b00 100644
203597
--- a/src/pw_dup.c
203597
+++ b/src/pw_dup.c
203597
@@ -121,6 +121,7 @@ pw_dup(const struct passwd *pw) {
203597
 		cp += ssize;
203597
 	}
203597
 
203597
+	/* cppcheck-suppress[memleak symbolName=cp] memory originally pointed to by cp returned via newpw */
203597
 	return (newpw);
203597
 }
203597
 
203597
-- 
203597
2.31.1
203597