Linux Device Mapper development
 help / color / mirror / Atom feed
From: "Benjamin Marzinski" <bmarzins@redhat.com>
To: Martin Wilck <mwilck@suse.com>
Cc: dm-devel@redhat.com
Subject: Re: [PATCH 02/21] libmultipath: fix memory leaks from scandir() use
Date: Thu, 25 Oct 2018 15:31:44 -0500	[thread overview]
Message-ID: <20181025203144.GK7100@octiron.msp.redhat.com> (raw)
In-Reply-To: <20181011222707.3631-3-mwilck@suse.com>

On Fri, Oct 12, 2018 at 12:26:48AM +0200, Martin Wilck wrote:
> scandir() users must not only free the resulting dirent* array,
> but also every member. Add a cleanup function, and fix the
> existing users of scandir() in libmultipath.
> 
> Add a small helper macro for casting function pointers to the
> type pthread_cleanup_push() expects.
> 

Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
> Signed-off-by: Martin Wilck <mwilck@suse.com>
> ---
>  libmultipath/config.c       | 10 ++++------
>  libmultipath/foreign.c      |  5 ++++-
>  libmultipath/foreign/nvme.c |  6 +++++-
>  libmultipath/sysfs.c        |  5 ++++-
>  libmultipath/util.c         |  9 +++++++++
>  libmultipath/util.h         |  9 +++++++++
>  6 files changed, 35 insertions(+), 9 deletions(-)
> 
> diff --git a/libmultipath/config.c b/libmultipath/config.c
> index 0aef186a..5af7af58 100644
> --- a/libmultipath/config.c
> +++ b/libmultipath/config.c
> @@ -639,17 +639,13 @@ free_config (struct config * conf)
>  	FREE(conf);
>  }
>  
> -static void free_namelist(void *nl)
> -{
> -	free(nl);
> -}
> -
>  /* if multipath fails to process the config directory, it should continue,
>   * with just a warning message */
>  static void
>  process_config_dir(struct config *conf, vector keywords, char *dir)
>  {
>  	struct dirent **namelist;
> +	struct scandir_result sr;
>  	int i, n;
>  	char path[LINE_MAX];
>  	int old_hwtable_size;
> @@ -669,7 +665,9 @@ process_config_dir(struct config *conf, vector keywords, char *dir)
>  		return;
>  	} else if (n == 0)
>  		return;
> -	pthread_cleanup_push(free_namelist, namelist);
> +	sr.di = namelist;
> +	sr.n = n;
> +	pthread_cleanup_push_cast(free_scandir_result, &sr);
>  	for (i = 0; i < n; i++) {
>  		if (!strstr(namelist[i]->d_name, ".conf"))
>  			continue;
> diff --git a/libmultipath/foreign.c b/libmultipath/foreign.c
> index 80b399ba..48e8d247 100644
> --- a/libmultipath/foreign.c
> +++ b/libmultipath/foreign.c
> @@ -115,6 +115,7 @@ static int _init_foreign(const char *multipath_dir)
>  {
>  	char pathbuf[PATH_MAX];
>  	struct dirent **di;
> +	struct scandir_result sr;
>  	int r, i;
>  
>  	foreigns = vector_alloc();
> @@ -135,7 +136,9 @@ static int _init_foreign(const char *multipath_dir)
>  		return -r;
>  	}
>  
> -	pthread_cleanup_push(free, di);
> +	sr.di = di;
> +	sr.n = r;
> +	pthread_cleanup_push_cast(free_scandir_result, &sr);
>  	for (i = 0; i < r; i++) {
>  		const char *msg, *fn, *c;
>  		struct foreign *fgn;
> diff --git a/libmultipath/foreign/nvme.c b/libmultipath/foreign/nvme.c
> index 8887a755..c753a747 100644
> --- a/libmultipath/foreign/nvme.c
> +++ b/libmultipath/foreign/nvme.c
> @@ -27,6 +27,7 @@
>  #include <dirent.h>
>  #include <errno.h>
>  #include <ctype.h>
> +#include "util.h"
>  #include "vector.h"
>  #include "generic.h"
>  #include "foreign.h"
> @@ -534,6 +535,7 @@ static void _find_controllers(struct context *ctx, struct nvme_map *map)
>  {
>  	char pathbuf[PATH_MAX], realbuf[PATH_MAX];
>  	struct dirent **di = NULL;
> +	struct scandir_result sr;
>  	struct udev_device *subsys;
>  	struct nvme_path *path;
>  	int r, i, n;
> @@ -568,7 +570,9 @@ static void _find_controllers(struct context *ctx, struct nvme_map *map)
>  		return;
>  	}
>  
> -	pthread_cleanup_push(free, di);
> +	sr.di = di;
> +	sr.n = r;
> +	pthread_cleanup_push_cast(free_scandir_result, &sr);
>  	for (i = 0; i < r; i++) {
>  		char *fn = di[i]->d_name;
>  		struct udev_device *ctrl, *udev;
> diff --git a/libmultipath/sysfs.c b/libmultipath/sysfs.c
> index b7dacaad..558c8d6a 100644
> --- a/libmultipath/sysfs.c
> +++ b/libmultipath/sysfs.c
> @@ -303,6 +303,7 @@ static void close_fd(void *arg)
>  bool sysfs_is_multipathed(const struct path *pp)
>  {
>  	char pathbuf[PATH_MAX];
> +	struct scandir_result sr;
>  	struct dirent **di;
>  	int n, r, i;
>  	bool found = false;
> @@ -323,7 +324,9 @@ bool sysfs_is_multipathed(const struct path *pp)
>  		return false;
>  	}
>  
> -	pthread_cleanup_push(free, di);
> +	sr.di = di;
> +	sr.n = r;
> +	pthread_cleanup_push_cast(free_scandir_result, &sr);
>  	for (i = 0; i < r && !found; i++) {
>  		long fd;
>  		int nr;
> diff --git a/libmultipath/util.c b/libmultipath/util.c
> index d08112db..66c47611 100644
> --- a/libmultipath/util.c
> +++ b/libmultipath/util.c
> @@ -496,3 +496,12 @@ void set_max_fds(int max_fds)
>  		}
>  	}
>  }
> +
> +void free_scandir_result(struct scandir_result *res)
> +{
> +	int i;
> +
> +	for (i = 0; i < res->n; i++)
> +		FREE(res->di[i]);
> +	FREE(res->di);
> +}
> diff --git a/libmultipath/util.h b/libmultipath/util.h
> index c2462950..a818e29a 100644
> --- a/libmultipath/util.h
> +++ b/libmultipath/util.h
> @@ -30,4 +30,13 @@ void set_max_fds(int max_fds);
>  #define safe_snprintf(var, size, format, args...)      \
>  	snprintf(var, size, format, ##args) >= size
>  
> +#define pthread_cleanup_push_cast(f, arg)		\
> +	pthread_cleanup_push(((void (*)(void *))&f), (arg))
> +
> +struct scandir_result {
> +	struct dirent **di;
> +	int n;
> +};
> +void free_scandir_result(struct scandir_result *);
> +
>  #endif /* _UTIL_H */
> -- 
> 2.19.0

  reply	other threads:[~2018-10-25 20:31 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-11 22:26 [PATCH 00/21] libmultipath: checkers overhaul Martin Wilck
2018-10-11 22:26 ` [PATCH 01/21] libmultipath: fix use of uninitialized memory in write() Martin Wilck
2018-10-25 20:31   ` Benjamin Marzinski
2018-10-11 22:26 ` [PATCH 02/21] libmultipath: fix memory leaks from scandir() use Martin Wilck
2018-10-25 20:31   ` Benjamin Marzinski [this message]
2018-10-11 22:26 ` [PATCH 03/21] libmultipath/checkers: replace message by msgid Martin Wilck
2018-10-25 20:57   ` Benjamin Marzinski
2018-10-25 21:36     ` Martin Wilck
2018-10-11 22:26 ` [PATCH 04/21] libmultipath/checkers: cciss_tur: use message id Martin Wilck
2018-10-25 20:57   ` Benjamin Marzinski
2018-10-11 22:26 ` [PATCH 05/21] libmultipath/checkers: directio: " Martin Wilck
2018-10-25 20:58   ` Benjamin Marzinski
2018-10-11 22:26 ` [PATCH 06/21] libmultipath/checkers: emc_clariion: " Martin Wilck
2018-10-25 20:58   ` Benjamin Marzinski
2018-10-11 22:26 ` [PATCH 07/21] libmultipath/checkers: hp_sw: " Martin Wilck
2018-10-25 20:58   ` Benjamin Marzinski
2018-10-11 22:26 ` [PATCH 08/21] libmultipath/checkers: rdac: " Martin Wilck
2018-10-25 20:59   ` Benjamin Marzinski
2018-10-11 22:26 ` [PATCH 09/21] libmultipath/checkers: readsector0: " Martin Wilck
2018-10-25 21:02   ` Benjamin Marzinski
2018-10-11 22:26 ` [PATCH 10/21] libmultipath/checkers: tur: " Martin Wilck
2018-10-25 21:32   ` Benjamin Marzinski
2018-10-11 22:26 ` [PATCH 11/21] multipathd: improve checker message logging Martin Wilck
2018-10-25 21:32   ` Benjamin Marzinski
2018-10-11 22:26 ` [PATCH 12/21] libmultipath/checkers: support unsupported paths Martin Wilck
2018-10-25 21:33   ` Benjamin Marzinski
2018-10-11 22:26 ` [PATCH 13/21] libmultipath: clariion checker: leave unsupported paths alone Martin Wilck
2018-10-25 21:41   ` Benjamin Marzinski
2018-10-26  9:01     ` Martin Wilck
2018-10-11 22:27 ` [PATCH 14/21] libmultipath: hp_sw " Martin Wilck
2018-10-25 21:42   ` Benjamin Marzinski
2018-10-11 22:27 ` [PATCH 15/21] libmultipath: rdac " Martin Wilck
2018-10-25 21:42   ` Benjamin Marzinski
2018-10-11 22:27 ` [PATCH 16/21] libmultipath: tur " Martin Wilck
2018-10-25 21:42   ` Benjamin Marzinski
2018-10-11 22:27 ` [PATCH 17/21] libmultipath: pathinfo: don't blank wwid if checker fails Martin Wilck
2018-10-25 21:50   ` Benjamin Marzinski
2018-10-26  9:16     ` Martin Wilck
2018-10-26 15:11       ` Benjamin Marzinski
2018-10-11 22:27 ` [PATCH 18/21] multipathd: check_path: improve logging for "unusable path" case Martin Wilck
2018-10-25 21:50   ` Benjamin Marzinski
2018-10-11 22:27 ` [PATCH 19/21] libmultipath: coalesce_paths: improve logging of orphaned paths Martin Wilck
2018-10-25 21:50   ` Benjamin Marzinski
2018-10-11 22:27 ` [PATCH 20/21] libmultipath: sync_map_state: log failing paths Martin Wilck
2018-10-25 21:52   ` Benjamin Marzinski
2018-10-11 22:27 ` [PATCH 21/21] libmultipath/checkers: cleanup class/instance model Martin Wilck
2018-10-25 21:52   ` Benjamin Marzinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181025203144.GK7100@octiron.msp.redhat.com \
    --to=bmarzins@redhat.com \
    --cc=dm-devel@redhat.com \
    --cc=mwilck@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox