helper/command: simplify exec_command()
The jimtcl interpreter guarantees that the Jim objects in argv[] are not deallocated during the command execution. Thus, there is no need to copy the string content of argv[]. Simplify exec_command() by inlining its two sub-functions and dropping the strdup(). While there, add a LOG_ERROR() for out of memory. Change-Id: I3e21ed7da50ca0bd072edbd49fca9740c81f95b0 Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com> Reviewed-on: https://review.openocd.org/c/openocd/+/8055 Tested-by: jenkins
This commit is contained in:
parent
712c1244e8
commit
f9ea9ce24c
|
@ -154,30 +154,6 @@ static void script_debug(Jim_Interp *interp, unsigned int argc, Jim_Obj * const
|
||||||
free(dbg);
|
free(dbg);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void script_command_args_free(char **words, unsigned nwords)
|
|
||||||
{
|
|
||||||
for (unsigned i = 0; i < nwords; i++)
|
|
||||||
free(words[i]);
|
|
||||||
free(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static char **script_command_args_alloc(unsigned int argc, Jim_Obj * const *argv)
|
|
||||||
{
|
|
||||||
char **words = malloc(argc * sizeof(char *));
|
|
||||||
if (!words)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < argc; i++) {
|
|
||||||
const char *w = Jim_GetString(argv[i], NULL);
|
|
||||||
words[i] = strdup(w);
|
|
||||||
if (!words[i]) {
|
|
||||||
script_command_args_free(words, i);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return words;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct command_context *current_command_context(Jim_Interp *interp)
|
struct command_context *current_command_context(Jim_Interp *interp)
|
||||||
{
|
{
|
||||||
/* grab the command context from the associated data */
|
/* grab the command context from the associated data */
|
||||||
|
@ -898,13 +874,17 @@ static int exec_command(Jim_Interp *interp, struct command_context *cmd_ctx,
|
||||||
return c->jim_handler(interp, argc, argv);
|
return c->jim_handler(interp, argc, argv);
|
||||||
|
|
||||||
/* use c->handler */
|
/* use c->handler */
|
||||||
unsigned int nwords = argc;
|
const char **words = malloc(argc * sizeof(char *));
|
||||||
char **words = script_command_args_alloc(argc, argv);
|
if (!words) {
|
||||||
if (!words)
|
LOG_ERROR("Out of memory");
|
||||||
return JIM_ERR;
|
return JIM_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
int retval = run_command(cmd_ctx, c, (const char **)words, nwords);
|
for (int i = 0; i < argc; i++)
|
||||||
script_command_args_free(words, nwords);
|
words[i] = Jim_GetString(argv[i], NULL);
|
||||||
|
|
||||||
|
int retval = run_command(cmd_ctx, c, words, argc);
|
||||||
|
free(words);
|
||||||
return command_retval_set(interp, retval);
|
return command_retval_set(interp, retval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue