From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=45793 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OHE7g-0003SS-2Z for qemu-devel@nongnu.org; Wed, 26 May 2010 06:46:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OHE7e-0002hS-GD for qemu-devel@nongnu.org; Wed, 26 May 2010 06:46:03 -0400 Received: from mx1.redhat.com ([209.132.183.28]:14138) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OHE7e-0002h3-87 for qemu-devel@nongnu.org; Wed, 26 May 2010 06:46:02 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o4QAk04I020976 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 26 May 2010 06:46:00 -0400 Date: Wed, 26 May 2010 11:45:58 +0100 From: "Daniel P. Berrange" Subject: Re: [Qemu-devel] [PATCH] qdev-properties: Fix (u)intXX parsers Message-ID: <20100526104558.GL18547@redhat.com> References: <1274869693-22884-1-git-send-email-kwolf@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1274869693-22884-1-git-send-email-kwolf@redhat.com> Reply-To: "Daniel P. Berrange" List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Kevin Wolf Cc: armbru@redhat.com, qemu-devel@nongnu.org, kraxel@redhat.com On Wed, May 26, 2010 at 12:28:13PM +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(-) > > diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c > index 9ffdba7..9a61ca2 100644 > --- a/hw/qdev-properties.c > +++ b/hw/qdev-properties.c > @@ -68,12 +68,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 != str + strlen(str)) { > return -EINVAL; > + } I think you can avoid the O(n) operation here & in the other cases with a test like this: if ((end == str) || (*end != '\0')) return -EINVAL Regards, Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|