Skip to contentSkip to conversation

rpmsg: use strscpy() to instead of strncpy()

PATCH In review [email protected] 2 participants 2 messages Dec 23, 2022 – Jan 5, 2023 Remote processor messaging (rpmsg)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rpmsg: use strscpy() to instead of strncpy()

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL-terminated strings.

---
 drivers/rpmsg/rpmsg_ns.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/rpmsg/rpmsg_ns.c +1 -3
index c70ad03ff2e9..c7b5b60aef4d 100644
@@ -48,9 +48,7 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
 	}
 
 	/* don't trust the remote processor for null terminating the name */
-	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
-
-	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
+	strscpy(chinfo.name, msg->name, sizeof(chinfo.name));
 	chinfo.src = RPMSG_ADDR_ANY;
 	chinfo.dst = rpmsg32_to_cpu(rpdev, msg->addr);
 
Signature · 1 line
2.15.2
maintainer
#1 lore raw
Quoted 16 lines · On Fri, Dec 23, 2022 at 10:44:10AM +0800, [email protected] wrote:

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL-terminated strings.

---
 drivers/rpmsg/rpmsg_ns.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/rpmsg/rpmsg_ns.c +0 -0
index c70ad03ff2e9..c7b5b60aef4d 100644

drivers/rpmsg/rpmsg_ns.c static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len, · line 52

Quoted 5 lines
drivers/rpmsg/rpmsg_ns.c +0 -2
@@ -48,9 +48,7 @@ static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
 	}
 
 	/* don't trust the remote processor for null terminating the name */
-	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
-

This is broken.

drivers/rpmsg/rpmsg_ns.c static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len, · line 51

Quoted 2 lines
drivers/rpmsg/rpmsg_ns.c +1 -1
-	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
+	strscpy(chinfo.name, msg->name, sizeof(chinfo.name));

In this case there isn't a need to use strscpy() since we _know_ from the above that msg->name is NULL terminated.

Quoted 5 lines
drivers/rpmsg/rpmsg_ns.c +0 -0
 	chinfo.src = RPMSG_ADDR_ANY;
 	chinfo.dst = rpmsg32_to_cpu(rpdev, msg->addr);
 
Signature · 1 line
2.15.2