From: David Laight <David.Laight@ACULAB.COM>
To: 'David Disseldorp' <ddiss@suse.de>, Qu Wenruo <wqu@suse.com>
Cc: "linux-btrfs@vger.kernel.org" <linux-btrfs@vger.kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: RE: [PATCH 1/2] lib/strtox: introduce kstrtoull_suffix() helper
Date: Tue, 19 Dec 2023 16:42:11 +0000 [thread overview]
Message-ID: <8095c6ae5f8d412d8e6ff95707961a08@AcuMS.aculab.com> (raw)
In-Reply-To: <20231218235946.32ab7a69@echidna>
From: David Disseldorp
> Sent: 18 December 2023 13:00
>
> On Fri, 15 Dec 2023 19:09:23 +1030, Qu Wenruo wrote:
>
> > Just as mentioned in the comment of memparse(), the simple_stroull()
> > usage can lead to overflow all by itself.
> >
> > Furthermore, the suffix calculation is also super overflow prone because
> > that some suffix like "E" itself would eat 60bits, leaving only 4 bits
> > available.
> >
> > And that suffix "E" can also lead to confusion since it's using the same
> > char of hex Ox'E'.
> >
> > One simple example to expose all the problem is to use memparse() on
> > "25E".
> > The correct value should be 28823037615171174400, but the suffix E makes
> > it super simple to overflow, resulting the incorrect value
> > 10376293541461622784 (9E).
Some more bikeshed paint :-)
...
> > + ret = _kstrtoull(s, base, &init_value, &endptr);
> > + /* Either already overflow or no number string at all. */
> > + if (ret < 0)
> > + return ret;
> > + final_value = init_value;
> > + /* No suffixes. */
> > + if (!*endptr)
> > + goto done;
How about:
suffix = *endptr;
if (!strchr(suffixes, suffix))
return -ENIVAL;
shift = strcspn("KkMmGgTtPp", suffix)/2 * 10 + 10;
if (shift > 50)
return -EINVAL;
if (value >> (64 - shift))
return -EOVERFLOW;
value <<= shift;
Although purists might want to multiply by 1000 not 1024.
And SI multipliers are all upper-case - except k.
...
> > + /* Overflow check. */
> > + if (final_value < init_value)
> > + return -EOVERFLOW;
That is just plain wrong.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
next prev parent reply other threads:[~2023-12-19 16:42 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-15 8:39 [PATCH 0/2] lib/kstrtox: introduce kstrtoull_suffix() helper Qu Wenruo
2023-12-15 8:39 ` [PATCH 1/2] lib/strtox: " Qu Wenruo
2023-12-18 12:59 ` David Disseldorp
2023-12-18 19:52 ` Qu Wenruo
2023-12-19 3:17 ` David Disseldorp
2023-12-19 16:42 ` David Laight [this message]
2023-12-19 21:17 ` Qu Wenruo
2023-12-20 8:31 ` David Laight
2023-12-20 9:32 ` Qu Wenruo
2023-12-15 8:39 ` [PATCH 2/2] btrfs: sysfs: use kstrtoull_suffix() to replace memparse() Qu Wenruo
2023-12-18 7:49 ` David Disseldorp
2023-12-18 8:11 ` Qu Wenruo
-- strict thread matches above, loose matches on Subject: below --
2023-12-18 13:44 [PATCH 1/2] lib/strtox: introduce kstrtoull_suffix() helper Andy Shevchenko
2023-12-20 9:54 Alexey Dobriyan
2023-12-20 10:01 ` Qu Wenruo
2023-12-20 14:24 Andy Shevchenko
2023-12-20 20:38 ` Qu Wenruo
2023-12-21 12:00 ` Andy Shevchenko
2023-12-21 20:37 ` Qu Wenruo
2023-12-21 20:55 ` 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=8095c6ae5f8d412d8e6ff95707961a08@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=christophe.jaillet@wanadoo.fr \
--cc=ddiss@suse.de \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=wqu@suse.com \
/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