]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
riscv: add NULL check before calling strlen in the riscv cpu's get_desc()
authorHanyuan Zhao <hanyuan-z@qq.com>
Mon, 6 May 2024 09:10:06 +0000 (17:10 +0800)
committerLeo Yu-Chi Liang <ycliang@andestech.com>
Tue, 14 May 2024 10:39:57 +0000 (18:39 +0800)
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>
drivers/cpu/riscv_cpu.c

index 4f2958a23cee0abfea3f1ca1f43beb954fcfe728..4fff4658b5fda9e500cffe4637c47d65cd2c7d9d 100644 (file)
@@ -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);