Skip to contentSkip to conversation

cov: fix memory leak caused by abnormal exit

PATCH New Wu Guanghao 4 participants 7 messages Aug 30, 2021 – Sep 10, 2021

lore
Share & tools
lore
#0 lore raw

cover cov: fix memory leak caused by abnormal exit

Several possible memory leaks were discovered through tool scans

wuguanghao (3):
cmdlib_lvm2_init: release cmd after registration failed
read_adopt_file: Set the default value of r to NULL,
and release r when exiting abnormally
read_adopt_file: fix memory leak

 daemons/lvmlockd/lvmlockd-core.c | 7 +++++--
 tools/lvmcmdlib.c                | 4 +++-
 2 files changed, 8 insertions(+), 3 deletions(-)

--
2.23.0

#1 lore raw

PATCH 1/3 cmdlib_lvm2_init: release cmd after registration failed

The cmd memory space is allocated by zalloc, and the registration fails and is not released

---
 tools/lvmcmdlib.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/lvmcmdlib.c +3 -1
index 152d176b2..6b326893c 100644
@@ -33,8 +33,10 @@ void *cmdlib_lvm2_init(unsigned static_compile, unsigned threaded)
        if (!(cmd = init_lvm(1, 1, threaded)))
                return NULL;
 
-       if (!lvm_register_commands(cmd, NULL))
+       if (!lvm_register_commands(cmd, NULL)) {
+               free(cmd);
                return NULL;
+       }
 
        return (void *) cmd;
 }

--
2.23.0

#2 lore raw

PATCH 1/3 cmdlib_lvm2_init: release cmd after registration failed

Dne 30. 08. 21 v 9:13 Wu Guanghao napsal(a):

Quoted 12 lines

The cmd memory space is allocated by zalloc, and the registration fails and is not released

---
  tools/lvmcmdlib.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
tools/lvmcmdlib.c +0 -0
index 152d176b2..6b326893c 100644

tools/lvmcmdlib.c void *cmdlib_lvm2_init(unsigned static_compile, unsigned threaded) · line 41

Quoted 10 lines
tools/lvmcmdlib.c +3 -1
@@ -33,8 +33,10 @@ void *cmdlib_lvm2_init(unsigned static_compile, unsigned threaded)
         if (!(cmd = init_lvm(1, 1, threaded)))
                 return NULL;
 
-       if (!lvm_register_commands(cmd, NULL))
+       if (!lvm_register_commands(cmd, NULL)) {
+               free(cmd);
                 return NULL;
+       }
 
         return (void *) cmd;

Well - all possible paths leading to 'return 0' from lvm_register_commands() are essentially internal errors - so app should crash anyway, but I guess it does no harm to add 'free()' call - so I'll push it.

Zdenek

#3 lore raw

PATCH 2/3 read_adopt_file: release r when exiting abnormally

In reply to Wu Guanghao

When exiting abnormally, r may not be released.At the same time, avoid freeing up other space, set the default value of r to NULL

---
 daemons/lvmlockd/lvmlockd-core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
daemons/lvmlockd/lvmlockd-core.c +2 -1
index fef9589b9..d4d7f106f 100644
@@ -938,7 +938,7 @@ static int read_adopt_file(struct list_head *vg_lockd)
        char lm_type_str[16];
        char mode[8];
        struct lockspace *ls = NULL, *ls2;
-       struct resource *r;
+       struct resource *r = NULL;
        FILE *fp;
 
        if (MAX_ARGS != 64 || MAX_NAME != 64)
@@ -1012,6 +1012,7 @@ static int read_adopt_file(struct list_head *vg_lockd)
                }
        }
 
+       free_resource(r);
        fclose(fp);
        return 0;
 

--
2.23.0

#4 lore raw

PATCH 3/3 read_adopt_file: fix memory leak

In reply to Wu Guanghao

When exiting abnormally, forget to release ls

---
 daemons/lvmlockd/lvmlockd-core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
daemons/lvmlockd/lvmlockd-core.c +3 -1
index d4d7f106f..71d708603 100644
@@ -970,8 +970,10 @@ static int read_adopt_file(struct list_head *vg_lockd)
 
                        memcpy(ls->vg_uuid, vg_uuid, 64);
 
-                       if ((ls->lm_type = str_to_lm(lm_type_str)) < 0)
+                       if ((ls->lm_type = str_to_lm(lm_type_str)) < 0) {
+                               free(ls);
                                goto fail;
+                       }
 
                        list_add(&ls->list, vg_lockd);
 

--
2.23.0

#5 lore raw

cover cov: fix memory leak caused by abnormal exit

In reply to Wu Guanghao

ping...

Quoted 16 lines · Wu Guanghao

Several possible memory leaks were discovered through tool scans

wuguanghao (3):
cmdlib_lvm2_init: release cmd after registration failed
read_adopt_file: Set the default value of r to NULL,
and release r when exiting abnormally
read_adopt_file: fix memory leak

 daemons/lvmlockd/lvmlockd-core.c | 7 +++++--
 tools/lvmcmdlib.c                | 4 +++-
 2 files changed, 8 insertions(+), 3 deletions(-)

--
2.23.0

.

#6 lore raw

cover cov: fix memory leak caused by abnormal exit

Quoted 10 lines · Zhiqiang Liu

ping...

Quoted 7 lines · Wu Guanghao

Several possible memory leaks were discovered through tool scans

wuguanghao (3):
cmdlib_lvm2_init: release cmd after registration failed
read_adopt_file: Set the default value of r to NULL,
and release r when exiting abnormally
read_adopt_file: fix memory leak

this commit addressed part of that, but the first didn't seem quite right.
https://sourceware.org/git/?p=lvm2.git;a=blobdiff;f=daemons/lvmlockd/lvmlockd-core.c;h=c570b9d14eb6ec0db4e0bffb371a3cd70c67d9b6;hp=fef9589b9d985e5767c19db2650235fbfd406f7f;hb=62c599f78151e65105c92c87ac406e3f3f360b1a;hpb=350f8845bd39dd7aaf5a52ad5deff6859fd06568