From patchwork Wed May 26 11:27:45 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 53610 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 8A0CDB7D12 for ; Wed, 26 May 2010 21:28:58 +1000 (EST) Received: from localhost ([127.0.0.1]:43447 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OHEn9-0006GT-9O for incoming@patchwork.ozlabs.org; Wed, 26 May 2010 07:28:55 -0400 Received: from [140.186.70.92] (port=49002 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OHEmN-0006FJ-1V for qemu-devel@nongnu.org; Wed, 26 May 2010 07:28:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OHEmL-0003TY-Sa for qemu-devel@nongnu.org; Wed, 26 May 2010 07:28:06 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38854) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OHEmL-0003TB-IB for qemu-devel@nongnu.org; Wed, 26 May 2010 07:28:05 -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 o4QBS4So027198 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 26 May 2010 07:28:04 -0400 Received: from localhost.localdomain (dhcp-5-217.str.redhat.com [10.32.5.217]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o4QBS2ZT018264; Wed, 26 May 2010 07:28:03 -0400 From: Kevin Wolf To: qemu-devel@nongnu.org Date: Wed, 26 May 2010 13:27:45 +0200 Message-Id: <1274873265-24393-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: kwolf@redhat.com, kraxel@redhat.com, armbru@redhat.com Subject: [Qemu-devel] [PATCH v2] qdev-properties: Fix (u)intXX parsers X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 Acked-by: Gerd Hoffmann --- 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..64d83ce 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 != '\0') || (end == str)) { return -EINVAL; + } + return 0; } @@ -96,12 +98,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; } @@ -124,12 +128,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; } @@ -150,9 +156,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; } @@ -175,9 +185,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; } @@ -200,12 +214,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; } @@ -228,9 +244,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; }