From a5353ae6cf1716c0ff6f7037dc3c336f0d99ecc1 Mon Sep 17 00:00:00 2001 From: Luis Machado Date: Thu, 13 Aug 2020 10:32:52 -0300 Subject: [PATCH] [ARM, sim] Fix build failure with -Werror (PR26365) There is a bit of a situation in the ARM sim with regards to the handling of argv. sim_open () gets a const char **argv, but ARM's sim_open gets clever and decides to modify argv in place via sim_target_parse_command_line. I'm not sure why. In any case, here's a fix that makes the code modify a copy of argv instead. sim/arm/ChangeLog: 2020-08-13 Luis Machado PR sim/26365 * wrapper.c (sim_target_parse_command_line): Free discarded argv entries. (sim_open): Use a duplicate of argv instead of the original argv. --- sim/arm/ChangeLog | 8 ++++++++ sim/arm/wrapper.c | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/sim/arm/ChangeLog b/sim/arm/ChangeLog index d5f96678cd9..1c8f89ab761 100644 --- a/sim/arm/ChangeLog +++ b/sim/arm/ChangeLog @@ -1,3 +1,11 @@ +2020-08-13 Luis Machado + + PR sim/26365 + + * wrapper.c (sim_target_parse_command_line): Free discarded argv + entries. + (sim_open): Use a duplicate of argv instead of the original argv. + 2020-01-17 Christian Biesinger * iwmmxt.c: Fix spelling error (seperate). diff --git a/sim/arm/wrapper.c b/sim/arm/wrapper.c index a5ef415686a..68f8242288f 100644 --- a/sim/arm/wrapper.c +++ b/sim/arm/wrapper.c @@ -672,7 +672,10 @@ sim_target_parse_command_line (int argc, char ** argv) { /* Remove this option from the argv array. */ for (arg = i; arg < argc; arg ++) - argv[arg] = argv[arg + 1]; + { + free (argv[arg]); + argv[arg] = argv[arg + 1]; + } argc --; i --; trace_funcs = 1; @@ -683,7 +686,10 @@ sim_target_parse_command_line (int argc, char ** argv) { /* Remove this option from the argv array. */ for (arg = i; arg < argc; arg ++) - argv[arg] = argv[arg + 1]; + { + free (argv[arg]); + argv[arg] = argv[arg + 1]; + } argc --; i --; disas = 1; @@ -697,7 +703,10 @@ sim_target_parse_command_line (int argc, char ** argv) { /* Remove this option from the argv array. */ for (arg = i; arg < argc; arg ++) - argv[arg] = argv[arg + 1]; + { + free (argv[arg]); + argv[arg] = argv[arg + 1]; + } argc --; ptr = argv[i]; @@ -733,7 +742,10 @@ sim_target_parse_command_line (int argc, char ** argv) /* Remove this option from the argv array. */ for (arg = i; arg < argc; arg ++) - argv[arg] = argv[arg + 1]; + { + free (argv[arg]); + argv[arg] = argv[arg + 1]; + } argc --; i --; } @@ -774,6 +786,7 @@ sim_open (SIM_OPEN_KIND kind, char * const *argv) { int i; + char **argv_copy; SIM_DESC sd = sim_state_alloc (kind, cb); SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); @@ -836,21 +849,24 @@ sim_open (SIM_OPEN_KIND kind, sim_callback = cb; - sim_target_parse_arg_array (argv); + /* Copy over the argv contents so we can modify them. */ + argv_copy = dupargv (argv); - if (argv[1] != NULL) + sim_target_parse_arg_array (argv_copy); + + if (argv_copy[1] != NULL) { int i; /* Scan for memory-size switches. */ - for (i = 0; (argv[i] != NULL) && (argv[i][0] != 0); i++) - if (argv[i][0] == '-' && argv[i][1] == 'm') + for (i = 0; (argv_copy[i] != NULL) && (argv_copy[i][0] != 0); i++) + if (argv_copy[i][0] == '-' && argv_copy[i][1] == 'm') { - if (argv[i][2] != '\0') - mem_size = atoi (&argv[i][2]); - else if (argv[i + 1] != NULL) + if (argv_copy[i][2] != '\0') + mem_size = atoi (&argv_copy[i][2]); + else if (argv_copy[i + 1] != NULL) { - mem_size = atoi (argv[i + 1]); + mem_size = atoi (argv_copy[i + 1]); i++; } else @@ -862,6 +878,8 @@ sim_open (SIM_OPEN_KIND kind, } } + freeargv (argv_copy); + return sd; } -- 2.30.2