[PATCH 1/1] kstrtox: delete saturation in case of overflow

From: Alexey Dobriyan

Date: Thu Sep 17 2026 - 11:49:18 EST


Partially revert

commit 6e30111dbb4075e3c26c230417b32bc4a1c66831
lib: fix _parse_integer_limit() to handle overflow

Originally, kstrto* was written in a way to keep simple_strto*()
functions as is, so that they continue to return incorrect result
if integer overflow occurs.

Saturation doesn't do anything useful, it just creates second incorrect
value and potentially breaks simple_strto*() users.

Caller can decide to saturate itself anyway.

Move saturation to memparse() from where the idea came from.

Signed-off-by: Alexey Dobriyan <adobriyan@xxxxxxxxx>
---
lib/cmdline.c | 19 ++++++++++++++-----
lib/kstrtox.c | 1 -
2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/lib/cmdline.c b/lib/cmdline.c
index 16cce6621cec..7f9859733fb5 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -14,6 +14,8 @@
#include <linux/string.h>
#include <linux/ctype.h>

+#include "kstrtox.h"
+
/*
* If a hyphen was found in get_option, this will handle the
* range of numbers, M-N. This will expand the range and insert
@@ -146,14 +148,21 @@ EXPORT_SYMBOL(get_options);
* Parses a string into a number. The number stored at @ptr is
* potentially suffixed with K, M, G, T, P, E.
*
- * Return: The value as recognized by simple_strtoull() multiplied
- * by the value as specified by suffix, if any.
+ * Return: The value multiplied by the value as specified by suffix, if any.
*/

unsigned long long memparse(const char *ptr, char **retptr)
{
- char *endptr; /* local pointer to end of parsed string */
- unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
+ unsigned int base = 0;
+ ptr = _parse_integer_fixup_radix(ptr, &base);
+ unsigned long long ret;
+ unsigned int rv = _parse_integer(ptr, base, &ret);
+ if (rv & KSTRTOX_OVERFLOW) {
+ rv &= ~KSTRTOX_OVERFLOW;
+ ret = -1;
+ }
+ /* local pointer to end of parsed string */
+ const char *endptr = ptr + rv;
unsigned int shl = 0;

/* Consume valid suffix even in case of overflow. */
@@ -194,7 +203,7 @@ unsigned long long memparse(const char *ptr, char **retptr)
}

if (retptr)
- *retptr = endptr;
+ *retptr = (char *)endptr;

return ret;
}
diff --git a/lib/kstrtox.c b/lib/kstrtox.c
index bac1c057e1b0..dcbc613fcde6 100644
--- a/lib/kstrtox.c
+++ b/lib/kstrtox.c
@@ -85,7 +85,6 @@ unsigned int _parse_integer_limit(const char *s, unsigned int base, unsigned lon
if (unlikely(res & (~0ull << 60))) {
if (check_mul_overflow(res, base, &res) ||
check_add_overflow(res, val, &res)) {
- res = ULLONG_MAX;
overflow = KSTRTOX_OVERFLOW;
}
} else {
--
2.55.0