qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] qdev-properties: Fix (u)intXX parsers
@ 2010-05-26 10:28 Kevin Wolf
  2010-05-26 10:45 ` Daniel P. Berrange
  2010-05-26 11:13 ` Markus Armbruster
  0 siblings, 2 replies; 5+ messages in thread
From: Kevin Wolf @ 2010-05-26 10:28 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, kraxel, armbru

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 <kwolf@redhat.com>
---
 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;
+    }
+
     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 != str + strlen(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 != str + strlen(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 != str + strlen(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 != str + strlen(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 != str + strlen(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 != str + strlen(str)) {
         return -EINVAL;
+    }
+
     return 0;
 }
 
-- 
1.6.6.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-05-26 11:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-26 10:28 [Qemu-devel] [PATCH] qdev-properties: Fix (u)intXX parsers Kevin Wolf
2010-05-26 10:45 ` Daniel P. Berrange
2010-05-26 10:54   ` Kevin Wolf
2010-05-26 11:13 ` Markus Armbruster
2010-05-26 11:19   ` Kevin Wolf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).