[libvirt] [PATCH 2/2] Fix memory leak in set_input_props

Adam Majer amajer at suse.de
Tue Jan 9 12:20:38 UTC 2018


When caption is specified, cap was allocated and assigned a new pointer
while the old never freed. Free cap before replacing it.

get_input_dev_caption only returns valid cap pointer on success, so
do not try to free it on failure.

Signed-off-by: Adam Majer <amajer at suse.de>
---
 src/Virt_SettingsDefineCapabilities.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/Virt_SettingsDefineCapabilities.c b/src/Virt_SettingsDefineCapabilities.c
index 85cb27a..0a1d61e 100644
--- a/src/Virt_SettingsDefineCapabilities.c
+++ b/src/Virt_SettingsDefineCapabilities.c
@@ -1978,9 +1978,9 @@ static CMPIStatus set_input_props(const CMPIObjectPath *ref,
         CMPIStatus s = {CMPI_RC_OK, NULL};
         CMPIInstance *inst;
         char *cap;
+        char *tmp;
 
         if (get_input_dev_caption(type, bus, &cap) != 1) {
-                free(cap);
                 cu_statusf(_BROKER, &s,
                            CMPI_RC_ERR_NOT_FOUND,
                            "Unable to build input caption");
@@ -1988,12 +1988,14 @@ static CMPIStatus set_input_props(const CMPIObjectPath *ref,
         }
 
         if (caption != NULL) {
-                if (asprintf(&cap, "%s %s", caption, cap) == -1) {
+                if (asprintf(&tmp, "%s %s", caption, cap) == -1) {
                         cu_statusf(_BROKER, &s,
                                    CMPI_RC_ERR_NOT_FOUND,
                                    "Unable to build input caption");
                         goto out;
                 }
+                free(cap);
+                cap = tmp;
         }
 
         inst = sdc_rasd_inst(&s, ref, CIM_RES_TYPE_INPUT, DEVICE_RASD);
-- 
2.13.6




More information about the libvir-list mailing list