Skip to content

Commit

Permalink
semihosting: fix memleak at semihosting_arg_fallback
Browse files Browse the repository at this point in the history
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>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-Id: <03d81c56bfc3d08224e4106efca5949d8894cfa5.1697801632.git.quic_mathbern@quicinc.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Message-Id: <20231029145033.592566-18-alex.bennee@linaro.org>
  • Loading branch information
quic-mathbern authored and stsquad committed Oct 31, 2023
1 parent dbd6623 commit 2eb71a0
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion semihosting/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,13 @@ static int add_semihosting_arg(void *opaque,
void semihosting_arg_fallback(const char *file, const char *cmd)
{
char *cmd_token;
g_autofree char *cmd_dup = g_strdup(cmd);

/* argv[0] */
add_semihosting_arg(&semihosting, "arg", file, NULL);

/* split -append and initialize argv[1..n] */
cmd_token = strtok(g_strdup(cmd), " ");
cmd_token = strtok(cmd_dup, " ");
while (cmd_token) {
add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
cmd_token = strtok(NULL, " ");
Expand Down

0 comments on commit 2eb71a0

Please sign in to comment.