mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] kstrtox: delete saturation in case of overflow
@ 2026-09-17 16:17 Alexey Dobriyan
  2026-09-17 21:37 ` Andrew Morton
  2026-09-18  6:19 ` Andy Shevchenko
  0 siblings, 2 replies; 3+ messages in thread
From: Alexey Dobriyan @ 2026-09-17 16:17 UTC (permalink / raw)
  To: akpm; +Cc: Dmitry Antipov, Andy Shevchenko, linux-kernel

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] kstrtox: delete saturation in case of overflow
  2026-09-17 16:17 [PATCH] kstrtox: delete saturation in case of overflow Alexey Dobriyan
@ 2026-09-17 21:37 ` Andrew Morton
  2026-09-18  6:19 ` Andy Shevchenko
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Morton @ 2026-09-17 21:37 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: Dmitry Antipov, Andy Shevchenko, linux-kernel

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?


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] kstrtox: delete saturation in case of overflow
  2026-09-17 16:17 [PATCH] kstrtox: delete saturation in case of overflow Alexey Dobriyan
  2026-09-17 21:37 ` Andrew Morton
@ 2026-09-18  6:19 ` Andy Shevchenko
  1 sibling, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2026-09-18  6:19 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: akpm, Dmitry Antipov, linux-kernel

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-09-18  6:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-17 16:17 [PATCH] kstrtox: delete saturation in case of overflow Alexey Dobriyan
2026-09-17 21:37 ` Andrew Morton
2026-09-18  6:19 ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®