[PATCH] mtd: nand: Do not dereference before NULL check
Michael Nazzareno Trimarchi
michael at amarulasolutions.com
Sun Aug 24 12:38:41 CEST 2025
Hi
On Thu, Jul 31, 2025 at 6:21 PM Andrew Goodbody <andrew.goodbody at linaro.org>
wrote:
> In nanddev_init mtd and memorg are assigned values that dereference nand
> but this happens before a NULL check for nand. Move the assignments
> after the NULL check.
>
> This issue was found by Smatch.
>
> Signed-off-by: Andrew Goodbody <andrew.goodbody at linaro.org>
> ---
> drivers/mtd/nand/core.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/mtd/nand/core.c b/drivers/mtd/nand/core.c
> index 472ad0bdefb..01ff6e3befa 100644
> --- a/drivers/mtd/nand/core.c
> +++ b/drivers/mtd/nand/core.c
> @@ -201,8 +201,8 @@ EXPORT_SYMBOL_GPL(nanddev_mtd_erase);
> int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
> struct module *owner)
> {
> - struct mtd_info *mtd = nanddev_to_mtd(nand);
> - struct nand_memory_organization *memorg = nanddev_get_memorg(nand);
> + struct mtd_info *mtd;
> + struct nand_memory_organization *memorg;
>
> if (!nand || !ops)
> return -EINVAL;
> @@ -210,6 +210,9 @@ int nanddev_init(struct nand_device *nand, const
> struct nand_ops *ops,
> if (!ops->erase || !ops->markbad || !ops->isbad)
> return -EINVAL;
>
> + mtd = nanddev_to_mtd(nand);
> + memorg = nanddev_get_memorg(nand);
> +
> if (!memorg->bits_per_cell || !memorg->pagesize ||
> !memorg->pages_per_eraseblock || !memorg->eraseblocks_per_lun
> ||
> !memorg->planes_per_lun || !memorg->luns_per_target ||
>
> ---
> base-commit: 79f3e77133bd7248e4579827effc13f97a32a8a8
> change-id: 20250731-nand_core-399525609ec5
>
> Best regards,
> --
> Andrew Goodbody <andrew.goodbody at linaro.org>
>
>
Applied thanks
Michael
--
Michael Nazzareno Trimarchi
Co-Founder & Chief Executive Officer
M. +39 347 913 2170
michael at amarulasolutions.com
__________________________________
Amarula Solutions BV
Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
T. +31 (0)85 111 9172
info at amarulasolutions.com
www.amarulasolutions.com
More information about the U-Boot
mailing list