From nobody Thu Apr 9 18:26:25 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 571DF320A00; Tue, 3 Mar 2026 13:27:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772544445; cv=none; b=bPCJbLL/ZRdtiSmRXBuCFb0V9crXuwWr1RKV5B41/2uyHQ3FCVD+PaLV9D6bm1jWFiZILvvrwOwdYgNazQQAZVM2Fqu/BiREA97KOUMUJWQywUj+Od+iZWGUhtA1PZlKEshpMsb/br/mI+D+PF9EdNHNtsm/9y6pH5BI4WawDjY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772544445; c=relaxed/simple; bh=N1VfDg8H/7194eoYlOpUeo1wfbx91H88u4KEWIcBo/I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=T9gy9cPYDTd7MOMZJeTgKZU42gI9KQEsZDx7pRDerW7oU4YGkTY2nhGzcHPi35MgyBSbq+7VcRRDJ1cpRRo2p+w18+SBgQ+bREuN0yYpBDT1CH7Q8jhfs+XQRXDeNAP6s/QytElGRcYDixM3sJeHsc/jFtLITDTNZYsliOQiHmY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Q7C8x/Po; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Q7C8x/Po" Received: by smtp.kernel.org (Postfix) with ESMTPS id 22224C2BCB1; Tue, 3 Mar 2026 13:27:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772544445; bh=N1VfDg8H/7194eoYlOpUeo1wfbx91H88u4KEWIcBo/I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Q7C8x/PoLrq5kh/pCh3/2IA8YgA9Syu7k3EUuzGmSexJOvIIpu2i2rBE3SPKWe6R7 gIsFYMgE9FC/X2Fy/+AL8IlAZxEW0tycqgh6Eg0j4oOY7HRdwIGuwEp/09qWeDQU8a cWZtgH+Pxa2BncxZqpuNka2q775m+T9SuuU2ZctMvi6uuZrz4USrfRW+iP4BRW93Q5 d8o9V4fJcOhWymqoHBE4GlFcd+4Gi8fGug58zUlQSU4eitGQmMjOSjrEB5fIqe/q9V sxkwwzolq95I+8IDtzGKjZno0se8VHZl8lb9EKv0QkT97hRAuwBL6399VFSbAvYrIv WapCsG0PbFuqQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 11FFBEC1441; Tue, 3 Mar 2026 13:27:25 +0000 (UTC) From: Rodrigo Alencar via B4 Relay Date: Tue, 03 Mar 2026 13:27:07 +0000 Subject: [PATCH v8 02/10] lib: kstrtox: add kstrntoull() helper Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260303-adf41513-iio-driver-v8-2-8dd2417cc465@analog.com> References: <20260303-adf41513-iio-driver-v8-0-8dd2417cc465@analog.com> In-Reply-To: <20260303-adf41513-iio-driver-v8-0-8dd2417cc465@analog.com> To: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-doc@vger.kernel.org Cc: Jonathan Cameron , David Lechner , Andy Shevchenko , Lars-Peter Clausen , Michael Hennerich , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , Andrew Morton , Rodrigo Alencar X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1772544443; l=3621; i=rodrigo.alencar@analog.com; s=default; h=from:subject:message-id; bh=QOA2ebXlCGOu4rEF7LtCPczO4bNdBpAyoo2OhH/870A=; b=wV3bcB78rfX1cS1C53uRVYeEZWLj9IlYYYqbKdCGwFXsZ9gp+BQgGEIz+SeKEFkki+7v3FBtC yQX8C5gQY2JD/Sxq364rygWsMEnB7CNS5Gh/P9MyRdSWPl4nE2Gmg3A X-Developer-Key: i=rodrigo.alencar@analog.com; a=ed25519; pk=ULeHbgU/OYh/PG/4anHDfLgldFItQHAhOktYRVLMFRo= X-Endpoint-Received: by B4 Relay for rodrigo.alencar@analog.com/default with auth_id=561 X-Original-From: Rodrigo Alencar Reply-To: rodrigo.alencar@analog.com From: Rodrigo Alencar Add kstrntoull() function, which converts a string to an ULL with a max character limit. The function is an alternative integer parsing function that does not require a null-terminated string. It becomes a better option over simple_strtoull() or kstrtoull() when parsing integers from a buffer with custom delimiters without having to create temporary copies. The function is consumed inside the implementation _kstrtoull(), promoting reuse. Signed-off-by: Rodrigo Alencar --- include/linux/kstrtox.h | 3 +++ lib/kstrtox.c | 47 +++++++++++++++++++++++++++++++++++++++++++--= -- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/include/linux/kstrtox.h b/include/linux/kstrtox.h index 6ea897222af1..49a6102b8e15 100644 --- a/include/linux/kstrtox.h +++ b/include/linux/kstrtox.h @@ -9,6 +9,9 @@ int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long= *res); int __must_check _kstrtol(const char *s, unsigned int base, long *res); =20 +ssize_t __must_check kstrntoull(const char *s, unsigned int base, + unsigned long long *res, size_t max_chars); + int __must_check kstrtoull(const char *s, unsigned int base, unsigned long= long *res); int __must_check kstrtoll(const char *s, unsigned int base, long long *res= ); =20 diff --git a/lib/kstrtox.c b/lib/kstrtox.c index bdde40cd69d7..202ecc058284 100644 --- a/lib/kstrtox.c +++ b/lib/kstrtox.c @@ -93,17 +93,56 @@ unsigned int _parse_integer(const char *s, unsigned int= base, unsigned long long return _parse_integer_limit(s, base, p, INT_MAX); } =20 -static int _kstrtoull(const char *s, unsigned int base, unsigned long long= *res) +/** + * kstrntoull - convert a string to an unsigned long long with a maximum + * character limit + * @s: The start of the string. The string does not need to be null-termin= ated. + * The first character cannot be a sign. + * @base: The number base to use. The maximum supported base is 16. If bas= e is + * given as 0, then the base of the string is automatically detected with= the + * conventional semantics - If it begins with 0x the number will be parse= d as a + * hexadecimal (case insensitive), if it otherwise begins with 0, it will= be + * parsed as an octal number. Otherwise it will be parsed as a decimal. + * @res: Where to write the result of the conversion. + * @max_chars: The maximum number of characters to convert. + * + * Conversion stops when the maximum number of characters is reached or a + * non-digit character is encountered. + * + * Returns the number of characters consumed on success, -ERANGE on overfl= ow and + * -EINVAL on invalid input. Return code must be checked. + */ +noinline +ssize_t kstrntoull(const char *s, unsigned int base, unsigned long long *r= es, + size_t max_chars) { - unsigned long long _res; + const char *cp; + size_t prefix_cnt; unsigned int rv; =20 - s =3D _parse_integer_fixup_radix(s, &base); - rv =3D _parse_integer(s, base, &_res); + cp =3D _parse_integer_fixup_radix(s, &base); + prefix_cnt =3D cp - s; + if (prefix_cnt >=3D max_chars) + return -EINVAL; + + rv =3D _parse_integer_limit(cp, base, res, max_chars - prefix_cnt); if (rv & KSTRTOX_OVERFLOW) return -ERANGE; if (rv =3D=3D 0) return -EINVAL; + + return prefix_cnt + rv; +} +EXPORT_SYMBOL(kstrntoull); + +static int _kstrtoull(const char *s, unsigned int base, unsigned long long= *res) +{ + unsigned long long _res; + ssize_t rv; + + rv =3D kstrntoull(s, base, &_res, INT_MAX); + if (rv < 0) + return rv; s +=3D rv; if (*s =3D=3D '\n') s++; --=20 2.43.0