]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
mdadm: Monitor.c fix coverity issues
authorNigel Croxon <ncroxon@redhat.com>
Mon, 15 Jul 2024 14:13:46 +0000 (10:13 -0400)
committerMariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
Tue, 16 Jul 2024 14:18:00 +0000 (16:18 +0200)
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

index 9b016bc3924540920962a51c2bd2edfe1c7ca013..26c53e13e1aa1c7ff74d1913947bb3448809537b 100644 (file)
--- 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)