From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=60123 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OU4Vx-0001Ij-7y for qemu-devel@nongnu.org; Wed, 30 Jun 2010 17:08:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OU4Vv-0005ES-Nl for qemu-devel@nongnu.org; Wed, 30 Jun 2010 17:08:13 -0400 Received: from hall.aurel32.net ([88.191.82.174]:49499) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OU4Vv-0005Di-Ba for qemu-devel@nongnu.org; Wed, 30 Jun 2010 17:08:11 -0400 Date: Wed, 30 Jun 2010 22:59:55 +0200 From: Aurelien Jarno Subject: Re: [Qemu-devel] [PATCH][RESEND] qdev-properties: Fix (u)intXX parsers Message-ID: <20100630205955.GA29976@ohm.aurel32.net> References: <1276878423-19955-1-git-send-email-kwolf@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline In-Reply-To: <1276878423-19955-1-git-send-email-kwolf@redhat.com> List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Kevin Wolf Cc: qemu-devel@nongnu.org On Fri, Jun 18, 2010 at 06:27:03PM +0200, Kevin Wolf wrote: > scanf calls must not use PRI constants, they have probably the wrong size and > corrupt memory. We could replace them by SCN ones, but strtol is simpler than > scanf here anyway. While at it, also fix the parsers to reject garbage after > the number ("4096xyz" was accepted before). > > Signed-off-by: Kevin Wolf > --- > hw/qdev-properties.c | 50 +++++++++++++++++++++++++++++++++++--------------- > 1 files changed, 35 insertions(+), 15 deletions(-) Thanks, applied. > diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c > index 5a8739d..5b7fd77 100644 > --- a/hw/qdev-properties.c > +++ b/hw/qdev-properties.c > @@ -67,12 +67,14 @@ PropertyInfo qdev_prop_bit = { > static int parse_uint8(DeviceState *dev, Property *prop, const char *str) > { > uint8_t *ptr = qdev_get_prop_ptr(dev, prop); > - const char *fmt; > + char *end; > > /* accept both hex and decimal */ > - fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx8 : "%" PRIu8; > - if (sscanf(str, fmt, ptr) != 1) > + *ptr = strtoul(str, &end, 0); > + if ((*end != '\0') || (end == str)) { > return -EINVAL; > + } > + > return 0; > } > > @@ -95,12 +97,14 @@ PropertyInfo qdev_prop_uint8 = { > static int parse_uint16(DeviceState *dev, Property *prop, const char *str) > { > uint16_t *ptr = qdev_get_prop_ptr(dev, prop); > - const char *fmt; > + char *end; > > /* accept both hex and decimal */ > - fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx16 : "%" PRIu16; > - if (sscanf(str, fmt, ptr) != 1) > + *ptr = strtoul(str, &end, 0); > + if ((*end != '\0') || (end == str)) { > return -EINVAL; > + } > + > return 0; > } > > @@ -123,12 +127,14 @@ PropertyInfo qdev_prop_uint16 = { > static int parse_uint32(DeviceState *dev, Property *prop, const char *str) > { > uint32_t *ptr = qdev_get_prop_ptr(dev, prop); > - const char *fmt; > + char *end; > > /* accept both hex and decimal */ > - fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx32 : "%" PRIu32; > - if (sscanf(str, fmt, ptr) != 1) > + *ptr = strtoul(str, &end, 0); > + if ((*end != '\0') || (end == str)) { > return -EINVAL; > + } > + > return 0; > } > > @@ -149,9 +155,13 @@ PropertyInfo qdev_prop_uint32 = { > static int parse_int32(DeviceState *dev, Property *prop, const char *str) > { > int32_t *ptr = qdev_get_prop_ptr(dev, prop); > + char *end; > > - if (sscanf(str, "%" PRId32, ptr) != 1) > + *ptr = strtol(str, &end, 10); > + if ((*end != '\0') || (end == str)) { > return -EINVAL; > + } > + > return 0; > } > > @@ -174,9 +184,13 @@ PropertyInfo qdev_prop_int32 = { > static int parse_hex32(DeviceState *dev, Property *prop, const char *str) > { > uint32_t *ptr = qdev_get_prop_ptr(dev, prop); > + char *end; > > - if (sscanf(str, "%" PRIx32, ptr) != 1) > + *ptr = strtoul(str, &end, 16); > + if ((*end != '\0') || (end == str)) { > return -EINVAL; > + } > + > return 0; > } > > @@ -199,12 +213,14 @@ PropertyInfo qdev_prop_hex32 = { > static int parse_uint64(DeviceState *dev, Property *prop, const char *str) > { > uint64_t *ptr = qdev_get_prop_ptr(dev, prop); > - const char *fmt; > + char *end; > > /* accept both hex and decimal */ > - fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx64 : "%" PRIu64; > - if (sscanf(str, fmt, ptr) != 1) > + *ptr = strtoull(str, &end, 0); > + if ((*end != '\0') || (end == str)) { > return -EINVAL; > + } > + > return 0; > } > > @@ -227,9 +243,13 @@ PropertyInfo qdev_prop_uint64 = { > static int parse_hex64(DeviceState *dev, Property *prop, const char *str) > { > uint64_t *ptr = qdev_get_prop_ptr(dev, prop); > + char *end; > > - if (sscanf(str, "%" PRIx64, ptr) != 1) > + *ptr = strtoull(str, &end, 16); > + if ((*end != '\0') || (end == str)) { > return -EINVAL; > + } > + > return 0; > } > > -- > 1.6.6.1 > > > -- Aurelien Jarno GPG: 1024D/F1BCDB73 aurelien@aurel32.net http://www.aurel32.net