mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexey Dobriyan <adobriyan@gmail.com>
To: akpm@linux-foundation.org
Cc: Dmitry Antipov <dmantipov@yandex.ru>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH] kstrtox: delete saturation in case of overflow
Date: Thu, 17 Sep 2026 19:17:07 +0300	[thread overview]
Message-ID: <a396f2ea-8de8-4eca-a216-ff69bc40fe85@p183> (raw)

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


             reply	other threads:[~2026-09-17 16:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-17 16:17 Alexey Dobriyan [this message]
2026-09-17 21:37 ` Andrew Morton
2026-09-18  6:19 ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a396f2ea-8de8-4eca-a216-ff69bc40fe85@p183 \
    --to=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=dmantipov@yandex.ru \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®