Skip to content

Commit 4a85eca

Browse files
Jakub Rzeszutkogalak
authored andcommitted
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 <[email protected]>
1 parent 24bd45b commit 4a85eca

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

subsys/shell/shell_utils.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,20 @@ const struct shell_static_entry *z_shell_find_cmd(
290290
struct shell_static_entry *dloc)
291291
{
292292
const struct shell_static_entry *entry;
293+
struct shell_static_entry parent_cpy;
293294
size_t idx = 0;
294295

296+
/* Dynamic command operates on shared memory. If we are processing two
297+
* dynamic commands at the same time (current and subcommand) they
298+
* will operate on the same memory region what can cause undefined
299+
* behaviour.
300+
* Hence we need a separate memory for each of them.
301+
*/
302+
if (parent) {
303+
memcpy(&parent_cpy, parent, sizeof(struct shell_static_entry));
304+
parent = &parent_cpy;
305+
}
306+
295307
while ((entry = z_shell_cmd_get(parent, idx++, dloc)) != NULL) {
296308
if (strcmp(cmd_str, entry->syntax) == 0) {
297309
return entry;

0 commit comments

Comments
 (0)