d3b522
From 4be4813262b3b57a95a5f3ce909d30741aa3ac72 Mon Sep 17 00:00:00 2001
d3b522
From: =?UTF-8?q?Jan=20Stan=C4=9Bk?= <jstanek@redhat.com>
d3b522
Date: Fri, 9 Apr 2021 16:47:33 +0200
d3b522
Subject: [PATCH] Address issues raised by static analysis
d3b522
MIME-Version: 1.0
d3b522
Content-Type: text/plain; charset=UTF-8
d3b522
Content-Transfer-Encoding: 8bit
d3b522
d3b522
Signed-off-by: Jan Staněk <jstanek@redhat.com>
d3b522
---
d3b522
 at.c     | 22 ++++++++++++++++++----
d3b522
 daemon.c | 21 ++++++++++++++-------
d3b522
 2 files changed, 32 insertions(+), 11 deletions(-)
d3b522
d3b522
diff --git a/at.c b/at.c
d3b522
index df55dc9..0c74e2e 100644
d3b522
--- a/at.c
d3b522
+++ b/at.c
d3b522
@@ -545,17 +545,27 @@ writefile(time_t runtimer, char queue)
d3b522
 	return;
d3b522
     }
d3b522
 
d3b522
-    if (fstat(fd, &statbuf) == -1)
d3b522
+    if (fstat(fd, &statbuf) == -1) {
d3b522
+	close(fd);
d3b522
 	return;
d3b522
+    }
d3b522
     if ((statbuf.st_uid != 0) || !S_ISREG(statbuf.st_mode) ||
d3b522
-	(statbuf.st_mode & (S_IWGRP | S_IWOTH)))
d3b522
+	(statbuf.st_mode & (S_IWGRP | S_IWOTH))) {
d3b522
+	close(fd);
d3b522
 	return;
d3b522
+    }
d3b522
 
d3b522
     fp = fdopen(fd, "r");
d3b522
-    if (fp == NULL)
d3b522
+    if (fp == NULL) {
d3b522
+	close(fd);
d3b522
 	return;
d3b522
-    if (fscanf(fp, "%d", &pid) != 1)
d3b522
+    }
d3b522
+    if (fscanf(fp, "%d", &pid) != 1) {
d3b522
+	fclose(fp);
d3b522
 	return;
d3b522
+    } else {
d3b522
+	fclose(fp);
d3b522
+    }
d3b522
 
d3b522
     kill_errno = 0;
d3b522
 
d3b522
@@ -640,6 +650,8 @@ list_jobs(void)
d3b522
 	else
d3b522
 	  printf("%ld\t%s %c\n", jobno, timestr, queue);
d3b522
     }
d3b522
+    closedir(spool);
d3b522
+
d3b522
     PRIV_END
d3b522
 }
d3b522
 
d3b522
@@ -722,6 +734,8 @@ process_jobs(int argc, char **argv, int what)
d3b522
 				putchar(ch);
d3b522
 			    }
d3b522
 			    done = 1;
d3b522
+			    fclose(fp);
d3b522
+			    fp = NULL;
d3b522
 			}
d3b522
 			else {
d3b522
 			    perr("Cannot open %.500s", dirent->d_name);
d3b522
diff --git a/daemon.c b/daemon.c
d3b522
index 4003b56..bc8191e 100644
d3b522
--- a/daemon.c
d3b522
+++ b/daemon.c
d3b522
@@ -122,18 +122,23 @@ daemon_setup()
d3b522
     /* Set up standard daemon environment */
d3b522
     pid_t pid;
d3b522
     mode_t old_umask;
d3b522
-    int fd;
d3b522
+    int fd, devnull;
d3b522
     FILE *fp;
d3b522
 
d3b522
     if (!daemon_debug) {
d3b522
-	close(0);
d3b522
-	close(1);
d3b522
-	close(2);
d3b522
-	if ((open("/dev/null", O_RDWR) != 0) ||
d3b522
-	    (open("/dev/null", O_RDWR) != 1) ||
d3b522
-	    (open("/dev/null", O_RDWR) != 2)) {
d3b522
+	devnull = open("/dev/null", O_RDWR);
d3b522
+	if (devnull == -1) {
d3b522
 	    perr("Error redirecting I/O");
d3b522
 	}
d3b522
+
d3b522
+	if ((dup2(devnull, 0) == -1) ||
d3b522
+	    (dup2(devnull, 1) == -1) ||
d3b522
+	    (dup2(devnull, 2) == -1)) {
d3b522
+	    close(devnull);
d3b522
+	    perr("Error redirecting I/O");
d3b522
+	} else {
d3b522
+	    close(devnull);
d3b522
+	}
d3b522
     }
d3b522
 
d3b522
     if (daemon_foreground)
d3b522
@@ -208,6 +213,8 @@ daemon_setup()
d3b522
     fcntl(fd, F_SETFD, FD_CLOEXEC);
d3b522
     PRIV_END
d3b522
 
d3b522
+    /* See the above comment. */
d3b522
+    /* coverity[leaked_storage: FALSE] */
d3b522
     return;
d3b522
 }
d3b522
 
d3b522
-- 
d3b522
2.31.1
d3b522