From 5cb6df3f190feccc8b3e82da2b01a0e01e612a25 Mon Sep 17 00:00:00 2001
From: Nigel Croxon <ncroxon@redhat.com>
Date: Mon, 15 Jul 2024 10:13:46 -0400
Subject: [PATCH 123/157] mdadm: Monitor.c fix coverity issues

Fixing the following coding errors the coverity tools found:

* Event check_return: Calling "fcntl(fd, 2, 1)" without checking
return value. This library function may fail and return an error code.

* Dereferencing "sl", which is known to be "NULL".

* Event fixed_size_dest: You might overrun the 32-character fixed-size
string "devnm" by copying "tmp" without checking the length.

Signed-off-by: Nigel Croxon <ncroxon@redhat.com>
---
 Monitor.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/Monitor.c b/Monitor.c
index 9b016bc3..26c53e13 100644
--- a/Monitor.c
+++ b/Monitor.c
@@ -782,7 +782,9 @@ static int check_array(struct state *st, struct mdstat_ent *mdstat,
 	if (!is_container && !md_array_active(fd))
 		goto disappeared;
 
-	fcntl(fd, F_SETFD, FD_CLOEXEC);
+	if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
+		goto out;
+
 	if (md_get_array_info(fd, &array) < 0)
 		goto disappeared;
 
@@ -997,7 +999,8 @@ static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist)
 				snprintf(st->parent_devnm, MD_NAME_MAX,
 					 "%s", mse->metadata_version + 10);
 				sl = strchr(st->parent_devnm, '/');
-				*sl = 0;
+				if (sl)
+					*sl = 0;
 			} else
 				st->parent_devnm[0] = 0;
 			*statelist = st;
@@ -1261,7 +1264,7 @@ int Wait(char *dev)
 		return 2;
 	}
 
-	strcpy(devnm, tmp);
+	snprintf(devnm, sizeof(devnm), "%s", tmp);
 
 	while(1) {
 		struct mdstat_ent *ms = mdstat_read(1, 0);
@@ -1332,7 +1335,8 @@ int WaitClean(char *dev, int verbose)
 		return 1;
 	}
 
-	strcpy(devnm, fd2devnm(fd));
+	snprintf(devnm, sizeof(devnm), "%s", fd2devnm(fd));
+
 	mdi = sysfs_read(fd, devnm, GET_VERSION|GET_LEVEL|GET_SAFEMODE);
 	if (!mdi) {
 		if (verbose)
-- 
2.41.0