[ovs-dev] [PATCH 1/1] util: helper function for freeing allocated array of pointers

Damijan Skvarc damjan.skvarc at gmail.com
Mon Feb 24 11:38:03 UTC 2020


OVS code provides useful functionality svec, which deals with dynamical
vector of strings. This module already provides functionality for adding, 
removing, modifying... strings in this vector. While working on reducing memory
leaks in OVN code I found ovs_cmdl_env_parse_all() function, which allocates
array of strings with the help of svec. Resulting array represents memory
leak (both in ovn-nctl and ovn-sctl) code, however since it is detached for 
svec entity apparent functionality for releasing this memory could not be used.

Instead of assigning allocated memory back to svec (with a purpose to deallocate it
with appropriate function) I was looking for yet another function in OVS which would 
do the same. Since I didn't find it I am proposing the following function. 
I think the most apropriate location would be in util.c module, however if someone
could advice for some other possibility I would be thankful.



Signed-off-by: Damijan Skvarc <damjan.skvarc at gmail.com>
---
 lib/util.c | 13 +++++++++++++
 lib/util.h |  1 +
 2 files changed, 14 insertions(+)

diff --git a/lib/util.c b/lib/util.c
index 830e145..a588c8e 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -346,6 +346,19 @@ xasprintf(const char *format, ...)
     return s;
 }
 
+/* free array of char pointers and array itself */
+void
+xstrsfree(char **strs, size_t size)
+{
+    size_t i;
+
+    for (i = 0; i < size; i++) {
+        free(strs[i]);
+    }
+    free(strs);
+}
+
+
 /* Similar to strlcpy() from OpenBSD, but it never reads more than 'size - 1'
  * bytes from 'src' and doesn't return anything. */
 void
diff --git a/lib/util.h b/lib/util.h
index 7ad8758..284973a 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -159,6 +159,7 @@ bool nullable_string_is_equal(const char *a, const char *b);
 char *xasprintf(const char *format, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE;
 char *xvasprintf(const char *format, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE;
 void *x2nrealloc(void *p, size_t *n, size_t s);
+void  xstrsfree(char **, size_t);
 
 void *xmalloc_cacheline(size_t) MALLOC_LIKE;
 void *xzalloc_cacheline(size_t) MALLOC_LIKE;
-- 
2.7.4



More information about the dev mailing list