[PATCH] Removing deprecated strncpy()

Kevin Paul Reddy Janagari posted 1 patch 10 months ago
There is a newer version of this series
arch/s390/boot/printk.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] Removing deprecated strncpy()
Posted by Kevin Paul Reddy Janagari 10 months ago
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

Signed-off-by: Kevin Paul Reddy Janagari <kevinpaul468@gmail.com>
---
 arch/s390/boot/printk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/s390/boot/printk.c b/arch/s390/boot/printk.c
index 8cf6331bc060..8f3b2244ef1b 100644
--- a/arch/s390/boot/printk.c
+++ b/arch/s390/boot/printk.c
@@ -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");
-- 
2.39.5
Re: [PATCH] Removing deprecated strncpy()
Posted by Heiko Carstens 10 months ago
On Thu, Apr 10, 2025 at 05:02:30PM +0530, Kevin Paul Reddy Janagari wrote:
> 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
> 
> Signed-off-by: Kevin Paul Reddy Janagari <kevinpaul468@gmail.com>
> ---
>  arch/s390/boot/printk.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/s390/boot/printk.c b/arch/s390/boot/printk.c
> index 8cf6331bc060..8f3b2244ef1b 100644
> --- a/arch/s390/boot/printk.c
> +++ b/arch/s390/boot/printk.c
> @@ -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.