From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Rodrigo Alencar <455.rodrigo.alencar@gmail.com>
Cc: Petr Mladek <pmladek@suse.com>,
rodrigo.alencar@analog.com, linux-kernel@vger.kernel.org,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-doc@vger.kernel.org, Jonathan Cameron <jic23@kernel.org>,
David Lechner <dlechner@baylibre.com>,
Andy Shevchenko <andy@kernel.org>,
Lars-Peter Clausen <lars@metafoo.de>,
Michael Hennerich <Michael.Hennerich@analog.com>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
Andrew Morton <akpm@linux-foundation.org>,
Steven Rostedt <rostedt@goodmis.org>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Sergey Senozhatsky <senozhatsky@chromium.org>,
Shuah Khan <skhan@linuxfoundation.org>
Subject: Re: [PATCH v9 2/9] lib: vsprintf: export simple_strntoull() in a safe prototype
Date: Fri, 27 Mar 2026 12:21:13 +0200 [thread overview]
Message-ID: <acZaGUV0MwuHNDru@ashevche-desk.local> (raw)
In-Reply-To: <4uijfg4efzaapu3esobez55hfwqzszwagpeb4lxej2ybmifu76@s3c57fmnsme6>
On Fri, Mar 27, 2026 at 10:11:56AM +0000, Rodrigo Alencar wrote:
> On 26/03/27 11:17AM, Andy Shevchenko wrote:
> > On Fri, Mar 27, 2026 at 09:45:17AM +0100, Petr Mladek wrote:
> > > On Fri 2026-03-20 16:27:27, Rodrigo Alencar via B4 Relay wrote:
...
> > > > +extern ssize_t __must_check simple_strntoull(const char *startp, const char **endp,
> > > > + unsigned int base, size_t max_chars,
> > > > + unsigned long long *res);
> > >
> > > Sigh, naming is hard. I personally find it a bit confusing that the
> > > name is too similar to the unsafe API.
> > >
> > > IMHO, the semantic of the new API is closer to kstrtoull().
> > > It just limits the size, so I would call it kstrntoull().
> >
> > It's not. kstrto*() quite strict about the input, this one is actually relaxed
> > variant, so I wouldn't mix these two groups.
> >
> > > Also I would use int as the return parameter, see below.
...
> > TBH, I am skeptical about this approach. My main objection is max_chars
> > parameter. If we want to limit the input strictly to the given number of
> > characters, we have to copy the string and then just use kstrto*() in a normal
> > way. The whole idea of that parameter is to be able to parse the fractional
> > part of the float number as 'iiiii.fffff', where 'i' is for integer part, and
> > 'f' for the fractional. Since we have *endp, we may simply check that.
>
> A max_chars would not be only useful for that. It can prevent out-of-bounds
> reads when the input isn't NUL-terminated (like buffers, file chunks,
> network packets, memory-mapped data, ....). Even if there is a NUL later in
> memory, a regular strtoull() function may consume characters that are outside
> the field one intends to parse.
Okay, but is it the current case or just an attempt to solve the problem that
doesn't exist (yet)?
> > In case if we want to parse only, say, 6 digits and input is longer there are
> > a few options (in my personal preferences, the first is the better):
> > - consider the input invalid
> > - parse it as is up to the maximum and then do ceil() or floor() on top of that
> > - copy only necessary amount of the (sub)string and parse that.
>
> Yes, my use case is the fixed point parsing, but I suppose we are implementing
> things here for reuse.
Yes, I'm full for reuse, but I want to have it balanced between complexity,
existing use cases and possible reuse in the future.
> Also, the default behavior of the previous fixed point
> parsing in IIO is flooring the result, which leads to the same result as
> ignoring further digits.
Correct, I also lean to implying floor() (as you can read below).
> > The problem with precision is that we need to also consider floor() or ceil()
> > and I don't think this should be burden of the library as it's individual
> > preference of each of the callers (users). At least for the starter, we will
> > see if it's only one approach is used, we may incorporate it into the library
> > code.
> >
> > The easiest way out is to just consider the input invalid if it overflows the
> > given type (s32 or s64).
> >
> > But we need to have an agreement what will be the representation of the
> > fixed-width float numbers in the kernel? Currently IIO uses
> > struct float // name is crafted for simplicity
> > {
> > int integer;
> > int fraction;
> > }
>
> Yes, but to represent things like that, an assumption is made to the precision that
> "fraction" carries.
Correct.
> > This parser wants AFAIU to have at the end of the day something like
> >
> > struct float
> > {
> > s64 integer;
> > s64 fraction;
> > }
> >
> > but also wants to have the fraction part be limited in some cases to s32
> > or so:
> >
> > struct float
> > {
> > s64 integer;
> > s32 fraction; // precision may be lost if input is longer
> > }
> >
> > Maybe we want to have kstrtof32() and kstrtof64() for these two cases?
> >
> > With that we will always consider the fraction part as 32- or 64-bit,
> > imply floor() on the fraction for the sake of simplicity and require
> > it to be NUL-terminated with possible trailing '\n'.
>
> I think this is a good idea, but calling it float or fixed point itself
> is a bit confusing as float often refers to the IEEE 754 standard and
> fixed point types is often expressed in Q-format.
Yeah... I am lack of better naming.
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2026-03-27 10:21 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-20 16:27 [PATCH v9 0/9] ADF41513/ADF41510 PLL frequency synthesizers Rodrigo Alencar via B4 Relay
2026-03-20 16:27 ` [PATCH v9 1/9] dt-bindings: iio: frequency: add adf41513 Rodrigo Alencar via B4 Relay
2026-03-20 16:27 ` [PATCH v9 2/9] lib: vsprintf: export simple_strntoull() in a safe prototype Rodrigo Alencar via B4 Relay
2026-03-27 8:45 ` Petr Mladek
2026-03-27 9:17 ` Andy Shevchenko
2026-03-27 10:11 ` Rodrigo Alencar
2026-03-27 10:21 ` Andy Shevchenko [this message]
2026-03-27 15:17 ` Rodrigo Alencar
2026-03-30 12:49 ` Rodrigo Alencar
2026-03-31 13:01 ` Rodrigo Alencar
2026-04-12 15:05 ` Jonathan Cameron
2026-04-01 12:22 ` Petr Mladek
2026-04-01 13:16 ` Rodrigo Alencar
2026-03-27 10:44 ` David Laight
2026-03-27 10:57 ` Andy Shevchenko
2026-03-27 13:28 ` David Laight
2026-03-27 9:24 ` Rodrigo Alencar
2026-03-20 16:27 ` [PATCH v9 3/9] iio: core: add fixed point parsing with 64-bit parts Rodrigo Alencar via B4 Relay
2026-03-20 16:27 ` [PATCH v9 4/9] iio: test: add kunit test for fixed-point parsing Rodrigo Alencar via B4 Relay
2026-03-20 16:27 ` [PATCH v9 5/9] iio: frequency: adf41513: driver implementation Rodrigo Alencar via B4 Relay
2026-03-20 16:27 ` [PATCH v9 6/9] iio: frequency: adf41513: handle LE synchronization feature Rodrigo Alencar via B4 Relay
2026-03-20 16:27 ` [PATCH v9 7/9] iio: frequency: adf41513: features on frequency change Rodrigo Alencar via B4 Relay
2026-03-20 16:27 ` [PATCH v9 8/9] docs: iio: add documentation for adf41513 driver Rodrigo Alencar via B4 Relay
2026-03-20 16:27 ` [PATCH v9 9/9] Documentation: ABI: testing: add common ABI file for iio/frequency Rodrigo Alencar via B4 Relay
2026-03-22 11:33 ` [PATCH v9 0/9] ADF41513/ADF41510 PLL frequency synthesizers Jonathan Cameron
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=acZaGUV0MwuHNDru@ashevche-desk.local \
--to=andriy.shevchenko@linux.intel.com \
--cc=455.rodrigo.alencar@gmail.com \
--cc=Michael.Hennerich@analog.com \
--cc=akpm@linux-foundation.org \
--cc=andy@kernel.org \
--cc=conor+dt@kernel.org \
--cc=corbet@lwn.net \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-doc@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=pmladek@suse.com \
--cc=robh@kernel.org \
--cc=rodrigo.alencar@analog.com \
--cc=rostedt@goodmis.org \
--cc=senozhatsky@chromium.org \
--cc=skhan@linuxfoundation.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®