qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] semihosting: fix memleak at semihosting_arg_fallback
@ 2023-10-19 18:32 Matheus Tavares Bernardino
  2023-10-19 21:05 ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 2+ messages in thread
From: Matheus Tavares Bernardino @ 2023-10-19 18:32 UTC (permalink / raw)
  To: qemu-devel; +Cc: bcain, alex.bennee, qemu-trivial

We duplicate "cmd" as strtok may modify its argument, but we forgot
to free it later. Furthermore, add_semihosting_arg doesn't take
responsibility for this memory either (it strdup's the argument).

Signed-off-by: Matheus Tavares Bernardino <quic_mathbern@quicinc.com>
---
 semihosting/config.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/semihosting/config.c b/semihosting/config.c
index 249a377ae8..32aa210460 100644
--- a/semihosting/config.c
+++ b/semihosting/config.c
@@ -112,17 +112,19 @@ static int add_semihosting_arg(void *opaque,
 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
 void semihosting_arg_fallback(const char *file, const char *cmd)
 {
-    char *cmd_token;
+    char *cmd_token, *cmd_dup;
 
     /* argv[0] */
     add_semihosting_arg(&semihosting, "arg", file, NULL);
 
     /* split -append and initialize argv[1..n] */
-    cmd_token = strtok(g_strdup(cmd), " ");
+    cmd_dup = g_strdup(cmd);
+    cmd_token = strtok(cmd_dup, " ");
     while (cmd_token) {
         add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
         cmd_token = strtok(NULL, " ");
     }
+    g_free(cmd_dup);
 }
 
 void qemu_semihosting_enable(void)
-- 
2.37.2



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] semihosting: fix memleak at semihosting_arg_fallback
  2023-10-19 18:32 [PATCH] semihosting: fix memleak at semihosting_arg_fallback Matheus Tavares Bernardino
@ 2023-10-19 21:05 ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 2+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-10-19 21:05 UTC (permalink / raw)
  To: Matheus Tavares Bernardino, qemu-devel; +Cc: bcain, alex.bennee, qemu-trivial

On 19/10/23 20:32, Matheus Tavares Bernardino wrote:
> We duplicate "cmd" as strtok may modify its argument, but we forgot
> to free it later. Furthermore, add_semihosting_arg doesn't take
> responsibility for this memory either (it strdup's the argument).
> 
> Signed-off-by: Matheus Tavares Bernardino <quic_mathbern@quicinc.com>
> ---
>   semihosting/config.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/semihosting/config.c b/semihosting/config.c
> index 249a377ae8..32aa210460 100644
> --- a/semihosting/config.c
> +++ b/semihosting/config.c
> @@ -112,17 +112,19 @@ static int add_semihosting_arg(void *opaque,
>   /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
>   void semihosting_arg_fallback(const char *file, const char *cmd)
>   {
> -    char *cmd_token;

Preferably using g_autofree:

        g_autofree char *cmd_dup = g_strdup(cmd);

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>

> +    char *cmd_token, *cmd_dup;
>   
>       /* argv[0] */
>       add_semihosting_arg(&semihosting, "arg", file, NULL);
>   
>       /* split -append and initialize argv[1..n] */
> -    cmd_token = strtok(g_strdup(cmd), " ");
> +    cmd_dup = g_strdup(cmd);
> +    cmd_token = strtok(cmd_dup, " ");
>       while (cmd_token) {
>           add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
>           cmd_token = strtok(NULL, " ");
>       }
> +    g_free(cmd_dup);
>   }
>   
>   void qemu_semihosting_enable(void)



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-10-19 21:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-19 18:32 [PATCH] semihosting: fix memleak at semihosting_arg_fallback Matheus Tavares Bernardino
2023-10-19 21:05 ` Philippe Mathieu-Daudé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).