[U-Boot] [PATCH] common: fix help command breakage

Kim Phillips kim.phillips at freescale.com
Tue Nov 6 02:51:11 CET 2012


commit 199adb601ff34bdbbd0667fac80dfe0a87bffc2b "common/misc: sparse
fixes" broke the help command trying to fix the sparse error
"command.c:44:38: error: bad constant expression".

As Henrik points out, the fix was bad because the commit used
CONFIG_SYS_MAXARGS whereas the code intended to use the maximum
number of commands (not arguments to a command).

this patch fixes both by making the allocation manually on the heap.

Reported-by: Henrik Nordström <henrik at henriknordstrom.net>
Signed-off-by: Kim Phillips <kim.phillips at freescale.com>
---
tested on an 8572ds board

 common/command.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/common/command.c b/common/command.c
index f51df26..991a7ee 100644
--- a/common/command.c
+++ b/common/command.c
@@ -28,6 +28,7 @@
 #include <common.h>
 #include <command.h>
 #include <linux/ctype.h>
+#include <malloc.h>
 
 /*
  * Use puts() instead of printf() to avoid printf buffer overflow
@@ -40,17 +41,16 @@ int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
 	int i;
 	int rcode = 0;
 
-	if (cmd_items > CONFIG_SYS_MAXARGS) {
-		printf("%s: cmd_items %d exceeds hardcoded limit %d."
-		       " Recompile with higher CONFIG_SYS_MAXARGS?\n",
-		       __func__, cmd_items, CONFIG_SYS_MAXARGS);
-		return -1;
-	}
-
 	if (argc == 1) {	/*show list of commands */
-		cmd_tbl_t *cmd_array[CONFIG_SYS_MAXARGS];
+		cmd_tbl_t **cmd_array;
 		int i, j, swaps;
 
+		cmd_array = malloc(cmd_items * sizeof(*cmd_array));
+		if (!cmd_array) {
+			printf("error: not enough memory\n");
+			return 1;
+		}
+
 		/* Make array of commands from .uboot_cmd section */
 		cmdtp = cmd_start;
 		for (i = 0; i < cmd_items; i++) {
@@ -79,13 +79,16 @@ int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
 			const char *usage = cmd_array[i]->usage;
 
 			/* allow user abort */
-			if (ctrlc ())
+			if (ctrlc()) {
+				free(cmd_array);
 				return 1;
+			}
 			if (usage == NULL)
 				continue;
 			printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
 			       cmd_array[i]->name, usage);
 		}
+		free(cmd_array);
 		return 0;
 	}
 	/*
-- 
1.8.0




More information about the U-Boot mailing list