01619e
From bee54eba4d9c28d3a7907a3e13a5deeee6bc0916 Mon Sep 17 00:00:00 2001
01619e
From: Ken Gaillot <kgaillot@redhat.com>
01619e
Date: Tue, 27 Jul 2021 11:01:04 -0500
01619e
Subject: [PATCH 1/2] Low: tools: avoid (insignificant) memory leaks
01619e
01619e
detected by valgrind
01619e
---
01619e
 lib/pacemaker/pcmk_cluster_queries.c |  2 ++
01619e
 tools/crm_diff.c                     |  2 +-
01619e
 tools/crm_resource.c                 | 33 ++++++++++++++++++++-------------
01619e
 tools/crm_resource_ban.c             |  2 +-
01619e
 4 files changed, 24 insertions(+), 15 deletions(-)
01619e
01619e
diff --git a/lib/pacemaker/pcmk_cluster_queries.c b/lib/pacemaker/pcmk_cluster_queries.c
01619e
index c68cf9d..46e5538 100644
01619e
--- a/lib/pacemaker/pcmk_cluster_queries.c
01619e
+++ b/lib/pacemaker/pcmk_cluster_queries.c
01619e
@@ -440,6 +440,7 @@ pcmk__list_nodes(pcmk__output_t *out, char *node_types, gboolean BASH_EXPORT)
01619e
     }
01619e
     rc = the_cib->cmds->signon(the_cib, crm_system_name, cib_command);
01619e
     if (rc != pcmk_ok) {
01619e
+        cib_delete(the_cib);
01619e
         return pcmk_legacy2rc(rc);
01619e
     }
01619e
 
01619e
@@ -488,6 +489,7 @@ pcmk__list_nodes(pcmk__output_t *out, char *node_types, gboolean BASH_EXPORT)
01619e
         free_xml(xml_node);
01619e
     }
01619e
     the_cib->cmds->signoff(the_cib);
01619e
+    cib_delete(the_cib);
01619e
     return pcmk_legacy2rc(rc);
01619e
 }
01619e
 
01619e
diff --git a/tools/crm_diff.c b/tools/crm_diff.c
01619e
index b37f0ea..9890c10 100644
01619e
--- a/tools/crm_diff.c
01619e
+++ b/tools/crm_diff.c
01619e
@@ -383,5 +383,5 @@ done:
01619e
     free_xml(object_2);
01619e
 
01619e
     pcmk__output_and_clear_error(error, NULL);
01619e
-    return exit_code;
01619e
+    crm_exit(exit_code);
01619e
 }
01619e
diff --git a/tools/crm_resource.c b/tools/crm_resource.c
01619e
index d8e140f..8ca90cb 100644
01619e
--- a/tools/crm_resource.c
01619e
+++ b/tools/crm_resource.c
01619e
@@ -1081,6 +1081,8 @@ clear_constraints(pcmk__output_t *out, xmlNodePtr *cib_xml_copy)
01619e
             g_set_error(&error, PCMK__RC_ERROR, rc,
01619e
                         "Could not get modified CIB: %s\n", pcmk_strerror(rc));
01619e
             g_list_free(before);
01619e
+            free_xml(*cib_xml_copy);
01619e
+            *cib_xml_copy = NULL;
01619e
             return rc;
01619e
         }
01619e
 
01619e
@@ -1232,29 +1234,34 @@ populate_working_set(xmlNodePtr *cib_xml_copy)
01619e
 
01619e
     if (options.xml_file != NULL) {
01619e
         *cib_xml_copy = filename2xml(options.xml_file);
01619e
+        if (*cib_xml_copy == NULL) {
01619e
+            rc = pcmk_rc_cib_corrupt;
01619e
+        }
01619e
     } else {
01619e
         rc = cib_conn->cmds->query(cib_conn, NULL, cib_xml_copy, cib_scope_local | cib_sync_call);
01619e
         rc = pcmk_legacy2rc(rc);
01619e
     }
01619e
 
01619e
-    if(rc != pcmk_rc_ok) {
01619e
-        return rc;
01619e
+    if (rc == pcmk_rc_ok) {
01619e
+        data_set = pe_new_working_set();
01619e
+        if (data_set == NULL) {
01619e
+            rc = ENOMEM;
01619e
+        } else {
01619e
+            pe__set_working_set_flags(data_set,
01619e
+                                      pe_flag_no_counts|pe_flag_no_compat);
01619e
+            data_set->priv = out;
01619e
+            rc = update_working_set_xml(data_set, cib_xml_copy);
01619e
+        }
01619e
     }
01619e
 
01619e
-    /* Populate the working set instance */
01619e
-    data_set = pe_new_working_set();
01619e
-    if (data_set == NULL) {
01619e
-        rc = ENOMEM;
01619e
+    if (rc != pcmk_rc_ok) {
01619e
+        free_xml(*cib_xml_copy);
01619e
+        *cib_xml_copy = NULL;
01619e
         return rc;
01619e
     }
01619e
 
01619e
-    pe__set_working_set_flags(data_set, pe_flag_no_counts|pe_flag_no_compat);
01619e
-    data_set->priv = out;
01619e
-    rc = update_working_set_xml(data_set, cib_xml_copy);
01619e
-    if (rc == pcmk_rc_ok) {
01619e
-        cluster_status(data_set);
01619e
-    }
01619e
-    return rc;
01619e
+    cluster_status(data_set);
01619e
+    return pcmk_rc_ok;
01619e
 }
01619e
 
01619e
 static int
01619e
diff --git a/tools/crm_resource_ban.c b/tools/crm_resource_ban.c
01619e
index a297d49..2c4f48d 100644
01619e
--- a/tools/crm_resource_ban.c
01619e
+++ b/tools/crm_resource_ban.c
01619e
@@ -292,7 +292,7 @@ resource_clear_node_in_location(const char *rsc_id, const char *host, cib_t * ci
01619e
         rc = pcmk_legacy2rc(rc);
01619e
     }
01619e
 
01619e
-    free(fragment);
01619e
+    free_xml(fragment);
01619e
     return rc;
01619e
 }
01619e
 
01619e
-- 
01619e
1.8.3.1
01619e
01619e
01619e
From a30ff4a87f291a0c9e03c4efb9c9046d2ac594f1 Mon Sep 17 00:00:00 2001
01619e
From: Ken Gaillot <kgaillot@redhat.com>
01619e
Date: Tue, 27 Jul 2021 11:26:59 -0500
01619e
Subject: [PATCH 2/2] Fix: tools: avoid memory leaks in crm_mon
01619e
01619e
could be significant in an interactive session
01619e
01619e
regressions introduced in 2.0.4 and 2.0.5
01619e
---
01619e
 lib/pengine/bundle.c    | 3 ++-
01619e
 lib/pengine/clone.c     | 5 ++---
01619e
 lib/pengine/pe_output.c | 3 +++
01619e
 3 files changed, 7 insertions(+), 4 deletions(-)
01619e
01619e
diff --git a/lib/pengine/bundle.c b/lib/pengine/bundle.c
01619e
index 6ba786a..7e1d428 100644
01619e
--- a/lib/pengine/bundle.c
01619e
+++ b/lib/pengine/bundle.c
01619e
@@ -1497,7 +1497,7 @@ pe__bundle_xml(pcmk__output_t *out, va_list args)
01619e
     for (GList *gIter = bundle_data->replicas; gIter != NULL;
01619e
          gIter = gIter->next) {
01619e
         pe__bundle_replica_t *replica = gIter->data;
01619e
-        char *id = pcmk__itoa(replica->offset);
01619e
+        char *id = NULL;
01619e
         gboolean print_ip, print_child, print_ctnr, print_remote;
01619e
 
01619e
         CRM_ASSERT(replica);
01619e
@@ -1531,6 +1531,7 @@ pe__bundle_xml(pcmk__output_t *out, va_list args)
01619e
             CRM_ASSERT(rc == pcmk_rc_ok);
01619e
         }
01619e
 
01619e
+        id = pcmk__itoa(replica->offset);
01619e
         rc = pe__name_and_nvpairs_xml(out, true, "replica", 1, "id", id);
01619e
         free(id);
01619e
         CRM_ASSERT(rc == pcmk_rc_ok);
01619e
diff --git a/lib/pengine/clone.c b/lib/pengine/clone.c
01619e
index 6323692..ab91fd1 100644
01619e
--- a/lib/pengine/clone.c
01619e
+++ b/lib/pengine/clone.c
01619e
@@ -807,10 +807,10 @@ pe__clone_html(pcmk__output_t *out, va_list args)
01619e
         pcmk__add_word(&list_text, &list_text_len, host->details->uname);
01619e
         active_instances++;
01619e
     }
01619e
+    g_list_free(promoted_list);
01619e
 
01619e
     if (list_text != NULL) {
01619e
         out->list_item(out, NULL, PROMOTED_INSTANCES ": [ %s ]", list_text);
01619e
-        g_list_free(promoted_list);
01619e
         free(list_text);
01619e
         list_text = NULL;
01619e
         list_text_len = 0;
01619e
@@ -828,6 +828,7 @@ pe__clone_html(pcmk__output_t *out, va_list args)
01619e
         pcmk__add_word(&list_text, &list_text_len, host->details->uname);
01619e
         active_instances++;
01619e
     }
01619e
+    g_list_free(started_list);
01619e
 
01619e
     if (list_text != NULL) {
01619e
         if (pcmk_is_set(rsc->flags, pe_rsc_promotable)) {
01619e
@@ -847,7 +848,6 @@ pe__clone_html(pcmk__output_t *out, va_list args)
01619e
             out->list_item(out, NULL, "Started: [ %s ]", list_text);
01619e
         }
01619e
 
01619e
-        g_list_free(started_list);
01619e
         free(list_text);
01619e
         list_text = NULL;
01619e
         list_text_len = 0;
01619e
@@ -1048,10 +1048,10 @@ pe__clone_text(pcmk__output_t *out, va_list args)
01619e
         pcmk__add_word(&list_text, &list_text_len, host->details->uname);
01619e
         active_instances++;
01619e
     }
01619e
+    g_list_free(promoted_list);
01619e
 
01619e
     if (list_text != NULL) {
01619e
         out->list_item(out, PROMOTED_INSTANCES, "[ %s ]", list_text);
01619e
-        g_list_free(promoted_list);
01619e
         free(list_text);
01619e
         list_text = NULL;
01619e
         list_text_len = 0;
01619e
@@ -1069,6 +1069,7 @@ pe__clone_text(pcmk__output_t *out, va_list args)
01619e
         pcmk__add_word(&list_text, &list_text_len, host->details->uname);
01619e
         active_instances++;
01619e
     }
01619e
+    g_list_free(started_list);
01619e
 
01619e
     if (list_text != NULL) {
01619e
         if (pcmk_is_set(rsc->flags, pe_rsc_promotable)) {
01619e
@@ -1084,7 +1085,6 @@ pe__clone_text(pcmk__output_t *out, va_list args)
01619e
             out->list_item(out, "Started", "[ %s ]", list_text);
01619e
         }
01619e
 
01619e
-        g_list_free(started_list);
01619e
         free(list_text);
01619e
         list_text = NULL;
01619e
     }
01619e
diff --git a/lib/pengine/pe_output.c b/lib/pengine/pe_output.c
01619e
index b8997c4..20bd1a9 100644
01619e
--- a/lib/pengine/pe_output.c
01619e
+++ b/lib/pengine/pe_output.c
01619e
@@ -1410,6 +1410,8 @@ node_text(pcmk__output_t *out, va_list args) {
01619e
 
01619e
                     out->end_list(out);
01619e
                     out->end_list(out);
01619e
+
01619e
+                    g_list_free(rscs);
01619e
                 }
01619e
 
01619e
             } else {
01619e
@@ -1739,6 +1741,7 @@ node_attribute_list(pcmk__output_t *out, va_list args) {
01619e
         }
01619e
 
01619e
         if (!pcmk__str_in_list(only_node, node->details->uname)) {
01619e
+            g_list_free(attr_list);
01619e
             continue;
01619e
         }
01619e
 
01619e
-- 
01619e
1.8.3.1
01619e