shell: fix tab for dynamic commands

The tabulator handler creates a single structure if it is handling
dynamic commands. If the currently processed dynamic command has a
dynamic subcommand they both share the same structure.
As a result tabulation operation may result in undefined behaviour.

As a solution, a new structure was introduced to keep subcommand
information.

Fixes #35926.

Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@nordicsemi.no>
This commit is contained in:
Jakub Rzeszutko 2021-06-04 15:01:49 +02:00 committed by Kumar Gala
parent 24bd45b287
commit 4a85ecacda

View file

@ -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;