qdev-properties: Fix (u)intXX parsers
PATCH 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
@@ -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 ; }
Signature · 1 line
1.6.6.1
Quoted 18 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).
Signed-off-by Kevin Wolf <[email protected]>--- hw/qdev-properties.c | 50 +++++++++++++++++++++++++++++++++++--------------- 1 files changed, 35 insertions(+), 15 deletions(-)hw/qdev-properties.c +0
-0@@ -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);
hw/qdev-properties.c PropertyInfo qdev_prop_bit = { · line 77
Quoted 10 lines
hw/qdev-properties.c +4
-3-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 -EINVALRegards,
Daniel
Signature · 4 lines
|: 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 :|
Am 26.05.2010 12:45, schrieb Daniel P. Berrange:
Quoted 33 lines · Kevin Wolf
Quoted 18 lines
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 <[email protected]>--- hw/qdev-properties.c | 50 +++++++++++++++++++++++++++++++++++--------------- 1 files changed, 35 insertions(+), 15 deletions(-)hw/qdev-properties.c +0
-0@@ -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);hw/qdev-properties.c PropertyInfo qdev_prop_bit = { · line 77
Quoted 10 lines
hw/qdev-properties.c +4
-3-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:
Quoted 2 lines
if ((end == str) || (*end != '\0'))
return -EINVALIt probably doesn't really make a difference here, but you're right. I'll send another version with this change.
Kevin
Quoted 4 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).
Do we have more misuse of PRI with scanf elsewhere? No need to fix them all in one commit (and thus delay this fix); I just want to make sure somebody looks.
Am 26.05.2010 13:13, schrieb Markus Armbruster:
Quoted 10 lines
Quoted 4 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).
Do we have more misuse of PRI with scanf elsewhere? No need to fix them all in one commit (and thus delay this fix); I just want to make sure somebody looks.
I saw another one in some Xen file that Gerd should fix some time. I'm not aware of any other.
Kevin