[PATCH] kstrtox: delete saturation in case of overflow

Alexey Dobriyan posted 1 patch 1 week ago
lib/cmdline.c | 19 ++++++++++++++-----
lib/kstrtox.c |  1 -
2 files changed, 14 insertions(+), 6 deletions(-)
[PATCH] kstrtox: delete saturation in case of overflow
Posted by Alexey Dobriyan 1 week ago
From 161367fd1e8b41e938274923bbb3d26de69f7a7e Mon Sep 17 00:00:00 2001
From: Alexey Dobriyan <adobriyan@gmail.com>
Date: Thu, 17 Sep 2026 18:49:18 +0300
Subject: [PATCH 1/1] kstrtox: delete saturation in case of overflow

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@gmail.com>
---
 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
Re: [PATCH] kstrtox: delete saturation in case of overflow
Posted by kernel test robot 2 days, 13 hours ago
Hi Alexey,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-nonmm-unstable]
[also build test ERROR on akpm-mm/mm-everything linus/master v7.3-rc4 next-20260921]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Alexey-Dobriyan/kstrtox-delete-saturation-in-case-of-overflow/20260917-191707
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-nonmm-unstable
patch link:    https://lore.kernel.org/r/a396f2ea-8de8-4eca-a216-ff69bc40fe85%40p183
patch subject: [PATCH] kstrtox: delete saturation in case of overflow
config: riscv-allnoconfig (https://download.01.org/0day-ci/archive/20260922/202609221939.FSyfaCW4-lkp@intel.com/config)
compiler: riscv64-linux-gcc (GCC) 16.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260922/202609221939.FSyfaCW4-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202609221939.FSyfaCW4-lkp@intel.com/

All errors (new ones prefixed by >>):

   riscv64-linux-ld: ./drivers/firmware/efi/libstub/lib-cmdline.stub.o: in function `__efistub_memparse':
>> __efistub_cmdline.c:(.init.text+0x13a): undefined reference to `__efistub__parse_integer_fixup_radix'
>> riscv64-linux-ld: __efistub_cmdline.c:(.init.text+0x154): undefined reference to `__efistub__parse_integer_limit'
   riscv64-linux-ld: .tmp_vmlinux1: hidden symbol `__efistub__parse_integer_fixup_radix' isn't defined
   riscv64-linux-ld: final link failed: bad value

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] kstrtox: delete saturation in case of overflow
Posted by kernel test robot 2 days, 13 hours ago
Hi Alexey,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-nonmm-unstable]
[also build test ERROR on akpm-mm/mm-everything linus/master v7.3-rc4 next-20260921]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Alexey-Dobriyan/kstrtox-delete-saturation-in-case-of-overflow/20260917-191707
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-nonmm-unstable
patch link:    https://lore.kernel.org/r/a396f2ea-8de8-4eca-a216-ff69bc40fe85%40p183
patch subject: [PATCH] kstrtox: delete saturation in case of overflow
config: s390-allnoconfig (https://download.01.org/0day-ci/archive/20260922/202609221929.OSikd7q0-lkp@intel.com/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 5dc57157f12f0ff676e823d9b4d98b45c07e5df2)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260922/202609221929.OSikd7q0-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202609221929.OSikd7q0-lkp@intel.com/

All errors (new ones prefixed by >>):

>> ld.lld: error: undefined symbol: _parse_integer_fixup_radix
   >>> referenced by cmdline.c
   >>>               arch/s390/boot/cmdline.o:(memparse)
--
>> ld.lld: error: undefined symbol: _parse_integer_limit
   >>> referenced by cmdline.c
   >>>               arch/s390/boot/cmdline.o:(memparse)

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] kstrtox: delete saturation in case of overflow
Posted by Andy Shevchenko 6 days, 19 hours ago
On Thu, Sep 17, 2026 at 07:17:07PM +0300, Alexey Dobriyan wrote:
> From 161367fd1e8b41e938274923bbb3d26de69f7a7e Mon Sep 17 00:00:00 2001
> From: Alexey Dobriyan <adobriyan@gmail.com>
> Date: Thu, 17 Sep 2026 18:49:18 +0300
> Subject: [PATCH 1/1] kstrtox: delete saturation in case of overflow

Dunno if this is okay to have in the body. I think you need to use some korg
tools to send mails in a better way.

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

The reference to the commit is standardized:
commit 6e30111dbb40 ("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.

I don't see the use of this saturation, so perhaps we indeed can live without
it as before (note, I haven't checked any POSIX/C standards for the conversions,
dunno if they specify that kind of behaviour).

...

All above assuming that test cases are passed as before.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH] kstrtox: delete saturation in case of overflow
Posted by Andrew Morton 1 week ago
On Thu, 17 Sep 2026 19:17:07 +0300 Alexey Dobriyan <adobriyan@gmail.com> wrote:

> >From 161367fd1e8b41e938274923bbb3d26de69f7a7e Mon Sep 17 00:00:00 2001
> From: Alexey Dobriyan <adobriyan@gmail.com>
> Date: Thu, 17 Sep 2026 18:49:18 +0300
> Subject: [PATCH 1/1] kstrtox: delete saturation in case of overflow
> 
> 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.

I don't know what this patch does :(

Can we define/explain "saturation"?

Should we be updating lib/tests/cmdline_kunit.c when altering memparse?