Skip to contentSkip to conversation

Removing deprecated strncpy()

PATCH In review Kevin Paul Reddy Janagari 2 participants 2 messages Apr 10, 2025 s390

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Removing deprecated strncpy()

This patch suggests the replacement of strncpy with strscpy as per the Documentation/process/deprecated
the strncpy() fails to guarntee NULL termination since the function adds zero pads, this isn't really convenient for short strings as it may cause performce issues

strscpy() is a preffered replacement because
it overcomes the limitations of strncpy as mentioned above

---
 arch/s390/boot/printk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/boot/printk.c +1 -1
index 8cf6331bc060..8f3b2244ef1b 100644
@@ -158,7 +158,7 @@ static noinline char *strsym(char *buf, void *ip)
 
 	p = findsym((unsigned long)ip, &off, &len);
 	if (p) {
-		strncpy(buf, p, MAX_SYMLEN);
+		strscpy(buf, p, MAX_SYMLEN);
 		/* reserve 15 bytes for offset/len in symbol+0x1234/0x1234 */
 		p = buf + strnlen(buf, MAX_SYMLEN - 15);
 		strcpy(p, "+0x");
Signature · 1 line
2.39.5
maintainer
#1 lore raw
Quoted 19 lines · Kevin Paul Reddy Janagari

This patch suggests the replacement of strncpy with strscpy as per the Documentation/process/deprecated
the strncpy() fails to guarntee NULL termination since the function adds zero pads, this isn't really convenient for short strings as it may cause performce issues

strscpy() is a preffered replacement because
it overcomes the limitations of strncpy as mentioned above

---
 arch/s390/boot/printk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/boot/printk.c +0 -0
index 8cf6331bc060..8f3b2244ef1b 100644

arch/s390/boot/printk.c static noinline char *strsym(char *buf, void *ip) · line 161

Quoted 5 lines
arch/s390/boot/printk.c +1 -1
@@ -158,7 +158,7 @@ static noinline char *strsym(char *buf, void *ip)
 
 	p = findsym((unsigned long)ip, &off, &len);
 	if (p) {
-		strncpy(buf, p, MAX_SYMLEN);
+		strscpy(buf, p, MAX_SYMLEN);

This won't even compile. Please compile test before sending any patches.