From: Davide Guerri Date: Wed, 29 Aug 2012 12:49:53 +0000 (+0200) Subject: use the stack to allocate number_string and free path_copy on error X-Git-Tag: collectd-5.2.0~15^2~1 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=4935921cf780cd6bc25df54cf16f0bdd9bd6d443;p=collectd.git use the stack to allocate number_string and free path_copy on error --- diff --git a/src/libvirt.c b/src/libvirt.c index 755d6242..2aa0a7ad 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -772,17 +772,18 @@ add_interface_device (virDomainPtr dom, const char *path, const char *address, u { struct interface_device *new_ptr; int new_size = sizeof (interface_devices[0]) * (nr_interface_devices+1); - char *path_copy, *address_copy, *number_string; + char *path_copy, *address_copy, number_string[15]; path_copy = strdup (path); if (!path_copy) return -1; address_copy = strdup (address); - if (!address_copy) return -1; - - number_string = (char*) malloc (15); - if (!number_string) return -1; - snprintf(number_string, 15, "interface-%u", number); + if (!address_copy) { + sfree(path_copy) + return -1; + } + + snprintf(number_string, sizeof (number_string), "interface-%u", number); if (interface_devices) new_ptr = realloc (interface_devices, new_size); @@ -798,7 +799,7 @@ add_interface_device (virDomainPtr dom, const char *path, const char *address, u interface_devices[nr_interface_devices].dom = dom; interface_devices[nr_interface_devices].path = path_copy; interface_devices[nr_interface_devices].address = address_copy; - interface_devices[nr_interface_devices].number = number_string; + interface_devices[nr_interface_devices].number = strdup(number_string); return nr_interface_devices++; }