Skip to contentSkip to conversation

mtd: sm_ftl: use strscpy() in sm_attr_show()

PATCH In review Dharanitharan R 2 participants 2 messages Dec 6, 2025 – Dec 16, 2025 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: sm_ftl: use strscpy() in sm_attr_show()

In sm_ftl.c, the sm_attr_show() function currently copies attribute data using:

    strncpy(buf, sm_attr->data, sm_attr->len);
    return sm_attr->len;

Using strncpy() can be unsafe because it does not guarantee a NUL terminator if the source length equals the buffer size. Although sm_attr->data comes from internal structures and is NUL-terminated, it is cleaner and safer to use strscpy(), which guarantees NUL termination and avoids zero-padding.

The destination buffer is PAGE_SIZE bytes, which is sufficient to hold sm_attr->data without truncation. The return value of strscpy() matches the expected behavior of sm_attr_show().

Replace the strncpy() call with:

    return strscpy(buf, sm_attr->data, PAGE_SIZE);
---
 drivers/mtd/sm_ftl.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/mtd/sm_ftl.c +1 -2
index abc7b186353f..229fdc4a754a 100644
@@ -44,8 +44,7 @@ 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);
-	return sm_attr->len;
+	return strscpy(buf, sm_attr->data, PAGE_SIZE);
 }
 
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hello,

Quoted 16 lines · Dharanitharan R

In sm_ftl.c, the sm_attr_show() function currently copies attribute data using:

    strncpy(buf, sm_attr->data, sm_attr->len);
    return sm_attr->len;

Using strncpy() can be unsafe because it does not guarantee a NUL terminator if the source length equals the buffer size. Although sm_attr->data comes from internal structures and is NUL-terminated, it is cleaner and safer to use strscpy(), which guarantees NUL termination and avoids zero-padding.

The destination buffer is PAGE_SIZE bytes, which is sufficient to hold sm_attr->data without truncation. The return value of strscpy() matches the expected behavior of sm_attr_show().

Quoted 5 lines

Replace the strncpy() call with:

    return strscpy(buf, sm_attr->data, PAGE_SIZE);

This commit won't apply, please resend based on v6.19-rc1.

Thanks,
Miquèl