Skip to contentSkip to conversation

libmultipath: fix memory leak in _check_bindings_file

PATCH In review [email protected] 2 participants 2 messages Sep 16, 2020 – Sep 21, 2020 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libmultipath: fix memory leak in _check_bindings_file

From: Martin Wilck <[email protected]>

We must pass "&line" to the cleanup function, not "line".

Fixes "libmultipath: add consistency check for alias settings"
---
 libmultipath/alias.c | 2 +-
 libmultipath/util.c  | 8 ++++++++
 libmultipath/util.h  | 1 +
 3 files changed, 10 insertions(+), 1 deletion(-)
libmultipath/alias.c +1 -1
index de28f25..a7ba485 100644
@@ -580,7 +580,7 @@ static int _check_bindings_file(const struct config *conf, FILE *file,
 	size_t line_len = 0;
 	ssize_t n;
 
-	pthread_cleanup_push(free, line);
+	pthread_cleanup_push(cleanup_free_ptr, &line);
 	while ((n = getline(&line, &line_len, file)) >= 0) {
 		char *c, *alias, *wwid, *saveptr;
 		const char *mpe_wwid;
libmultipath/util.c +8 -0
index 8412d30..395c710 100644
@@ -406,6 +406,14 @@ void cleanup_mutex(void *arg)
 	pthread_mutex_unlock(arg);
 }
 
+void cleanup_free_ptr(void *arg)
+{
+	void **p = arg;
+
+	if (p && *p)
+		free(*p);
+}
+
 struct bitfield *alloc_bitfield(unsigned int maxbit)
 {
 	unsigned int n;
libmultipath/util.h +1 -0
index 94ed872..21a4088 100644
@@ -49,6 +49,7 @@ int should_exit(void);
 
 void close_fd(void *arg);
 void cleanup_mutex(void *arg);
+void cleanup_free_ptr(void *arg);
 
 struct scandir_result {
 	struct dirent **di;
Signature · 1 line
2.28.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · On Wed, Sep 16, 2020 at 06:26:58PM +0200, [email protected] wrote:

From: Martin Wilck <[email protected]>

We must pass "&line" to the cleanup function, not "line".

Quoted 54 lines
Fixes "libmultipath: add consistency check for alias settings"
---
 libmultipath/alias.c | 2 +-
 libmultipath/util.c  | 8 ++++++++
 libmultipath/util.h  | 1 +
 3 files changed, 10 insertions(+), 1 deletion(-)
libmultipath/alias.c +1 -1
index de28f25..a7ba485 100644
@@ -580,7 +580,7 @@ static int _check_bindings_file(const struct config *conf, FILE *file,
 	size_t line_len = 0;
 	ssize_t n;
 
-	pthread_cleanup_push(free, line);
+	pthread_cleanup_push(cleanup_free_ptr, &line);
 	while ((n = getline(&line, &line_len, file)) >= 0) {
 		char *c, *alias, *wwid, *saveptr;
 		const char *mpe_wwid;
libmultipath/util.c +8 -0
index 8412d30..395c710 100644
@@ -406,6 +406,14 @@ void cleanup_mutex(void *arg)
 	pthread_mutex_unlock(arg);
 }
 
+void cleanup_free_ptr(void *arg)
+{
+	void **p = arg;
+
+	if (p && *p)
+		free(*p);
+}
+
 struct bitfield *alloc_bitfield(unsigned int maxbit)
 {
 	unsigned int n;
libmultipath/util.h +1 -0
index 94ed872..21a4088 100644
@@ -49,6 +49,7 @@ int should_exit(void);
 
 void close_fd(void *arg);
 void cleanup_mutex(void *arg);
+void cleanup_free_ptr(void *arg);
 
 struct scandir_result {
 	struct dirent **di;
Signature · 1 line
2.28.0