qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Anthony Liguori <anthony@codemonkey.ws>
To: Kevin Wolf <kwolf@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH][RESEND] qdev-properties: Fix (u)intXX parsers
Date: Tue, 06 Jul 2010 10:45:29 -0500	[thread overview]
Message-ID: <4C334F99.6070508@codemonkey.ws> (raw)
In-Reply-To: <1276878423-19955-1-git-send-email-kwolf@redhat.com>

On 06/18/2010 11:27 AM, 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<kwolf@redhat.com>
>    

Applied.  Thanks.

Regards,

Anthony Liguori
> ---
>   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 5a8739d..5b7fd77 100644
> --- a/hw/qdev-properties.c
> +++ b/hw/qdev-properties.c
> @@ -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;
>   }
>
>    

      parent reply	other threads:[~2010-07-06 15:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-18 16:27 [Qemu-devel] [PATCH][RESEND] qdev-properties: Fix (u)intXX parsers Kevin Wolf
2010-06-21  0:40 ` Richard Henderson
2010-06-28 13:54 ` Markus Armbruster
2010-06-30 20:59 ` Aurelien Jarno
2010-07-06 15:45 ` Anthony Liguori [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4C334F99.6070508@codemonkey.ws \
    --to=anthony@codemonkey.ws \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).