From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44863) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZKS7N-00085s-7g for qemu-devel@nongnu.org; Wed, 29 Jul 2015 10:14:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZKS7J-0007Fz-DD for qemu-devel@nongnu.org; Wed, 29 Jul 2015 10:14:01 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40621) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZKS7J-0007Fk-8n for qemu-devel@nongnu.org; Wed, 29 Jul 2015 10:13:57 -0400 Date: Wed, 29 Jul 2015 15:13:52 +0100 From: "Daniel P. Berrange" Message-ID: <20150729141352.GF16847@redhat.com> References: <019001d0c9fd$d3268410$79738c30$@samsung.com> <20150729133459.GE16847@redhat.com> <01b801d0ca08$05e335b0$11a9a110$@samsung.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <01b801d0ca08$05e335b0$11a9a110$@samsung.com> Subject: Re: [Qemu-devel] [PATCH] Do not use slow [*] expansion for GPIO creation Reply-To: "Daniel P. Berrange" List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Pavel Fedin Cc: 'Paolo Bonzini' , 'QEMU Developers' , 'Andreas =?utf-8?Q?F=C3=A4rber'?= , 'Markus Armbruster' 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 :|