kdb: Replace the use of simple_strto with safer kstrto in kdb_main
authorYuran Pereira <yuran.pereira@hotmail.com>
Mon, 28 Oct 2024 19:19:16 +0000 (19:19 +0000)
committerDaniel Thompson <daniel.thompson@linaro.org>
Sat, 2 Nov 2024 08:33:13 +0000 (08:33 +0000)
commit120fb87ced8eb95531ff69ec269fcbcc5be24e9c
treee211e12a7d7ee74d792e163620772778387fd98b
parent81983758430957d9a5cb3333fe324fd70cf63e7e
kdb: Replace the use of simple_strto with safer kstrto in kdb_main

The simple_str* family of functions perform no error checking in
scenarios where the input value overflows the intended output variable.
This results in these functions successfully returning even when the
output does not match the input string.

Or as it was mentioned [1], "...simple_strtol(), simple_strtoll(),
simple_strtoul(), and simple_strtoull() functions explicitly ignore
overflows, which may lead to unexpected results in callers."
Hence, the use of those functions is discouraged.

This patch replaces all uses of the simple_strto* series of functions
with their safer kstrto* alternatives.

Side effects of this patch:
- Every string to long or long long conversion using kstrto* is now
  checked for failure.
- kstrto* errors are handled with appropriate `KDB_BADINT` wherever
  applicable.
- A good side effect is that we end up saving a few lines of code
  since unlike in simple_strto* functions, kstrto functions do not
  need an additional "end pointer" variable, and the return values
  of the latter can be directly checked in an "if" statement without
  the need to define additional `ret` or `err` variables.
  This, of course, results in cleaner, yet still easy to understand
  code.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#simple-strtol-simple-strtoll-simple-strtoul-simple-strtoull

Signed-off-by: Yuran Pereira <yuran.pereira@hotmail.com>
[nir: addressed review comments by fixing styling, invalid conversion and a missing error return]
Signed-off-by: Nir Lichtman <nir@lichtman.org>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Link: https://lore.kernel.org/r/20241028191916.GA918454@lichtman.org
Signed-off-by: Daniel Thompson <daniel.thompson@linaro.org>
kernel/debug/kdb/kdb_main.c