[linux-next] x86/mce/dev-mcelog: use strscpy() to instead of strncpy()

Message ID 202212031419324523731@zte.com.cn
State New
Headers
Series [linux-next] x86/mce/dev-mcelog: use strscpy() to instead of strncpy() |

Commit Message

Yang Yang Dec. 3, 2022, 6:19 a.m. UTC
  From: Xu Panda <xu.panda@zte.com.cn>

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

Signed-off-by: Xu Panda <xu.panda@zte.com.cn>
Signed-off-by: Yang Yang <yang.yang29@zte.com>
---
 arch/x86/kernel/cpu/mce/dev-mcelog.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
  

Comments

Luck, Tony Dec. 3, 2022, 10:21 p.m. UTC | #1
> From: Xu Panda <xu.panda@zte.com.cn>
>
> The implementation of strscpy() is more robust and safer.
> That's now the recommended way to copy NUL terminated strings.

Reviewed-by: Tony Luck <tony.luck@intel.com>
  
Ricardo Neri Dec. 14, 2022, 8:54 p.m. UTC | #2
On Sat, Dec 03, 2022 at 02:19:32PM +0800, yang.yang29@zte.com.cn wrote:
> From: Xu Panda <xu.panda@zte.com.cn>
> 
> The implementation of strscpy() is more robust and safer.
> That's now the recommended way to copy NUL terminated strings.

It should read "NULL-terminated strings".

Thanks and BR,
Ricardo
  
Luck, Tony Dec. 14, 2022, 9:29 p.m. UTC | #3
>> The implementation of strscpy() is more robust and safer.
>> That's now the recommended way to copy NUL terminated strings.
>
> It should read "NULL-terminated strings".

Both "NUL-terminated" and "NULL-terminated" seem to be used in the kernel
source and commit comments.  NULL is ahead 124:45 in the source, but it
is closer in the commit logs where the ratio is 129:76.

Single "L" NUL seems technically more accurate as the string terminator is
an ascii NUL character. From man 5 ascii the character name is NUL (though
the description says "null" to muddy the waters)

       Oct   Dec   Hex   Char
       ───────────────────────────────────────────
       000   0     00    NUL '\0' (null character)

-Tony
  
Ricardo Neri Dec. 14, 2022, 10:54 p.m. UTC | #4
On Wed, Dec 14, 2022 at 09:29:59PM +0000, Luck, Tony wrote:
> >> The implementation of strscpy() is more robust and safer.
> >> That's now the recommended way to copy NUL terminated strings.
> >
> > It should read "NULL-terminated strings".
> 
> Both "NUL-terminated" and "NULL-terminated" seem to be used in the kernel
> source and commit comments.  NULL is ahead 124:45 in the source, but it
> is closer in the commit logs where the ratio is 129:76.
> 
> Single "L" NUL seems technically more accurate as the string terminator is
> an ascii NUL character. From man 5 ascii the character name is NUL (though
> the description says "null" to muddy the waters)
> 
>        Oct   Dec   Hex   Char
>        ───────────────────────────────────────────
>        000   0     00    NUL '\0' (null character)

Thanks for the analysis Tony. Indeed, I agree that "NUL-terminated" is
more accurate.

Thanks and BR,
Ricardo
  

Patch

diff --git a/arch/x86/kernel/cpu/mce/dev-mcelog.c b/arch/x86/kernel/cpu/mce/dev-mcelog.c
index 100fbeebdc72..a05ac0716ecf 100644
--- a/arch/x86/kernel/cpu/mce/dev-mcelog.c
+++ b/arch/x86/kernel/cpu/mce/dev-mcelog.c
@@ -105,8 +105,7 @@  static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
 {
 	char *p;

-	strncpy(mce_helper, buf, sizeof(mce_helper));
-	mce_helper[sizeof(mce_helper)-1] = 0;
+	strscpy(mce_helper, buf, sizeof(mce_helper));
 	p = strchr(mce_helper, '\n');

 	if (p)