]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
move *cmd_array[] from stack to heap
authorLeo C <erbl259-lmu@yahoo.de>
Tue, 10 Apr 2018 06:26:33 +0000 (08:26 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Tue, 10 Apr 2018 06:26:33 +0000 (08:26 +0200)
avr/command.c

index e88e2f37083041823ce05857f4c20c10dc73ab8f..c8a73f0bea37facb1b640451e5dd30d55dedd324 100644 (file)
@@ -166,39 +166,41 @@ command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag UNUSED, int argc, char
 
        if (argc == 1) {        /*show list of commands */
                int cmd_items = cmd_tbl_item_count(tbl_start);
-               cmd_tbl_t *cmd_array[cmd_items];
-               cmd_tbl_t *tp = tbl_start;
-               uint_fast8_t max_len = 0;
+               cmd_tbl_t **cmd_list = (cmd_tbl_t **) malloc(cmd_items * sizeof(cmd_tbl_t *));
+               uint_fast8_t maxlen_cmd = 0;
 
-               /* Make array of commands from .uboot_cmd section */
+               /* Make array of commands */
+               cmd_tbl_t *tp = tbl_start;
                for (int i = 0; i < cmd_items; i++) {
-                       cmd_array[i] = tp++;
-                       uint_fast8_t l = strlen_P(cmd_array[i]->name);
-                       if (l > max_len)
-                               max_len = l;
+                       uint_fast8_t l = strlen_P(tp->name);
+                       if (l > maxlen_cmd)
+                               maxlen_cmd = l;
+                       cmd_list[i] = tp++;
                }
-
                /* Sort command list */
-               qsort(cmd_array, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP);
+               qsort(cmd_list, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP);
 
                /* print short help (usage) */
                for (int i = 0; i < cmd_items; i++) {
-                       if (opt_debug || !(cmd_array[i]->flags & CTBL_DBG)) {
-                               const FLASH char *usage = cmd_array[i]->usage;
+                       if (opt_debug || !(cmd_list[i]->flags & CTBL_DBG)) {
+                               const FLASH char *usage = cmd_list[i]->usage;
 
                                /* allow user abort */
-                               if (ctrlc ())
+                               if (ctrlc ()) {
+                                       free(cmd_list);
                                        return CMD_RET_FAILURE;
+                               }
                                if (usage == NULL)
                                        continue;
 #if defined(GCC_BUG_61443) || 1
-                               print_usage_line(cmd_array[i]->name, max_len, usage);
+                               print_usage_line(cmd_list[i]->name, maxlen_cmd, usage);
 #else
                                printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
-                                               cmd_array[i]->name, usage);
+                                               cmd_list[i]->name, usage);
 #endif
                        }
                }
+               free(cmd_list);
                return CMD_RET_SUCCESS;
        }