diff mbox series

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

Message ID 20240617072905.2275940-2-dominique.martinet@atmark-techno.com
State Accepted
Headers show
Series misc fixes from chunked stream rework | expand

Commit Message

Dominique Martinet June 17, 2024, 7:29 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(-)

Comments

Stefano Babic June 18, 2024, 8:57 p.m. UTC | #1
On 17.06.24 09:29, Dominique Martinet wrote:
> 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 --git a/core/parsing_library.c b/core/parsing_library.c
> index a97f8114becc..038c6851adf9 100644
> --- a/core/parsing_library.c
> +++ b/core/parsing_library.c
> @@ -210,7 +210,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;
> @@ -218,7 +218,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,
> @@ -301,7 +300,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 ddb79f6fb152..4ddc338031a0 100644
> --- a/corelib/parsing_library_libconfig.c
> +++ b/corelib/parsing_library_libconfig.c
> @@ -196,7 +196,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 4728d200fc8e..9456d51eca9d 100644
> --- a/corelib/parsing_library_libjson.c
> +++ b/corelib/parsing_library_libjson.c
> @@ -237,7 +237,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 99b6519fa480..48f38d2a8c92 100644
> --- a/include/parselib.h
> +++ b/include/parselib.h
> @@ -64,7 +64,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 04d7b72c9b9a..2273796e0452 100644
> --- a/parser/parser.c
> +++ b/parser/parser.c
> @@ -144,7 +144,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;
>   	}

Reviewed-by: Stefano Babic <stefano.babic@swupdate.org>
diff mbox series

Patch

diff --git a/core/parsing_library.c b/core/parsing_library.c
index a97f8114becc..038c6851adf9 100644
--- a/core/parsing_library.c
+++ b/core/parsing_library.c
@@ -210,7 +210,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;
@@ -218,7 +218,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,
@@ -301,7 +300,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 ddb79f6fb152..4ddc338031a0 100644
--- a/corelib/parsing_library_libconfig.c
+++ b/corelib/parsing_library_libconfig.c
@@ -196,7 +196,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 4728d200fc8e..9456d51eca9d 100644
--- a/corelib/parsing_library_libjson.c
+++ b/corelib/parsing_library_libjson.c
@@ -237,7 +237,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 99b6519fa480..48f38d2a8c92 100644
--- a/include/parselib.h
+++ b/include/parselib.h
@@ -64,7 +64,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 04d7b72c9b9a..2273796e0452 100644
--- a/parser/parser.c
+++ b/parser/parser.c
@@ -144,7 +144,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;
 	}