Input: axp20x-pek - avoid needless newline removal
authorJustin Stitt <justinstitt@google.com>
Sat, 30 Sep 2023 16:32:31 +0000 (09:32 -0700)
committerDmitry Torokhov <dmitry.torokhov@gmail.com>
Sat, 30 Sep 2023 16:35:31 +0000 (09:35 -0700)
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>
drivers/input/misc/axp20x-pek.c

index 4581606a28d63011c28c37f776001b3ee0ef871e..24f9e9d893de3a2fe24f4271c0281f421ef5abc1 100644 (file)
@@ -133,20 +133,11 @@ static ssize_t axp20x_store_attr(struct device *dev,
                                 size_t count)
 {
        struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
-       char val_str[20];
-       size_t len;
        int ret, i;
        unsigned int val, idx = 0;
        unsigned int best_err = UINT_MAX;
 
-       val_str[sizeof(val_str) - 1] = '\0';
-       strncpy(val_str, buf, sizeof(val_str) - 1);
-       len = strlen(val_str);
-
-       if (len && val_str[len - 1] == '\n')
-               val_str[len - 1] = '\0';
-
-       ret = kstrtouint(val_str, 10, &val);
+       ret = kstrtouint(buf, 10, &val);
        if (ret)
                return ret;