From: "Daniel P. Berrange" <berrange@redhat.com>
To: Kevin Wolf <kwolf@redhat.com>
Cc: armbru@redhat.com, qemu-devel@nongnu.org, kraxel@redhat.com
Subject: Re: [Qemu-devel] [PATCH] qdev-properties: Fix (u)intXX parsers
Date: Wed, 26 May 2010 11:45:58 +0100 [thread overview]
Message-ID: <20100526104558.GL18547@redhat.com> (raw)
In-Reply-To: <1274869693-22884-1-git-send-email-kwolf@redhat.com>
On Wed, May 26, 2010 at 12:28:13PM +0200, 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>
> ---
> 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;
> + }
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 -EINVAL
Regards,
Daniel
--
|: 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 :|
next prev parent reply other threads:[~2010-05-26 10:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-05-26 10:28 [Qemu-devel] [PATCH] qdev-properties: Fix (u)intXX parsers Kevin Wolf
2010-05-26 10:45 ` Daniel P. Berrange [this message]
2010-05-26 10:54 ` Kevin Wolf
2010-05-26 11:13 ` Markus Armbruster
2010-05-26 11:19 ` Kevin Wolf
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=20100526104558.GL18547@redhat.com \
--to=berrange@redhat.com \
--cc=armbru@redhat.com \
--cc=kraxel@redhat.com \
--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).