Skip to contentSkip to conversation

kstrtox: delete saturation in case of overflow

PATCH In review Alexey Dobriyan 4 participants 5 messages Sep 17 – Sep 22 Library code

lore
Share & tools
lore
#0 lore raw

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

From 161367fd1e8b41e938274923bbb3d26de69f7a7e Mon Sep 17 00:00:00 2001
From: Alexey Dobriyan <[email protected]>
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.

---
 lib/cmdline.c | 19 ++++++++++++++-----
 lib/kstrtox.c |  1 -
 2 files changed, 14 insertions(+), 6 deletions(-)
lib/cmdline.c +14 -5
index 16cce6621cec..7f9859733fb5 100644
@@ -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;
 }
lib/kstrtox.c +0 -1
index bac1c057e1b0..dcbc613fcde6 100644
@@ -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 {
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 14 lines · On Thu, 17 Sep 2026 19:17:07 +0300 Alexey Dobriyan <[email protected]> wrote:
Quoted 1 line

From 161367fd1e8b41e938274923bbb3d26de69f7a7e Mon Sep 17 00:00:00 2001

From: Alexey Dobriyan <[email protected]>
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.

Quoted 6 lines

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?

#2 lore raw
In reply to Alexey Dobriyan
Quoted 4 lines · Alexey Dobriyan

From 161367fd1e8b41e938274923bbb3d26de69f7a7e Mon Sep 17 00:00:00 2001
From: Alexey Dobriyan <[email protected]>
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.

Quoted 4 lines

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")

Quoted 10 lines

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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
In reply to Alexey Dobriyan
kernel test robot noticed the following build errors:

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/[email protected]/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/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

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

#4 lore raw
In reply to Alexey Dobriyan
kernel test robot noticed the following build errors:

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/[email protected]/config)
compiler: riscv64-linux-gcc (GCC) 16.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260922/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

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