diff mbox series

[3/4] Fix memory leak in file Manage

Message ID 20230323013053.3238005-4-miaoguanqin@huawei.com (mailing list archive)
State Changes Requested, archived
Delegated to: Mariusz Tkaczyk
Headers show
Series Fix memory leak for Manage Assemble Kill mdadm | expand

Commit Message

Guanqin Miao March 23, 2023, 1:30 a.m. UTC
When we test mdadm with asan,we found some memory leaks in Manage.c
We fix these memory leaks based on code logic.

Signed-off-by: miaoguanqin <miaoguanqin@huawei.com>
Signed-off-by: lixiaokeng <lixiaokeng@huawei.com>
---
 Manage.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

Comments

Mariusz Tkaczyk March 28, 2023, 10:28 a.m. UTC | #1
On Thu, 23 Mar 2023 09:30:52 +0800
miaoguanqin <miaoguanqin@huawei.com> wrote:

> When we test mdadm with asan,we found some memory leaks in Manage.c
> We fix these memory leaks based on code logic.

Missing space after comma.
> 
> Signed-off-by: miaoguanqin <miaoguanqin@huawei.com>
> Signed-off-by: lixiaokeng <lixiaokeng@huawei.com>
> ---
>  Manage.c | 16 +++++++++++++++-
>  1 file changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/Manage.c b/Manage.c
> index fde6aba3..75bed83b 100644
> --- a/Manage.c
> +++ b/Manage.c
> @@ -222,6 +222,8 @@ int Manage_stop(char *devname, int fd, int verbose, int
> will_retry) if (verbose >= 0)
>  			pr_err("Cannot get exclusive access to %s:Perhaps a
> running process, mounted filesystem or active volume group?\n", devname);
> +		if (mdi)
> +			sysfs_free(mdi);
>  		return 1;
>  	}
>  	/* If this is an mdmon managed array, just write 'inactive'
> @@ -818,8 +820,16 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
>  						    rdev, update, devname,
>  						    verbose, array);
>  				dev_st->ss->free_super(dev_st);
> -				if (rv)
> +				if (rv) {
> +					if (dev_st)
> +						free(dev_st);

We are calling dev_st->ss->free_super(dev_st) in this code branch so it seems
that dev_st cannot be null, just free is needed.
diff mbox series

Patch

diff --git a/Manage.c b/Manage.c
index fde6aba3..75bed83b 100644
--- a/Manage.c
+++ b/Manage.c
@@ -222,6 +222,8 @@  int Manage_stop(char *devname, int fd, int verbose, int will_retry)
 		if (verbose >= 0)
 			pr_err("Cannot get exclusive access to %s:Perhaps a running process, mounted filesystem or active volume group?\n",
 			       devname);
+		if (mdi)
+			sysfs_free(mdi);
 		return 1;
 	}
 	/* If this is an mdmon managed array, just write 'inactive'
@@ -818,8 +820,16 @@  int Manage_add(int fd, int tfd, struct mddev_dev *dv,
 						    rdev, update, devname,
 						    verbose, array);
 				dev_st->ss->free_super(dev_st);
-				if (rv)
+				if (rv) {
+					if (dev_st)
+						free(dev_st);
 					return rv;
+				}
+			}
+			if (dev_st) {
+				if (dev_st->sb)
+					dev_st->ss->free_super(dev_st);
+				free(dev_st);
 			}
 		}
 		if (dv->disposition == 'M') {
@@ -1716,6 +1726,8 @@  int Manage_subdevs(char *devname, int fd,
 			break;
 		}
 	}
+	if (tst)
+		free(tst);
 	if (frozen > 0)
 		sysfs_set_str(&info, NULL, "sync_action","idle");
 	if (test && count == 0)
@@ -1723,6 +1735,8 @@  int Manage_subdevs(char *devname, int fd,
 	return 0;
 
 abort:
+	if (tst)
+		free(tst);
 	if (frozen > 0)
 		sysfs_set_str(&info, NULL, "sync_action","idle");
 	return !test && busy ? 2 : 1;