Skip to contentSkip to conversation

accessibility: speakup: use strscpy() to instead of strncpy()

PATCH In review [email protected] 2 participants 2 messages Dec 3, 2022 Speakup console speech driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accessibility: speakup: 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/accessibility/speakup/kobjects.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/accessibility/speakup/kobjects.c +1 -2
index a7522d409802..273c076b661a 100644
@@ -426,8 +426,7 @@ static ssize_t synth_direct_store(struct kobject *kobj,
 	spin_lock_irqsave(&speakup_info.spinlock, flags);
 	while (len > 0) {
 		bytes = min_t(size_t, len, 250);
-		strncpy(tmp, ptr, bytes);
-		tmp[bytes] = '\0';
+		strscpy(tmp, ptr, bytes);
 		string_unescape_any_inplace(tmp);
 		synth_printf("%s", tmp);
 		ptr += bytes;
Signature · 1 line
2.15.2
maintainer
#1 lore raw

[email protected], le sam. 03 déc. 2022 14:21:28 +0800, a ecrit:

Quoted 16 lines

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/accessibility/speakup/kobjects.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/accessibility/speakup/kobjects.c +0 -0
index a7522d409802..273c076b661a 100644

drivers/accessibility/speakup/kobjects.c static ssize_t synth_direct_store(struct kobject *kobj, · line 429

Quoted 6 lines
drivers/accessibility/speakup/kobjects.c +1 -2
@@ -426,8 +426,7 @@ static ssize_t synth_direct_store(struct kobject *kobj,
 	spin_lock_irqsave(&speakup_info.spinlock, flags);
 	while (len > 0) {
 		bytes = min_t(size_t, len, 250);
-		strncpy(tmp, ptr, bytes);
-		tmp[bytes] = '\0';
+		strscpy(tmp, ptr, bytes);

This needs to be bytes+1, otherwise the trailing \0 will be put at bytes-1 instead of bytes as the original code does, thus eating one character.

Quoted 6 lines
drivers/accessibility/speakup/kobjects.c +0 -0
 		string_unescape_any_inplace(tmp);
 		synth_printf("%s", tmp);
 		ptr += bytes;
Signature · 1 line
2.15.2