Received: from eggs.gnu.org ([208.118.235.92]:58522) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvWr3-0000jd-Rk for qemu-devel@nongnu.org; Wed, 16 Jan 2013 12:32:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TvWr2-0001Bi-Jt for qemu-devel@nongnu.org; Wed, 16 Jan 2013 12:32:49 -0500 Received: from mx1.redhat.com ([209.132.183.28]:60158) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvWr2-0001Be-D8 for qemu-devel@nongnu.org; Wed, 16 Jan 2013 12:32:48 -0500 Date: Wed, 16 Jan 2013 15:33:36 -0200 From: Eduardo Habkost Message-ID: <20130116173336.GC10683@otherpad.lan.raisama.net> References: <1358349851-20960-1-git-send-email-ehabkost@redhat.com> <1358349851-20960-2-git-send-email-ehabkost@redhat.com> <50F6DF12.8030703@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <50F6DF12.8030703@redhat.com> Subject: Re: [Qemu-devel] [PATCH 1/8] cutils: unsigned int parsing functions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: Laszlo Ersek , Chegu Vinod , qemu-devel@nongnu.org, Anthony Liguori On Wed, Jan 16, 2013 at 10:10:42AM -0700, Eric Blake wrote: > On 01/16/2013 08:24 AM, Eduardo Habkost wrote: > > There are lots of duplicate parsing code using strto*() in QEMU, and > > most of that code is broken in one way or another. Even the visitors > > code have duplicate integer parsing code[1]. This introduces functions > > to help parsing unsigned int values: parse_uint() and parse_uint_full(). > > > > Parsing functions for signed ints and floats will be submitted later. > > > > parse_uint_full() has all the checks made by opts_type_uint64() at > > opts-visitor.c: > > > > - Check for NULL (returns -EINVAL) > > - Check for negative numbers (returns -ERANGE) > > - Check for empty string (returns -EINVAL) > > - Check for overflow or other errno values set by strtoll() (returns > > -errno) > > - Check for end of string (reject invalid characters after number) > > (returns -EINVAL) > > > > parse_uint() does everything above except checking for the end of the > > string, so callers can continue parsing the remainder of string after > > the number. > > > > Unit tests included. > > > > [1] string-input-visitor.c:parse_int() could use the same parsing code > > used by opts-visitor.c:opts_type_int(), instead of duplicating that > > logic. > > > > Signed-off-by: Eduardo Habkost > > --- > > Cc: Laszlo Ersek > > Cc: Eric Blake > > --- > > > +++ b/tests/test-cutils.c > > > + * > > + * Permission is hereby granted, free of charge, to any person obtaining a copy > > Interesting that you chose a BSD license instead of GPL, but doesn't > affect my review. It's a personal preference, but by coincidence is also the license of cutils.c. > > Your test case lacks test of octal or hexadecimal input strings; is that > worth adding? I believe I trust strtoll() enough to not require tests for those cases. Just like I didn't add tests involving trailing spaces or tabs, except on the negative-number test case. > > +++ b/util/cutils.c > > @@ -270,6 +270,82 @@ int64_t strtosz(const char *nptr, char **end) > > return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB); > > } > > > > +/* Try to parse an unsigned integer > > + * > > + * Error checks done by the function: > > + * - NULL pointer will return -EINVAL. > > + * - Empty strings will return -EINVAL. > > + * - Overflow errors or other errno values set by strtoull() will > > + * return -errno (-ERANGE in case of overflow). > > + * - Differently from strtoull(), values starting with a minus sign are > > + * rejected (returning -ERANGE). > > Interesting that you chose to reject negative numbers, even though > strtoull() is required to accept them. But you documented it and tested > for it, so I can live with it. I find this behavior of strtoull() confusing for users. If an option/field requires positive numbers, I expect QEMU to reject "-1" instead of silently converting it to ULLONG_MAX. > > > + errno = 0; > > + val = strtoull(s, &endp, 0); > > + if (errno) { > > + r = -errno; > > Why two spaces? Typo. I will send a fixed version (keeping your Reviewed-by, if you don't mind). > > Reviewed-by: Eric Blake Thanks! -- Eduardo