From 4c85bfedca4d19fe0b4e84897c96d1ddf463ee37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=82=E3=81=8F?= Date: Thu, 29 Jul 2021 18:27:06 +0300 Subject: [PATCH] [FL-1620] Cli command list crash fix. Correct name validation in scripts. (#613) * Scripts: fix incorrect name validation regexp. * Cli: fix command list iteration. --- applications/cli/cli_commands.c | 37 ++++++++++++++++++++------------- scripts/otp.py | 2 +- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/applications/cli/cli_commands.c b/applications/cli/cli_commands.c index 1ee535b5..d23a8c59 100644 --- a/applications/cli/cli_commands.c +++ b/applications/cli/cli_commands.c @@ -94,23 +94,30 @@ void cli_command_help(Cli* cli, string_t args, void* context) { (void)args; printf("Commands we have:"); - // Get the middle element - CliCommandTree_it_t it_mid; - uint8_t cmd_num = CliCommandTree_size(cli->commands); - uint8_t i = cmd_num / 2 + cmd_num % 2; - for(CliCommandTree_it(it_mid, cli->commands); i; --i, CliCommandTree_next(it_mid)) - ; + // Command count + const size_t commands_count = CliCommandTree_size(cli->commands); + const size_t commands_count_mid = commands_count / 2 + commands_count % 2; + // Use 2 iterators from start and middle to show 2 columns - CliCommandTree_it_t it_i; - CliCommandTree_it_t it_j; - for(CliCommandTree_it(it_i, cli->commands), CliCommandTree_it_set(it_j, it_mid); - !CliCommandTree_it_equal_p(it_i, it_mid); - CliCommandTree_next(it_i), CliCommandTree_next(it_j)) { - CliCommandTree_itref_t* ref = CliCommandTree_ref(it_i); + CliCommandTree_it_t it_left; + CliCommandTree_it(it_left, cli->commands); + CliCommandTree_it_t it_right; + CliCommandTree_it(it_right, cli->commands); + for(size_t i = 0; i < commands_count_mid; i++) CliCommandTree_next(it_right); + + // Iterate throw tree + for(size_t i = 0; i < commands_count_mid; i++) { printf("\r\n"); - printf("%-30s", string_get_cstr(ref->key_ptr[0])); - ref = CliCommandTree_ref(it_j); - printf(string_get_cstr(ref->key_ptr[0])); + // Left Column + if(!CliCommandTree_end_p(it_left)) { + printf("%-30s", string_get_cstr(*CliCommandTree_ref(it_left)->key_ptr)); + CliCommandTree_next(it_left); + } + // Right Column + if(!CliCommandTree_end_p(it_right)) { + printf(string_get_cstr(*CliCommandTree_ref(it_right)->key_ptr)); + CliCommandTree_next(it_right); + } }; if(string_size(args) > 0) { diff --git a/scripts/otp.py b/scripts/otp.py index f76ccd8d..999e2766 100755 --- a/scripts/otp.py +++ b/scripts/otp.py @@ -89,7 +89,7 @@ class Main: if len(self.args.name) > 8: self.parser.error("Name is too long. Max 8 symbols.") - if re.match(r"[a-zA-Z0-9]+", self.args.name) is None: + if re.match(r"^[a-zA-Z0-9.]+$", self.args.name) is None: self.parser.error( "Name contains incorrect symbols. Only a-zA-Z0-9 allowed." )