From bee54eba4d9c28d3a7907a3e13a5deeee6bc0916 Mon Sep 17 00:00:00 2001
From: Ken Gaillot <kgaillot@redhat.com>
Date: Tue, 27 Jul 2021 11:01:04 -0500
Subject: [PATCH 7/8] Low: tools: avoid (insignificant) memory leaks

detected by valgrind
---
 tools/crmadmin.c |  2 ++
 tools/crm_diff.c                     |  2 +-
 tools/crm_resource.c                 | 33 +++++++++++++++++-----------
 tools/crm_resource_ban.c             |  2 +-
 4 files changed, 24 insertions(+), 15 deletions(-)

Index: pacemaker-2.0.4+20200616.2deceaa3a/tools/crmadmin.c
===================================================================
--- pacemaker-2.0.4+20200616.2deceaa3a.orig/tools/crmadmin.c
+++ pacemaker-2.0.4+20200616.2deceaa3a/tools/crmadmin.c
@@ -309,6 +309,7 @@ do_work(void)
         int rc = the_cib->cmds->signon(the_cib, crm_system_name, cib_command);
 
         if (rc != pcmk_ok) {
+            cib_delete(the_cib);
             fprintf(stderr, "Could not connect to CIB: %s\n",
                     pcmk_strerror(rc));
             return -1;
@@ -321,6 +322,7 @@ do_work(void)
             free_xml(output);
         }
         the_cib->cmds->signoff(the_cib);
+        cib_delete(the_cib);
         crm_exit(crm_errno2exit(rc));
 
     } else if (DO_RESET) {
Index: pacemaker-2.0.4+20200616.2deceaa3a/tools/crm_diff.c
===================================================================
--- pacemaker-2.0.4+20200616.2deceaa3a.orig/tools/crm_diff.c
+++ pacemaker-2.0.4+20200616.2deceaa3a/tools/crm_diff.c
@@ -397,5 +397,5 @@ done:
     free(options.xml_file_2);
     free_xml(object_1);
     free_xml(object_2);
-    return crm_errno2exit(rc);
+    crm_exit(crm_errno2exit(rc));
 }
Index: pacemaker-2.0.4+20200616.2deceaa3a/tools/crm_resource.c
===================================================================
--- pacemaker-2.0.4+20200616.2deceaa3a.orig/tools/crm_resource.c
+++ pacemaker-2.0.4+20200616.2deceaa3a/tools/crm_resource.c
@@ -1128,26 +1128,30 @@ main(int argc, char **argv)
     if (require_dataset) {
         if (xml_file != NULL) {
             cib_xml_copy = filename2xml(xml_file);
+            if (cib_xml_copy == NULL) {
+                rc = pcmk_rc_cib_corrupt;
+            }
         } else {
             rc = cib_conn->cmds->query(cib_conn, NULL, &cib_xml_copy, cib_scope_local | cib_sync_call);
         }
 
-        if(rc != pcmk_ok) {
-            goto bail;
+        if (rc == pcmk_rc_ok) {
+            data_set = pe_new_working_set();
+            if (data_set == NULL) {
+                rc = -ENOMEM;
+            } else {
+                set_bit(data_set->flags, pe_flag_no_counts);
+                set_bit(data_set->flags, pe_flag_no_compat);
+                rc = update_working_set_xml(data_set, &cib_xml_copy);
+            }
         }
 
-        /* Populate the working set instance */
-        data_set = pe_new_working_set();
-        if (data_set == NULL) {
-            rc = -ENOMEM;
-            goto bail;
-        }
-        set_bit(data_set->flags, pe_flag_no_counts);
-        set_bit(data_set->flags, pe_flag_no_compat);
-        rc = update_working_set_xml(data_set, &cib_xml_copy);
-        if (rc != pcmk_ok) {
+        if (rc != pcmk_rc_ok) {
+            free_xml(cib_xml_copy);
+            cib_xml_copy = NULL;
             goto bail;
         }
+
         cluster_status(data_set);
     }
 
@@ -1337,6 +1341,8 @@ main(int argc, char **argv)
             if (rc != pcmk_ok) {
                 CMD_ERR("Could not get modified CIB: %s\n", pcmk_strerror(rc));
                 g_list_free(before);
+                free_xml(cib_xml_copy);
+                cib_xml_copy = NULL;
                 goto bail;
             }
 
Index: pacemaker-2.0.4+20200616.2deceaa3a/tools/crm_resource_ban.c
===================================================================
--- pacemaker-2.0.4+20200616.2deceaa3a.orig/tools/crm_resource_ban.c
+++ pacemaker-2.0.4+20200616.2deceaa3a/tools/crm_resource_ban.c
@@ -264,7 +264,7 @@ resource_clear_node_in_location(const ch
         rc = pcmk_ok;
     }
 
-    free(fragment);
+    free_xml(fragment);
     return rc;
 }
 
