diff --git a/subsys/shell/shell_utils.c b/subsys/shell/shell_utils.c index 16490575835..8939ee4aa2b 100644 --- a/subsys/shell/shell_utils.c +++ b/subsys/shell/shell_utils.c @@ -290,8 +290,20 @@ const struct shell_static_entry *z_shell_find_cmd( struct shell_static_entry *dloc) { const struct shell_static_entry *entry; + struct shell_static_entry parent_cpy; size_t idx = 0; + /* Dynamic command operates on shared memory. If we are processing two + * dynamic commands at the same time (current and subcommand) they + * will operate on the same memory region what can cause undefined + * behaviour. + * Hence we need a separate memory for each of them. + */ + if (parent) { + memcpy(&parent_cpy, parent, sizeof(struct shell_static_entry)); + parent = &parent_cpy; + } + while ((entry = z_shell_cmd_get(parent, idx++, dloc)) != NULL) { if (strcmp(cmd_str, entry->syntax) == 0) { return entry;