[PATCH v14 02/12] lib: kstrtox: add local _parse_integer_limit_init() helper

From: Rodrigo Alencar via B4 Relay

Date: Sun May 24 2026 - 06:37:16 EST


From: Rodrigo Alencar <rodrigo.alencar@xxxxxxxxxx>

Add parsing helper that accepts an initial value for the accumulated
result when parsing an 64-bit integer. It reuses current implementation
for _parse_integer_limit(), which now consumes the new function with
init = 0. The diff algorithm would have the documentation header and
prototype of _parse_integer_limit() moved around so it is adjusted
according to guidelines.

Signed-off-by: Rodrigo Alencar <rodrigo.alencar@xxxxxxxxxx>
---
lib/kstrtox.c | 41 ++++++++++++++++++++++++++++-------------
1 file changed, 28 insertions(+), 13 deletions(-)

diff --git a/lib/kstrtox.c b/lib/kstrtox.c
index 97be2a39f537..bd63c55b8490 100644
--- a/lib/kstrtox.c
+++ b/lib/kstrtox.c
@@ -39,23 +39,15 @@ const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
return s;
}

-/*
- * Convert non-negative integer string representation in explicitly given radix
- * to an integer. A maximum of max_chars characters will be converted.
- *
- * Return number of characters consumed maybe or-ed with overflow bit.
- * If overflow occurs, result integer (incorrect) is still returned.
- *
- * Don't you dare use this function.
- */
-noinline
-unsigned int _parse_integer_limit(const char *s, unsigned int base, unsigned long long *p,
- size_t max_chars)
+static unsigned int _parse_integer_limit_init(const char *s, unsigned int base,
+ unsigned long long init,
+ unsigned long long *p,
+ size_t max_chars)
{
unsigned long long res;
unsigned int rv;

- res = 0;
+ res = init;
rv = 0;
while (max_chars--) {
unsigned int c = *s;
@@ -87,6 +79,29 @@ unsigned int _parse_integer_limit(const char *s, unsigned int base, unsigned lon
return rv;
}

+/**
+ * _parse_integer_limit() - Convert integer string representation to an integer
+ * limiting the number of characters parsed.
+ * @s: The start of the string.
+ * @base: The number base to use.
+ * @p: Where to write the result of the conversion.
+ * @max_chars: Maximum amount of characters to consume.
+ *
+ * Convert non-negative integer string representation in explicitly given radix
+ * to an integer. A maximum of max_chars characters will be converted.
+ *
+ * Avoid using this function directly, consider kstrto*() functions instead.
+ *
+ * Return: Number of characters consumed maybe or-ed with overflow bit.
+ * If overflow occurs, result integer (incorrect) is still returned.
+ */
+noinline
+unsigned int _parse_integer_limit(const char *s, unsigned int base,
+ unsigned long long *p, size_t max_chars)
+{
+ return _parse_integer_limit_init(s, base, 0, p, max_chars);
+}
+
noinline
unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
{

--
2.43.0