[PATCH] mtd: sm_ftl: replace strncpy with memcpy

Rahul Kumar posted 1 patch 4 weeks, 1 day ago
There is a newer version of this series
drivers/mtd/sm_ftl.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] mtd: sm_ftl: replace strncpy with memcpy
Posted by Rahul Kumar 4 weeks, 1 day ago
Replace strncpy with memcpy in sm_attr_show and explicitly add a NUL
terminator after the copy. This aligns with current kernel best practices
as strncpy is deprecated for such use, as explained in
Documentation/process/deprecated.rst. This change does not alter the
functionality or introduce any behavioral changes.

Signed-off-by: Rahul Kumar <rk0006818@gmail.com>
---
 drivers/mtd/sm_ftl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/sm_ftl.c b/drivers/mtd/sm_ftl.c
index abc7b186353f..575e137ce8f3 100644
--- a/drivers/mtd/sm_ftl.c
+++ b/drivers/mtd/sm_ftl.c
@@ -44,7 +44,8 @@ static ssize_t sm_attr_show(struct device *dev, struct device_attribute *attr,
 	struct sm_sysfs_attribute *sm_attr =
 		container_of(attr, struct sm_sysfs_attribute, dev_attr);
 
-	strncpy(buf, sm_attr->data, sm_attr->len);
+	memcpy(buf, sm_attr->data, sm_attr->len);
+	buf[sm_attr->len] = '\0';
 	return sm_attr->len;
 }
 
-- 
2.43.0
Re: [PATCH] mtd: sm_ftl: replace strncpy with memcpy
Posted by Pratyush Yadav 4 weeks, 1 day ago
On Wed, Sep 03 2025, Rahul Kumar wrote:

> Replace strncpy with memcpy in sm_attr_show and explicitly add a NUL
> terminator after the copy. This aligns with current kernel best practices
> as strncpy is deprecated for such use, as explained in
> Documentation/process/deprecated.rst. This change does not alter the
> functionality or introduce any behavioral changes.
>
> Signed-off-by: Rahul Kumar <rk0006818@gmail.com>
> ---
>  drivers/mtd/sm_ftl.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/mtd/sm_ftl.c b/drivers/mtd/sm_ftl.c
> index abc7b186353f..575e137ce8f3 100644
> --- a/drivers/mtd/sm_ftl.c
> +++ b/drivers/mtd/sm_ftl.c
> @@ -44,7 +44,8 @@ static ssize_t sm_attr_show(struct device *dev, struct device_attribute *attr,
>  	struct sm_sysfs_attribute *sm_attr =
>  		container_of(attr, struct sm_sysfs_attribute, dev_attr);
>  
> -	strncpy(buf, sm_attr->data, sm_attr->len);
> +	memcpy(buf, sm_attr->data, sm_attr->len);
> +	buf[sm_attr->len] = '\0';

This writes one more byte than strncpy() (which would stop at
buf[sm_attr->len - 1]). Is this safe?

>  	return sm_attr->len;

Since you write sm_attr->len + 1 bytes now, I think this should also be
updated.

>  }

-- 
Regards,
Pratyush Yadav