diff mbox series

[1/2] Monitor: use devname as char array instead of pointer

Message ID 20220714070211.9941-2-kinga.tanska@intel.com (mailing list archive)
State Mainlined, archived
Headers show
Series String functions fixes in Monitor | expand

Commit Message

Kinga Tanska July 14, 2022, 7:02 a.m. UTC
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>
---
 Monitor.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Comments

Coly Li July 28, 2022, 12:50 p.m. UTC | #1
> 2022年7月14日 15:02,Kinga Tanska <kinga.tanska@intel.com> 写道:
> 
> 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>

This patch looks good to me. I added my Acked-by and submitted to Jes today.

Thanks.

Coly Li

> ---
> Monitor.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/Monitor.c b/Monitor.c
> index 6ca1ebe5..a5b11ae2 100644
> --- 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;
> -- 
> 2.26.2
>
diff mbox series

Patch

diff --git a/Monitor.c b/Monitor.c
index 6ca1ebe5..a5b11ae2 100644
--- 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;