qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PULL 2/9] cutils: add qemu_strtoi & qemu_strtoui parse


From: Daniel P . Berrangé
Subject: Re: [Qemu-devel] [PULL 2/9] cutils: add qemu_strtoi & qemu_strtoui parsers for int/unsigned int types
Date: Tue, 13 Mar 2018 18:11:34 +0000
User-agent: Mutt/1.9.2 (2017-12-15)

On Mon, Mar 12, 2018 at 08:12:58PM +0000, Daniel P. Berrangé wrote:
> From: "Daniel P. Berrange" <address@hidden>
> 
> There are qemu_strtoNN functions for various sized integers. This adds two
> more for plain int & unsigned int types, with suitable range checking.
> 
> Reviewed-by: Eric Blake <address@hidden>
> Reviewed-by: Marc-André Lureau <address@hidden>
> Signed-off-by: Daniel P. Berrange <address@hidden>
> diff --git a/util/cutils.c b/util/cutils.c
> index b33ede83d1..774d5f7362 100644
> --- a/util/cutils.c
> +++ b/util/cutils.c
> +int qemu_strtoui(const char *nptr, const char **endptr, int base,
> +                 unsigned int *result)
> +{
> +    char *ep;
> +    long lresult;
> +
> +    if (!nptr) {
> +        if (endptr) {
> +            *endptr = nptr;
> +        }
> +        return -EINVAL;
> +    }
> +
> +    errno = 0;
> +    lresult = strtoul(nptr, &ep, base);

The test failure on 32-bit is caused by use of strtoul instead
of strtoull here, so I'll just switch them, so the logic that
follows below actually works on 32-bit.

> +    /* Windows returns 1 for negative out-of-range values.  */
> +    if (errno == ERANGE) {
> +        *result = -1;
> +    } else {
> +        if (lresult > UINT_MAX) {
> +            *result = UINT_MAX;
> +            errno = ERANGE;
> +        } else if (lresult < INT_MIN) {
> +            *result = UINT_MAX;
> +            errno = ERANGE;
> +        } else {
> +            *result = lresult;
> +        }
> +    }
> +    return check_strtox_error(nptr, ep, endptr, errno);
> +}
> +
>  /**
>   * Convert string @nptr to a long integer, and store it in @result.
>   *
> -- 
> 2.14.3
> 

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



reply via email to

[Prev in Thread] Current Thread [Next in Thread]