From 4935921cf780cd6bc25df54cf16f0bdd9bd6d443 Mon Sep 17 00:00:00 2001 From: Davide Guerri Date: Wed, 29 Aug 2012 14:49:53 +0200 Subject: [PATCH] use the stack to allocate number_string and free path_copy on error --- src/libvirt.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) 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++; } -- 2.11.0