From a910f59d534e19bd6fca5aa0e47b40afbbfc7f56 Mon Sep 17 00:00:00 2001
From: chenjinhao <chen.jinhao@zte.com.cn>
Date: Mon, 22 Jul 2024 11:10:28 +0800
Subject: [PATCH] glusterd: fix memory leaks due to lack of GF_FREE

In glusterd-store.c, there are while loops like:

gf_store_iter_get_next(iter, &key, &value, &op_errno);
while (!ret) {
    if (xx_condition) {
        do_something();
        goto out;
    }
    GF_FREE(key);
    GF_FREE(value);
    key = NULL;
    value = NULL;

    ret = gf_store_iter_get_next(iter, &key, &value, &op_errno);

}
It's ok under normal case, howerver, once the condition does not meet
and the procedure goto 'out', there will be memory leak.

Hence, it is necessary to put a check at 'out'.

Similar leaks will be triggered in glusterd_store_retrieve_peers.
If no peerinfo is found, the procedure will goto the next loop.
It means memory previously allocated for key & value will be
leaked once gf_store_iter_get_next is called again in the next loop.

Signed-off-by: chenjinhao <chen.jinhao@zte.com.cn>
---
 xlators/mgmt/glusterd/src/glusterd-store.c | 16 ++++++++++++++++
 xlators/storage/posix/src/posix-helpers.c  |  4 ++++
 2 files changed, 20 insertions(+)

diff --git a/xlators/mgmt/glusterd/src/glusterd-store.c b/xlators/mgmt/glusterd/src/glusterd-store.c
index 42d82d8..8594fc9 100644
--- a/xlators/mgmt/glusterd/src/glusterd-store.c
+++ b/xlators/mgmt/glusterd/src/glusterd-store.c
@@ -2364,6 +2364,10 @@ glusterd_store_retrieve_snapd(glusterd_volinfo_t *volinfo)
                      SLEN(GLUSTERD_STORE_KEY_SNAPD_PORT))) {
             volinfo->snapd.port = atoi(value);
         }
+        GF_FREE(key);
+        GF_FREE(value);
+        key = NULL;
+        value = NULL;
 
         ret = gf_store_iter_get_next(iter, &key, &value, &op_errno);
     }
@@ -2896,6 +2900,10 @@ glusterd_store_retrieve_bricks(glusterd_volinfo_t *volinfo)
     ret = 0;
 
 out:
+    if (key)
+        GF_FREE(key);
+    if (value)
+        GF_FREE(value);
     if (gf_store_iter_destroy(&tmpiter)) {
         gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_STORE_ITER_DESTROY_FAIL,
                "Failed to destroy store iter");
@@ -3041,6 +3049,10 @@ out:
 
     if (dup_value)
         GF_FREE(dup_value);
+    if (key)
+        GF_FREE(key);
+    if (value)
+        GF_FREE(value);
     if (ret) {
         if (volinfo->rebal.dict)
             dict_unref(volinfo->rebal.dict);
@@ -4633,6 +4645,10 @@ glusterd_store_retrieve_peers(xlator_t *this)
         peerinfo = glusterd_peerinfo_new(GD_FRIEND_STATE_DEFAULT, NULL, NULL,
                                          0);
         if (peerinfo == NULL) {
+            GF_FREE(key);
+            GF_FREE(value);
+            key = NULL;
+            value = NULL;
             ret = -1;
             goto next;
         }
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
index 08bb4ac..9fae102 100644
--- a/xlators/storage/posix/src/posix-helpers.c
+++ b/xlators/storage/posix/src/posix-helpers.c
@@ -3288,6 +3288,8 @@ posix_cs_set_state(xlator_t *this, dict_t **rsp, gf_cs_obj_state state,
             xattrsize = sys_fgetxattr(*fd, GF_CS_OBJECT_REMOTE, value,
                                       xattrsize + 1);
             if (xattrsize == -1) {
+                if (value)
+                    GF_FREE(value);
                 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
                        " getxattr failed for key %s", GF_CS_OBJECT_REMOTE);
                 goto out;
@@ -3311,6 +3313,8 @@ posix_cs_set_state(xlator_t *this, dict_t **rsp, gf_cs_obj_state state,
             xattrsize = sys_lgetxattr(path, GF_CS_OBJECT_REMOTE, value,
                                       xattrsize + 1);
             if (xattrsize == -1) {
+                if (value)
+                    GF_FREE(value);
                 gf_msg(this->name, GF_LOG_ERROR, 0, errno,
                        " getxattr failed for key %s", GF_CS_OBJECT_REMOTE);
                 goto out;
-- 
2.33.0

