]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
Monitor: use devname as char array instead of pointer
authorKinga Tanska <kinga.tanska@intel.com>
Thu, 14 Jul 2022 07:02:10 +0000 (09:02 +0200)
committerJes Sorensen <jsorensen@fb.com>
Thu, 28 Jul 2022 21:10:22 +0000 (17:10 -0400)
Device name wasn't filled properly due to incorrect use of strcpy.
Strcpy was used twice. Firstly to fill devname with "/dev/md/"
and then to add chosen name. First strcpy result was overwritten by
second one (as a result <device_name> instead of "/dev/md/<device_name>"
was assigned). This commit changes this implementation to use snprintf
and devname with fixed size.

Signed-off-by: Kinga Tanska <kinga.tanska@intel.com>
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
Monitor.c

index 6ca1ebe55267e5ab2f004735f5438577d4b164ef..a5b11ae25f712b8f269f081c12d8052725b3a4e6 100644 (file)
--- a/Monitor.c
+++ b/Monitor.c
@@ -190,9 +190,11 @@ int Monitor(struct mddev_dev *devlist,
                        if (mdlist->devname[0] == '/')
                                st->devname = xstrdup(mdlist->devname);
                        else {
-                               st->devname = xmalloc(8+strlen(mdlist->devname)+1);
-                               strcpy(strcpy(st->devname, "/dev/md/"),
-                                      mdlist->devname);
+                               /* length of "/dev/md/" + device name + terminating byte */
+                               size_t _len = sizeof("/dev/md/") + strnlen(mdlist->devname, PATH_MAX);
+
+                               st->devname = xcalloc(_len, sizeof(char));
+                               snprintf(st->devname, _len, "/dev/md/%s", mdlist->devname);
                        }
                        if (!is_mddev(mdlist->devname))
                                return 1;