File pacemaker#3394-0002-Low-libpacemaker-Correctly-free-graphs-and-synapses.patch of Package pacemaker.34783

From e725721056920d2480bc488688af51768969ba22 Mon Sep 17 00:00:00 2001
From: Reid Wahl <nrwahl@protonmail.com>
Date: Wed, 20 Mar 2024 17:40:18 -0700
Subject: [PATCH 2/2] Low: libpacemaker: Correctly free graphs and synapses

Memory leak found by Cppcheck.

Signed-off-by: Reid Wahl <nrwahl@protonmail.com>
---
 lib/pacemaker/pcmk_trans_unpack.c | 136 ++++++++++++++--------------
 1 file changed, 68 insertions(+), 68 deletions(-)

Index: pacemaker-2.0.4+20200616.2deceaa3a/lib/pacemaker/pcmk_trans_unpack.c
===================================================================
--- pacemaker-2.0.4+20200616.2deceaa3a.orig/lib/pacemaker/pcmk_trans_unpack.c
+++ pacemaker-2.0.4+20200616.2deceaa3a/lib/pacemaker/pcmk_trans_unpack.c
@@ -17,6 +17,58 @@
 #include <crm/common/xml.h>
 #include <pacemaker-internal.h>
 
+
+static void
+destroy_action(crm_action_t * action)
+{
+    if (action->timer && action->timer->source_id != 0) {
+        crm_warn("Cancelling timer for action %d (src=%d)", action->id, action->timer->source_id);
+        g_source_remove(action->timer->source_id);
+    }
+    if (action->params) {
+        g_hash_table_destroy(action->params);
+    }
+    free_xml(action->xml);
+    free(action->timer);
+    free(action);
+}
+
+static void
+destroy_synapse(synapse_t * synapse)
+{
+    while (synapse->actions != NULL) {
+        crm_action_t *action = g_list_nth_data(synapse->actions, 0);
+
+        synapse->actions = g_list_remove(synapse->actions, action);
+        destroy_action(action);
+    }
+
+    while (synapse->inputs != NULL) {
+        crm_action_t *action = g_list_nth_data(synapse->inputs, 0);
+
+        synapse->inputs = g_list_remove(synapse->inputs, action);
+        destroy_action(action);
+    }
+    free(synapse);
+}
+
+void
+destroy_graph(crm_graph_t * graph)
+{
+    if (graph == NULL) {
+        return;
+    }
+    while (graph->synapses != NULL) {
+        synapse_t *synapse = g_list_nth_data(graph->synapses, 0);
+
+        graph->synapses = g_list_remove(graph->synapses, synapse);
+        destroy_synapse(synapse);
+    }
+
+    free(graph->source);
+    free(graph);
+}
+
 static crm_action_t *
 unpack_action(synapse_t * parent, xmlNode * xml_action)
 {
@@ -99,8 +151,8 @@ unpack_synapse(crm_graph_t * new_graph,
         new_synapse->priority = crm_parse_int(value, NULL);
     }
 
-    CRM_CHECK(new_synapse->id >= 0, free(new_synapse);
-              return NULL);
+    CRM_CHECK(new_synapse->id >= 0,
+              destroy_synapse(new_synapse); return NULL);
 
     new_graph->num_synapses++;
 
@@ -192,13 +244,13 @@ unpack_graph(xmlNode * xml_graph, const
 
     if (xml_graph != NULL) {
         t_id = crm_element_value(xml_graph, "transition_id");
-        CRM_CHECK(t_id != NULL, free(new_graph);
-                  return NULL);
+        CRM_CHECK(t_id != NULL,
+                  destroy_graph(new_graph); return NULL);
         new_graph->id = crm_parse_int(t_id, "-1");
 
         time = crm_element_value(xml_graph, "cluster-delay");
-        CRM_CHECK(time != NULL, free(new_graph);
-                  return NULL);
+        CRM_CHECK(time != NULL,
+                  destroy_graph(new_graph); return NULL);
         new_graph->network_delay = crm_parse_interval_spec(time);
 
         time = crm_element_value(xml_graph, "stonith-timeout");
@@ -231,57 +283,6 @@ unpack_graph(xmlNode * xml_graph, const
     return new_graph;
 }
 
-static void
-destroy_action(crm_action_t * action)
-{
-    if (action->timer && action->timer->source_id != 0) {
-        crm_warn("Cancelling timer for action %d (src=%d)", action->id, action->timer->source_id);
-        g_source_remove(action->timer->source_id);
-    }
-    if (action->params) {
-        g_hash_table_destroy(action->params);
-    }
-    free_xml(action->xml);
-    free(action->timer);
-    free(action);
-}
-
-static void
-destroy_synapse(synapse_t * synapse)
-{
-    while (synapse->actions != NULL) {
-        crm_action_t *action = g_list_nth_data(synapse->actions, 0);
-
-        synapse->actions = g_list_remove(synapse->actions, action);
-        destroy_action(action);
-    }
-
-    while (synapse->inputs != NULL) {
-        crm_action_t *action = g_list_nth_data(synapse->inputs, 0);
-
-        synapse->inputs = g_list_remove(synapse->inputs, action);
-        destroy_action(action);
-    }
-    free(synapse);
-}
-
-void
-destroy_graph(crm_graph_t * graph)
-{
-    if (graph == NULL) {
-        return;
-    }
-    while (graph->synapses != NULL) {
-        synapse_t *synapse = g_list_nth_data(graph->synapses, 0);
-
-        graph->synapses = g_list_remove(graph->synapses, synapse);
-        destroy_synapse(synapse);
-    }
-
-    free(graph->source);
-    free(graph);
-}
-
 lrmd_event_data_t *
 convert_graph_action(xmlNode * resource, crm_action_t * action, int status, int rc)
 {
openSUSE Build Service is sponsored by