Re: [PATCH v9 2/9] lib: vsprintf: export simple_strntoull() in a safe prototype

From: David Laight

Date: Fri Mar 27 2026 - 09:28:46 EST


On Fri, 27 Mar 2026 12:57:56 +0200
Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:

> On Fri, Mar 27, 2026 at 10:44:40AM +0000, David Laight wrote:
,,,
> > > 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
> > > }
> >
> > Are those 'fraction' counts of (say) 10^-6 (like times in seconds+usecs)
> > or true binary values where the value could be treated as a u64 (or u128)
> > for addition and subtraction.
>
> It depends. IIO has scale on top of that, so the fraction part can be 10⁻³,
> 10⁻⁶, 10⁻⁹. I don't remember by heart if the ABI requires all digits to be
> placed, I think we don't require that.

Seems like you want this function (untested):
u64 strtofrac(const char *buf, const char **end, unsigned int len)
{
u64 val = 0;
unsigned int digit;

while (len--) {
digit = *buf - '0';
if (digit <= 9) {
buf++;
val += digit;
}
val *= 10;
}
while (*buf - '0' <= 9u)
buf++;
*end = buf;
return val;
}

David