From: Hanyuan Zhao <hanyuan-z@qq.com>
Date: Mon, 6 May 2024 09:10:06 +0000 (+0800)
Subject: riscv: add NULL check before calling strlen in the riscv cpu's get_desc()
X-Git-Tag: v2025.01-rc5-pxa1908~474^2~7
X-Git-Url: http://git.dujemihanovic.xyz/img/html/static/%7B%7B?a=commitdiff_plain;h=9578e74571596eb8f1a74b46cbb2ddf6ed5dee39;p=u-boot.git

riscv: add NULL check before calling strlen in the riscv cpu's get_desc()

Without the NULL check, if the devicetree that u-boot loads does not have a
compatible property then a store access fault will be raised and force the
machine to reset, due to the NULL pointer we passed to strlen. This commit
adds this check and will return -ENOSPC to indicate the get_desc failed.

Signed-off-by: Hanyuan Zhao <zhaohy22@mails.tsinghua.edu.cn>
Reviewed-by: Leo Yu-Chi Liang <ycliang@andestech.com>
---

diff --git a/drivers/cpu/riscv_cpu.c b/drivers/cpu/riscv_cpu.c
index 4f2958a23c..4fff4658b5 100644
--- a/drivers/cpu/riscv_cpu.c
+++ b/drivers/cpu/riscv_cpu.c
@@ -23,7 +23,7 @@ static int riscv_cpu_get_desc(const struct udevice *dev, char *buf, int size)
 	const char *cpu;
 
 	cpu = dev_read_string(dev, "compatible");
-	if (size < (strlen(cpu) + 1))
+	if (!cpu || size < (strlen(cpu) + 1))
 		return -ENOSPC;
 
 	strcpy(buf, cpu);