[PATCH] irqchip/riscv-imsic: Fix 'imsic' dereferenced before NULL check

Kuan-Wei Chiu posted 1 patch 2 months ago
drivers/irqchip/irq-riscv-imsic-platform.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
[PATCH] irqchip/riscv-imsic: Fix 'imsic' dereferenced before NULL check
Posted by Kuan-Wei Chiu 2 months ago
Smatch reported a warning in imsic_irqdomain_init():

drivers/irqchip/irq-riscv-imsic-platform.c:317 imsic_irqdomain_init() warn: variable dereferenced before check 'imsic' (see line 311)

The variable imsic was dereferenced before being checked for NULL.
To fix this, move the initialization of struct irq_domain_info after
the NULL check to avoid accessing imsic prematurely.

Fixes: 59422904dd98 ("irqchip/riscv-imsic: Convert to msi_create_parent_irq_domain() helper")
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <error27@gmail.com>
Closes: https://lore.kernel.org/r/202507311953.NFVZkr0a-lkp@intel.com/
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
 drivers/irqchip/irq-riscv-imsic-platform.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/irqchip/irq-riscv-imsic-platform.c b/drivers/irqchip/irq-riscv-imsic-platform.c
index 74a2a28f9403..04037653f274 100644
--- a/drivers/irqchip/irq-riscv-imsic-platform.c
+++ b/drivers/irqchip/irq-riscv-imsic-platform.c
@@ -307,11 +307,6 @@ static const struct msi_parent_ops imsic_msi_parent_ops = {
 
 int imsic_irqdomain_init(void)
 {
-	struct irq_domain_info info = {
-		.fwnode		= imsic->fwnode,
-		.ops		= &imsic_base_domain_ops,
-		.host_data	= imsic,
-	};
 	struct imsic_global_config *global;
 
 	if (!imsic || !imsic->fwnode) {
@@ -324,6 +319,12 @@ int imsic_irqdomain_init(void)
 		return -ENODEV;
 	}
 
+	struct irq_domain_info info = {
+		.fwnode		= imsic->fwnode,
+		.ops		= &imsic_base_domain_ops,
+		.host_data	= imsic,
+	};
+
 	/* Create Base IRQ domain */
 	imsic->base_domain = msi_create_parent_irq_domain(&info, &imsic_msi_parent_ops);
 	if (!imsic->base_domain) {
-- 
2.34.1
Re: [PATCH] irqchip/riscv-imsic: Fix 'imsic' dereferenced before NULL check
Posted by Thomas Gleixner 2 months ago
On Sat, Aug 02 2025 at 01:24, Kuan-Wei Chiu wrote:
> Smatch reported a warning in imsic_irqdomain_init():
>
> drivers/irqchip/irq-riscv-imsic-platform.c:317 imsic_irqdomain_init() warn: variable dereferenced before check 'imsic' (see line 311)
>
> The variable imsic was dereferenced before being checked for NULL.
> To fix this, move the initialization of struct irq_domain_info after
> the NULL check to avoid accessing imsic prematurely.

It's fixed already w/o moving the struct into the middle of the code:

https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/commit/?h=irq/urgent

But thanks a lot for caring!

Thanks,

        tglx