Skip to contentSkip to conversation

qdev-properties: Fix (u)intXX parsers

PATCH RESEND Applied Kevin Wolf 5 participants 5 messages Jun 18, 2010 – Jul 6, 2010

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH RESEND 1/1 qdev-properties: Fix (u)intXX parsers

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).

---
 hw/qdev-properties.c |   50 +++++++++++++++++++++++++++++++++++---------------
 1 files changed, 35 insertions(+), 15 deletions(-)
hw/qdev-properties.c +35 -15
index 5a8739d..5b7fd77 100644
@@ -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;
 }
 
Signature · 1 line
1.6.6.1
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Kevin Wolf

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).

---
 hw/qdev-properties.c |   50 +++++++++++++++++++++++++++++++++++---------------
 1 files changed, 35 insertions(+), 15 deletions(-)

r~

#2 lore raw
In reply to Kevin Wolf
Quoted 6 lines · Kevin Wolf

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).

Ping... this secures an open deathtrap, please apply.

#3 lore raw
In reply to Kevin Wolf
Quoted 9 lines · Kevin Wolf

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).

---
 hw/qdev-properties.c |   50 +++++++++++++++++++++++++++++++++++---------------
 1 files changed, 35 insertions(+), 15 deletions(-)

Thanks, applied.

Quoted 126 lines
hw/qdev-properties.c +35 -15
index 5a8739d..5b7fd77 100644
@@ -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;
 }
 
Signature · 1 line
1.6.6.1
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#4 lore raw
In reply to Kevin Wolf
Quoted 7 lines · Kevin Wolf

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).

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 126 lines
---
  hw/qdev-properties.c |   50 +++++++++++++++++++++++++++++++++++---------------
  1 files changed, 35 insertions(+), 15 deletions(-)
hw/qdev-properties.c +35 -15
index 5a8739d..5b7fd77 100644
@@ -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;
  }