From: Marek Vasut <marek.vasut+renesas@mailbox.org>
Date: Thu, 2 Mar 2023 03:08:18 +0000 (+0100)
Subject: cmd: fdt: Check argc before accessing argv in fdt bootcpu
X-Git-Tag: v2025.01-rc5-pxa1908~1023^2~36^2~30
X-Git-Url: http://git.dujemihanovic.xyz/img/static/html/%7B%7B?a=commitdiff_plain;h=9d019f5106cc677fefd8ab6ccfc5ba0ed9e0b738;p=u-boot.git

cmd: fdt: Check argc before accessing argv in fdt bootcpu

On case 'fdt bootcpu' is invoked without parameters, argv[2] is not
valid and this command would SEGFAULT in sandbox environment. Add
missing argc test to avoid the crash and rather print usage help
message.

Signed-off-by: Marek Vasut <marek.vasut+renesas@mailbox.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
---

diff --git a/cmd/fdt.c b/cmd/fdt.c
index 29d748891d..734c9b36a0 100644
--- a/cmd/fdt.c
+++ b/cmd/fdt.c
@@ -597,7 +597,12 @@ static int do_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	 * Set boot cpu id
 	 */
 	} else if (strncmp(argv[1], "boo", 3) == 0) {
-		unsigned long tmp = hextoul(argv[2], NULL);
+		unsigned long tmp;
+
+		if (argc != 3)
+			return CMD_RET_USAGE;
+
+		tmp = hextoul(argv[2], NULL);
 		fdt_set_boot_cpuid_phys(working_fdt, tmp);
 
 	/*