qemu-devel
[Top][All Lists]
Advanced

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

Re: [Qemu-devel] [PATCH v4 2/9] cutils: add qemu_strtoi & qemu_strtoui p


From: Daniel P . Berrangé
Subject: Re: [Qemu-devel] [PATCH v4 2/9] cutils: add qemu_strtoi & qemu_strtoui parsers for int/unsigned int types
Date: Wed, 7 Feb 2018 16:29:36 +0000
User-agent: Mutt/1.9.1 (2017-09-22)

On Mon, Feb 05, 2018 at 01:37:21PM -0600, Eric Blake wrote:
> On 02/05/2018 09:24 AM, 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: Marc-André Lureau <address@hidden>
> > Signed-off-by: Daniel P. Berrange <address@hidden>
> > ---
> 
> > +++ b/util/cutils.c
> > @@ -297,6 +297,110 @@ static int check_strtox_error(const char *nptr, char 
> > *ep,
> >       return -libc_errno;
> >   }
> > +/**
> > + * Convert string @nptr to a long integer, and store it in @result.
> 
> s/a long/an/
> 
> > + */
> > +int qemu_strtoi(const char *nptr, const char **endptr, int base,
> > +                int *result)
> > +{
> > +    char *ep;
> > +    long lresult;
> > +
> > +    if (!nptr) {
> > +        if (endptr) {
> > +            *endptr = nptr;
> > +        }
> > +        return -EINVAL;
> > +    }
> > +
> > +    errno = 0;
> > +    lresult = strtol(nptr, &ep, base);
> > +    if (lresult < INT_MIN) {
> > +        *result = INT_MIN;
> > +    } else if (lresult > INT_MAX) {
> > +        *result = INT_MAX;
> 
> On 64-bit platforms, this clamps the result, but does not set errno, for
> values beyond int but still within the range of long.  Which is different
> than what it does on 32-bit platforms.  Gross.  The testsuite is missing
> coverage of this, which ideally would be the same behavior (setting
> errno=ERANGE) on both platforms.

Opps, yes, bad, I'll fix it to set ERANGE and figure out some test.

I notice the qemu_strtoui is also broken on 32-bit because I mistakenly
used  strtol instead of strtoul, so didn't handled full range of unsigned
int values.

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]