diff mbox series

[4/6] parser: set_find_path: properly return to-free string array

Message ID 20220530060304.1296389-5-dominique.martinet@atmark-techno.com
State Changes Requested
Headers show
Series Fix gcc warnings or -fanalyzer warnings | expand

Commit Message

Dominique Martinet May 30, 2022, 6:03 a.m. UTC
properly set 'tmp' through an extra indirection instead of leaking
the string array

Reported-by: gcc -fanalyzer
Signed-off-by: Dominique Martinet <dominique.martinet@atmark-techno.com>
---
 core/parsing_library.c              | 5 ++---
 corelib/parsing_library_libconfig.c | 2 +-
 corelib/parsing_library_libjson.c   | 2 +-
 include/parselib.h                  | 2 +-
 parser/parser.c                     | 2 +-
 5 files changed, 6 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/core/parsing_library.c b/core/parsing_library.c
index 3e2ebaaee475..e1574c1434ae 100644
--- a/core/parsing_library.c
+++ b/core/parsing_library.c
@@ -196,7 +196,7 @@  void get_hash_value(parsertype p, void *elem, unsigned char *hash)
 	ascii_to_hash(hash, hash_ascii);
 }
 
-bool set_find_path(const char **nodes, const char *newpath, char **tmp)
+bool set_find_path(const char **nodes, const char *newpath, char ***tmp)
 {
 	char **paths;
 	unsigned int count;
@@ -204,7 +204,6 @@  bool set_find_path(const char **nodes, const char *newpath, char **tmp)
 	char *token, *ref;
 	bool first = true;
 	int allocstr = 0;
-	(void)tmp;
 
 	/*
 	 * Include of files is not supported,
@@ -287,7 +286,7 @@  bool set_find_path(const char **nodes, const char *newpath, char **tmp)
 	}
 
 	free(ref);
-	tmp = paths;
+	*tmp = paths;
 
 	return true;
 }
diff --git a/corelib/parsing_library_libconfig.c b/corelib/parsing_library_libconfig.c
index c6625767506e..1f0bfcdcc409 100644
--- a/corelib/parsing_library_libconfig.c
+++ b/corelib/parsing_library_libconfig.c
@@ -155,7 +155,7 @@  void *find_root_libconfig(config_t *cfg, const char **nodes, unsigned int depth)
 	if (elem && config_setting_is_group(elem) == CONFIG_TRUE) {
 		ref = get_field_string_libconfig(elem, "ref");
 		if (ref) {
-			if (!set_find_path(nodes, ref, tmp)) {
+			if (!set_find_path(nodes, ref, &tmp)) {
 				free(root);
 				return NULL;
 			}
diff --git a/corelib/parsing_library_libjson.c b/corelib/parsing_library_libjson.c
index 4d208d4611c2..9b207aa1d4a0 100644
--- a/corelib/parsing_library_libjson.c
+++ b/corelib/parsing_library_libjson.c
@@ -201,7 +201,7 @@  void *find_root_json(json_object *root, const char **nodes, unsigned int depth)
 		if (type == json_type_object || type == json_type_array) {
 			str = get_field_string_json(node, "ref");
 			if (str) {
-				if (!set_find_path(nodes, str, tmp))
+				if (!set_find_path(nodes, str, &tmp))
 					return NULL;
 				node = find_root_json(root, nodes, depth);
 				free_string_array(tmp);
diff --git a/include/parselib.h b/include/parselib.h
index 66ea2b5afdb6..a22839b8a0db 100644
--- a/include/parselib.h
+++ b/include/parselib.h
@@ -98,7 +98,7 @@  void get_hash_value(parsertype p, void *elem, unsigned char *hash);
 void check_field_string(const char *src, char *dst, const size_t max_len);
 void *find_root(parsertype p, void *root, const char **nodes);
 void *get_node(parsertype p, void *root, const char **nodes);
-bool set_find_path(const char **nodes, const char *newpath, char **tmp);
+bool set_find_path(const char **nodes, const char *newpath, char ***tmp);
 
 #define GET_FIELD_STRING(p, e, name, d) \
 	get_field_string_with_size(p, e, name, d, sizeof(d))
diff --git a/parser/parser.c b/parser/parser.c
index eebd58172e70..3091ae8e4c05 100644
--- a/parser/parser.c
+++ b/parser/parser.c
@@ -145,7 +145,7 @@  static int parser_follow_link(parsertype p, void *cfg, void *elem,
 	for (int j = 0; j < count_string_array(nodes); j++) {
 		linknodes[j] = nodes[j];
 	}
-	if (!set_find_path(linknodes, ref, tmp)) {
+	if (!set_find_path(linknodes, ref, &tmp)) {
 		free(linknodes);
 		return -1;
 	}