Skip to contentSkip to conversation

x86/mce/dev-mcelog: use strscpy() to instead of strncpy()

PATCH Applied [email protected] 4 participants 6 messages Dec 3, 2022 – Jan 7, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/mce/dev-mcelog: 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.

---
 arch/x86/kernel/cpu/mce/dev-mcelog.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kernel/cpu/mce/dev-mcelog.c +1 -2
index 100fbeebdc72..a05ac0716ecf 100644
@@ -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)
Signature · 1 line
2.15.2
maintainer
#3 lore raw
Quoted 4 lines
Quoted 2 lines

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

#4 lore raw
Quoted 9 lines · Tony
Quoted 4 lines
Quoted 2 lines

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.

Quoted 7 lines

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

merge bot Applied
#5 lore raw
In reply to [email protected]
The following commit has been merged into the x86/platform branch of tip:

The following commit has been merged into the x86/platform branch of tip:

Commit-ID:     7ddf0050a21fdcc025c3cb1055fe3db60df1cd97
Gitweb:        https://git.kernel.org/tip/7ddf0050a21fdcc025c3cb1055fe3db60df1cd97
Author:        Xu Panda <[email protected]>
AuthorDate:    Sat, 03 Dec 2022 14:19:32 +08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Sat, 07 Jan 2023 11:47:35 +01:00

x86/mce/dev-mcelog: use strscpy() to instead of strncpy()

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

---
 arch/x86/kernel/cpu/mce/dev-mcelog.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kernel/cpu/mce/dev-mcelog.c +1 -2
index 100fbee..a05ac07 100644
@@ -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)