qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: David Hildenbrand <david@redhat.com>, qemu-devel@nongnu.org
Cc: Markus Armbruster <armbru@redhat.com>,
	Michael Roth <mdroth@linux.vnet.ibm.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v2 2/9] cutils: Fix qemu_strtosz() & friends to reject non-finite sizes
Date: Tue, 20 Nov 2018 10:29:39 -0600	[thread overview]
Message-ID: <7d8d4e4f-bb70-5047-4e37-ed3672c89e02@redhat.com> (raw)
In-Reply-To: <20181120092542.13102-3-david@redhat.com>

On 11/20/18 3:25 AM, David Hildenbrand wrote:
> qemu_strtosz() & friends reject NaNs, but happily accept inifities.

s/inifities/infinities/

> They shouldn't. Fix that.
> 
> The fix makes use of qemu_strtod_finite(). To avoid ugly casts,
> change the @end parameter of qemu_strtosz() & friends from char **
> to const char **.
> 
> Also, add two test cases, testing that "inf" and "NaN" are properly
> rejected.
> 
> Signed-off-by: David Hildenbrand <david@redhat.com>
> ---
>   include/qemu/cutils.h |  6 +++---
>   monitor.c             |  2 +-
>   tests/test-cutils.c   | 24 +++++++++++++++++-------
>   util/cutils.c         | 16 +++++++---------
>   4 files changed, 28 insertions(+), 20 deletions(-)
> 

> +++ b/util/cutils.c
> @@ -206,20 +206,18 @@ static int64_t suffix_mul(char suffix, int64_t unit)
>    * in *end, if not NULL. Return -ERANGE on overflow, Return -EINVAL on

Pre-existing, but since you're touching this area: the second 'Return' 
is unusual capitalization for being mid-sentence.  You could even 
s/Return/of/

>    * other error.
>    */
> -static int do_strtosz(const char *nptr, char **end,
> +static int do_strtosz(const char *nptr, const char **end,
>                         const char default_suffix, int64_t unit,
>                         uint64_t *result)
>   {
>       int retval;
> -    char *endptr;
> +    const char *endptr;
>       unsigned char c;
>       int mul_required = 0;
>       double val, mul, integral, fraction;
>   
> -    errno = 0;
> -    val = strtod(nptr, &endptr);
> -    if (isnan(val) || endptr == nptr || errno != 0) {
> -        retval = -EINVAL;
> +    retval = qemu_strtod_finite(nptr, &endptr, &val);
> +    if (retval) {
>           goto out;

Here, retval can be -EINVAL (for failure to parse, or encountering "inf" 
or "NaN") or -ERANGE (overflow, underflow)...

>       }
>       fraction = modf(val, &integral);
> @@ -259,17 +257,17 @@ out:

out:
     if (end) {
         *end = endptr;
     } else if (*endptr) {
         retval = -EINVAL;
     }

>       return retval;

...if the failure was -EINVAL due to trailing garbage or empty string, 
nothing changes. If the failure was -EINVAL due to "inf", and the user 
passed in 'end', then 'end' now points to the beginning of "inf" instead 
of the end (probably okay). If the failure was -EINVAL due to "inf" and 
the user gave NULL for 'end', then we slam retval back to -EINVAL (no 
change).  If the failure was -ERANGE, then there is no trailing garbage, 
so *endptr had better be NULL, and we still fail with -ERANGE.  Any 
other way to reach the out label is unchanged from earlier logic.

It's some hairy code to think about, but I can't find anything wrong 
with it.  Typo fixes are minor, so

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

  reply	other threads:[~2018-11-20 16:29 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-20  9:25 [Qemu-devel] [PATCH v2 0/9] qapi: rewrite string-input-visitor David Hildenbrand
2018-11-20  9:25 ` [Qemu-devel] [PATCH v2 1/9] cutils: Add qemu_strtod() and qemu_strtod_finite() David Hildenbrand
2018-11-20 16:13   ` Eric Blake
2018-11-20 20:07     ` Markus Armbruster
2018-11-21 10:35       ` David Hildenbrand
2018-11-21 14:00         ` Markus Armbruster
2018-11-21 17:16         ` Eric Blake
2018-11-20  9:25 ` [Qemu-devel] [PATCH v2 2/9] cutils: Fix qemu_strtosz() & friends to reject non-finite sizes David Hildenbrand
2018-11-20 16:29   ` Eric Blake [this message]
2018-11-20 20:31     ` Markus Armbruster
2018-11-20 20:41       ` Eric Blake
2018-11-21 10:44         ` David Hildenbrand
2018-11-21 14:16           ` Markus Armbruster
2018-11-21 17:25           ` Eric Blake
2018-11-20  9:25 ` [Qemu-devel] [PATCH v2 3/9] qapi: Fix string-input-visitor to reject NaN and infinities David Hildenbrand
2018-11-20 20:34   ` Markus Armbruster
2018-11-20  9:25 ` [Qemu-devel] [PATCH v2 4/9] qapi: Use qemu_strtod_finite() in qobject-input-visitor David Hildenbrand
2018-11-20  9:25 ` [Qemu-devel] [PATCH v2 5/9] test-string-input-visitor: Add more tests David Hildenbrand
2018-11-20 17:06   ` Eric Blake
2018-11-20 17:20     ` Eric Blake
2018-11-20 17:26       ` Eric Blake
2018-11-20 20:46         ` Markus Armbruster
2018-11-21 10:49         ` David Hildenbrand
2018-11-21 14:09           ` Markus Armbruster
2018-11-20  9:25 ` [Qemu-devel] [PATCH v2 6/9] qapi: Rewrite string-input-visitor David Hildenbrand
2018-11-20 17:40   ` Eric Blake
2018-11-20 20:58     ` Markus Armbruster
2018-11-21 10:53       ` David Hildenbrand
2018-11-21 14:12         ` Markus Armbruster
2018-11-20  9:25 ` [Qemu-devel] [PATCH v2 7/9] test-string-input-visitor: Use virtual walk David Hildenbrand
2018-11-20 17:41   ` Eric Blake
2018-11-20  9:25 ` [Qemu-devel] [PATCH v2 8/9] test-string-input-visitor: Split off uint64 list tests David Hildenbrand
2018-11-20  9:25 ` [Qemu-devel] [PATCH v2 9/9] test-string-input-visitor: Add range overflow tests David Hildenbrand

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=7d8d4e4f-bb70-5047-4e37-ed3672c89e02@redhat.com \
    --to=eblake@redhat.com \
    --cc=armbru@redhat.com \
    --cc=david@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=pbonzini@redhat.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).