From: "Daniel P. Berrange" <berrange@redhat.com>
To: Pavel Fedin <p.fedin@samsung.com>
Cc: "'Paolo Bonzini'" <pbonzini@redhat.com>,
"'QEMU Developers'" <qemu-devel@nongnu.org>,
"'Andreas Färber'" <afaerber@suse.de>,
"'Markus Armbruster'" <armbru@redhat.com>
Subject: Re: [Qemu-devel] [PATCH] Do not use slow [*] expansion for GPIO creation
Date: Wed, 29 Jul 2015 15:13:52 +0100 [thread overview]
Message-ID: <20150729141352.GF16847@redhat.com> (raw)
In-Reply-To: <01b801d0ca08$05e335b0$11a9a110$@samsung.com>
On Wed, Jul 29, 2015 at 05:08:18PM +0300, Pavel Fedin wrote:
> Hello!
>
> > > + l = strlen(name);
> > > + propname = g_malloc(l + 13); /* 10 characters for UINT_MAX plus "[]" */
> > > + memcpy(propname, name, l);
> >
> > Please don't do manual string length calculations in combination with
> > unbounded sprintf calls. It is a recipe for future security bugs.
>
> [skip]
>
> > > for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
> > > + g_sprintf(&propname[l], "[%u]", i);
> >
> > Replace this with
> >
> > gchar *propname = g_strdup_printf("%s[%u]", name, i)
> >
> > > object_property_add_child(OBJECT(dev), propname,
> > > OBJECT(gpio_list->in[i]), &error_abort);
> >
> > g_free(propname);
>
> IMHO it's not really good because of repeating allocation-free. This
> is not VERY slow, but still slower than it could be (imagine that this
> repeats ~1000 times).
Unless this repeated allocation overhead is illustrated to be a real world
problem, I think using g_strdup_printf is preferrable.
> I have a better idea instead. What if instead:
>
> propname = g_malloc(l + 13); /* 10 characters for UINT_MAX plus "[]" */
>
> i do:
>
> propname = g_strdup_printf("%s[%u]", name, -1)
>
> ? This will automatically give me a buffer to fit in the largest possible integer.
I think it is premature/unneccesary optimization unless there are bench
marks to show this is a real world problem.
Regards,
Daniel
--
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org -o- http://virt-manager.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
prev parent reply other threads:[~2015-07-29 14:14 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-29 12:55 [Qemu-devel] [PATCH] Do not use slow [*] expansion for GPIO creation Pavel Fedin
2015-07-29 13:34 ` Daniel P. Berrange
2015-07-29 14:08 ` Pavel Fedin
2015-07-29 14:13 ` Daniel P. Berrange [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=20150729141352.GF16847@redhat.com \
--to=berrange@redhat.com \
--cc=afaerber@suse.de \
--cc=armbru@redhat.com \
--cc=p.fedin@samsung.com \
--cc=pbonzini@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.