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" <berra...@redhat.com> > > > > 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 <marcandre.lur...@redhat.com> > > Signed-off-by: Daniel P. Berrange <berra...@redhat.com> > > --- > > > +++ 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 :|