Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Input: axp20x-pek - avoid needless newline removal

This code is doing more work than it needs to.

Before handing off `val_str` to `kstrtouint()` we are eagerly removing
any trailing newline which requires copying `buf`, validating it's
length and checking/replacing any potential newlines.

kstrtouint() handles this implicitly:
kstrtouint ->
kstrotoull -> (documentation)
| /**
| * kstrtoull - convert a string to an unsigned long long
| * @s: The start of the string. The string must be null-terminated, and may also
| * include a single newline before its terminating null. The first character
| ...

Let's remove the redundant functionality and let kstrtouint handle it.

Suggested-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Justin Stitt <justinstitt@google.com>
Reviewed-by: Chen-Yu Tsai <wens@csie.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20230925-strncpy-drivers-input-misc-axp20x-pek-c-v2-1-ff7abe8498d6@google.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

authored by

Justin Stitt and committed by
Dmitry Torokhov
68ede283 348cbf98

+1 -10
+1 -10
drivers/input/misc/axp20x-pek.c
··· 133 133 size_t count) 134 134 { 135 135 struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev); 136 - char val_str[20]; 137 - size_t len; 138 136 int ret, i; 139 137 unsigned int val, idx = 0; 140 138 unsigned int best_err = UINT_MAX; 141 139 142 - val_str[sizeof(val_str) - 1] = '\0'; 143 - strncpy(val_str, buf, sizeof(val_str) - 1); 144 - len = strlen(val_str); 145 - 146 - if (len && val_str[len - 1] == '\n') 147 - val_str[len - 1] = '\0'; 148 - 149 - ret = kstrtouint(val_str, 10, &val); 140 + ret = kstrtouint(buf, 10, &val); 150 141 if (ret) 151 142 return ret; 152 143