diff mbox series

fwts_framework: ensure the string returned from fwts_arch_names is free'd

Message ID 20200424134824.112643-1-colin.king@canonical.com
State Accepted
Headers show
Series fwts_framework: ensure the string returned from fwts_arch_names is free'd | expand

Commit Message

Colin Ian King April 24, 2020, 1:48 p.m. UTC
From: Colin Ian King <colin.king@canonical.com>

The string returned from fwts_arch_names is currently leaking. Fix this
by free'ing the returned object. To do so, the returned string can't be
a const char* so remove the const too.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/include/fwts_arch.h  | 2 +-
 src/lib/src/fwts_arch.c      | 2 +-
 src/lib/src/fwts_framework.c | 6 ++++--
 3 files changed, 6 insertions(+), 4 deletions(-)

Comments

Alex Hung April 24, 2020, 7:23 p.m. UTC | #1
On 2020-04-24 7:48 a.m., Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The string returned from fwts_arch_names is currently leaking. Fix this
> by free'ing the returned object. To do so, the returned string can't be
> a const char* so remove the const too.
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/include/fwts_arch.h  | 2 +-
>  src/lib/src/fwts_arch.c      | 2 +-
>  src/lib/src/fwts_framework.c | 6 ++++--
>  3 files changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/src/lib/include/fwts_arch.h b/src/lib/include/fwts_arch.h
> index 55f1110b..07ec48b1 100644
> --- a/src/lib/include/fwts_arch.h
> +++ b/src/lib/include/fwts_arch.h
> @@ -32,7 +32,7 @@ typedef enum {
>  
>  extern fwts_architecture fwts_arch_get_host(void);
>  extern fwts_architecture fwts_arch_get_arch(const char *name);
> -extern const char *fwts_arch_names(void);
> +extern char *fwts_arch_names(void);
>  extern const char *fwts_arch_get_name(const fwts_architecture arch);
>  
>  #endif
> diff --git a/src/lib/src/fwts_arch.c b/src/lib/src/fwts_arch.c
> index 27fce6b0..4c92e910 100644
> --- a/src/lib/src/fwts_arch.c
> +++ b/src/lib/src/fwts_arch.c
> @@ -69,7 +69,7 @@ fwts_architecture fwts_arch_get_arch(const char *name)
>  	return __fwts_arch_get_arch(name);
>  }
>  
> -const char *fwts_arch_names(void)
> +char *fwts_arch_names(void)
>  {
>  	const struct fwts_arch_info *ptr;
>  	size_t len;
> diff --git a/src/lib/src/fwts_framework.c b/src/lib/src/fwts_framework.c
> index dc1e3642..e1e896bb 100644
> --- a/src/lib/src/fwts_framework.c
> +++ b/src/lib/src/fwts_framework.c
> @@ -1157,8 +1157,10 @@ static int fwts_framework_an_parse(fwts_framework *fw, const char *arg)
>  {
>  	fw->target_arch = fwts_arch_get_arch(arg);
>  	if (fw->target_arch == FWTS_ARCH_OTHER) {
> -		fprintf(stderr, "--arch can be one of: %s\n",
> -			fwts_arch_names());
> +		char *names = fwts_arch_names();
> +
> +		fprintf(stderr, "--arch can be one of: %s\n", names ? names : "<unknown>");
> +		free(names);
>  		return FWTS_ERROR;
>  	}
>  
> 

Acked-by: Alex Hung <alex.hung@canonical.com>
Ivan Hu April 30, 2020, 7:17 a.m. UTC | #2
On 4/24/20 9:48 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The string returned from fwts_arch_names is currently leaking. Fix this
> by free'ing the returned object. To do so, the returned string can't be
> a const char* so remove the const too.
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/include/fwts_arch.h  | 2 +-
>  src/lib/src/fwts_arch.c      | 2 +-
>  src/lib/src/fwts_framework.c | 6 ++++--
>  3 files changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/src/lib/include/fwts_arch.h b/src/lib/include/fwts_arch.h
> index 55f1110b..07ec48b1 100644
> --- a/src/lib/include/fwts_arch.h
> +++ b/src/lib/include/fwts_arch.h
> @@ -32,7 +32,7 @@ typedef enum {
>  
>  extern fwts_architecture fwts_arch_get_host(void);
>  extern fwts_architecture fwts_arch_get_arch(const char *name);
> -extern const char *fwts_arch_names(void);
> +extern char *fwts_arch_names(void);
>  extern const char *fwts_arch_get_name(const fwts_architecture arch);
>  
>  #endif
> diff --git a/src/lib/src/fwts_arch.c b/src/lib/src/fwts_arch.c
> index 27fce6b0..4c92e910 100644
> --- a/src/lib/src/fwts_arch.c
> +++ b/src/lib/src/fwts_arch.c
> @@ -69,7 +69,7 @@ fwts_architecture fwts_arch_get_arch(const char *name)
>  	return __fwts_arch_get_arch(name);
>  }
>  
> -const char *fwts_arch_names(void)
> +char *fwts_arch_names(void)
>  {
>  	const struct fwts_arch_info *ptr;
>  	size_t len;
> diff --git a/src/lib/src/fwts_framework.c b/src/lib/src/fwts_framework.c
> index dc1e3642..e1e896bb 100644
> --- a/src/lib/src/fwts_framework.c
> +++ b/src/lib/src/fwts_framework.c
> @@ -1157,8 +1157,10 @@ static int fwts_framework_an_parse(fwts_framework *fw, const char *arg)
>  {
>  	fw->target_arch = fwts_arch_get_arch(arg);
>  	if (fw->target_arch == FWTS_ARCH_OTHER) {
> -		fprintf(stderr, "--arch can be one of: %s\n",
> -			fwts_arch_names());
> +		char *names = fwts_arch_names();
> +
> +		fprintf(stderr, "--arch can be one of: %s\n", names ? names : "<unknown>");
> +		free(names);
>  		return FWTS_ERROR;
>  	}
>  
> 

Acked-by: Ivan Hu <ivan.hu@canonical.com>
diff mbox series

Patch

diff --git a/src/lib/include/fwts_arch.h b/src/lib/include/fwts_arch.h
index 55f1110b..07ec48b1 100644
--- a/src/lib/include/fwts_arch.h
+++ b/src/lib/include/fwts_arch.h
@@ -32,7 +32,7 @@  typedef enum {
 
 extern fwts_architecture fwts_arch_get_host(void);
 extern fwts_architecture fwts_arch_get_arch(const char *name);
-extern const char *fwts_arch_names(void);
+extern char *fwts_arch_names(void);
 extern const char *fwts_arch_get_name(const fwts_architecture arch);
 
 #endif
diff --git a/src/lib/src/fwts_arch.c b/src/lib/src/fwts_arch.c
index 27fce6b0..4c92e910 100644
--- a/src/lib/src/fwts_arch.c
+++ b/src/lib/src/fwts_arch.c
@@ -69,7 +69,7 @@  fwts_architecture fwts_arch_get_arch(const char *name)
 	return __fwts_arch_get_arch(name);
 }
 
-const char *fwts_arch_names(void)
+char *fwts_arch_names(void)
 {
 	const struct fwts_arch_info *ptr;
 	size_t len;
diff --git a/src/lib/src/fwts_framework.c b/src/lib/src/fwts_framework.c
index dc1e3642..e1e896bb 100644
--- a/src/lib/src/fwts_framework.c
+++ b/src/lib/src/fwts_framework.c
@@ -1157,8 +1157,10 @@  static int fwts_framework_an_parse(fwts_framework *fw, const char *arg)
 {
 	fw->target_arch = fwts_arch_get_arch(arg);
 	if (fw->target_arch == FWTS_ARCH_OTHER) {
-		fprintf(stderr, "--arch can be one of: %s\n",
-			fwts_arch_names());
+		char *names = fwts_arch_names();
+
+		fprintf(stderr, "--arch can be one of: %s\n", names ? names : "<unknown>");
+		free(names);
 		return FWTS_ERROR;
 	}