From: Tao Xu <tao3.xu@intel.com>
To: Christophe de Dinechin <dinechin@redhat.com>
Cc: "qemu-devel@nongnu.org" <qemu-devel@nongnu.org>,
Markus Armbruster <armbru@redhat.com>,
"ehabkost@redhat.com" <ehabkost@redhat.com>,
"mdroth@linux.vnet.ibm.com" <mdroth@linux.vnet.ibm.com>
Subject: Re: [PATCH RESEND v2] util/cutils: Expand do_strtosz parsing precision to 64 bits
Date: Wed, 18 Dec 2019 09:40:51 +0800 [thread overview]
Message-ID: <0ceebef1-de21-a2e9-6d38-2f51350dcd67@intel.com> (raw)
In-Reply-To: <E002371E-11D7-4482-9213-DF2C8F12FC4B@redhat.com>
On 12/17/2019 7:44 PM, Christophe de Dinechin wrote:
>
>
>> On 9 Dec 2019, at 09:30, Tao Xu <tao3.xu@intel.com> wrote:
>>
>> Parse input string both as a double and as a uint64_t, then use the
>> method which consumes more characters. Update the related test cases.
>>
>> Signed-off-by: Tao Xu <tao3.xu@intel.com>
>> ---
>>
>> Changes in v2:
>> - Resend to use double small than DBL_MIN
>> - Add more test case for double overflow and underflow.
>> - Set mul as int64_t (Markus)
>> - Restore endptr (Markus)
>> ---
>> tests/test-cutils.c | 37 +++++++----------------
>> tests/test-keyval.c | 47 +++++------------------------
>> tests/test-qemu-opts.c | 39 +++++-------------------
>> util/cutils.c | 67 +++++++++++++++++++++++++++++++-----------
>> 4 files changed, 75 insertions(+), 115 deletions(-)
>>
[...]
>> + /*
>> + * Parse @nptr both as a double and as a uint64_t, then use the method
>> + * which consumes more characters.
>> + */
>
> Why do ever need to parse as double if you have uint64?
>
Because we want to keep do_strtosz Compatible with double input (such as
1.5k).
>> + retd = qemu_strtod_finite(nptr, &suffixd, &vald);
>> + retu = qemu_strtou64(nptr, &suffixu, 0, &valu);
>> + use_strtod = strlen(suffixd) < strlen(suffixu);
>
> You could simply compare suffixd and suffixu:
>
> use_strtod = suffixd > suffixu;
>
Thank you for your suggestion.
>> +
>> + if (use_strtod) {
>> + endptr = suffixd;
>> + retval = retd;
>> + } else {
>> + endptr = suffixu;
>> + retval = retu;
>> + }
>>
>> - retval = qemu_strtod_finite(nptr, &endptr, &val);
>> if (retval) {
>> goto out;
>> }
>> - fraction = modf(val, &integral);
>> - if (fraction != 0) {
>> - mul_required = 1;
>> + if (use_strtod) {
>> + fraction = modf(vald, &integral);
>> + if (fraction != 0) {
>> + mul_required = 1;
>> + }
>> }
>> c = *endptr;
>> mul = suffix_mul(c, unit);
>> @@ -238,17 +258,30 @@ static int do_strtosz(const char *nptr, const char **end,
>> retval = -EINVAL;
>> goto out;
>> }
>> - /*
>> - * Values near UINT64_MAX overflow to 2**64 when converting to double
>> - * precision. Compare against the maximum representable double precision
>> - * value below 2**64, computed as "the next value after 2**64 (0x1p64) in
>> - * the direction of 0".
>> - */
>> - if ((val * mul > nextafter(0x1p64, 0)) || val < 0) {
>> - retval = -ERANGE;
>> - goto out;
>> +
>> + if (use_strtod) {
>> + /*
>> + * Values near UINT64_MAX overflow to 2**64 when converting to double
>> + * precision. Compare against the maximum representable double precision
>> + * value below 2**64, computed as "the next value after 2**64 (0x1p64)
>> + * in the direction of 0".
>> + */
>> + if ((vald * mul > nextafter(0x1p64, 0)) || vald < 0) {
>> + retval = -ERANGE;
>> + goto out;
>> + }
>> + *result = vald * mul;
>> + } else {
>> + /* Reject negative input and overflow output */
>> + while (qemu_isspace(*nptr)) {
>> + nptr++;
>> + }
>> + if (*nptr == '-' || UINT64_MAX / mul < valu) {
>> + retval = -ERANGE;
>> + goto out;
>> + }
>> + *result = valu * mul;
>> }
>> - *result = val * mul;
>> retval = 0;
>>
>> out:
>> --
>> 2.20.1
>>
>>
>
prev parent reply other threads:[~2019-12-18 1:41 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-09 8:30 [PATCH RESEND v2] util/cutils: Expand do_strtosz parsing precision to 64 bits Tao Xu
2019-12-17 6:51 ` Tao Xu
2019-12-17 11:44 ` Christophe de Dinechin
2019-12-18 1:40 ` Tao Xu [this message]
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=0ceebef1-de21-a2e9-6d38-2f51350dcd67@intel.com \
--to=tao3.xu@intel.com \
--cc=armbru@redhat.com \
--cc=dinechin@redhat.com \
--cc=ehabkost@redhat.com \
--cc=mdroth@linux.vnet.ibm.com \
--cc=qemu-devel@nongnu.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;
as well as URLs for NNTP newsgroup(s).