[PATCH] driver core: faux: Move a variable assignment behind a condition check in faux_device_destroy()

Markus Elfring <[email protected]> Sun, 12 Jul 2026 15:56:09 +0200
Newsgroups org.kernel.vger.kernel-janitors,dev.linux.lists.driver-core,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
From: Markus Elfring <[email protected]>
Date: Sun, 12 Jul 2026 15:45:19 +0200

The address of a data structure member was determined before
a corresponding null pointer check in the implementation of
the function “faux_device_destroy”.

Thus avoid the risk for undefined behaviour by moving the assignment
for the variable “dev” behind a condition check.

This issue was detected by using the Coccinelle software.

Fixes: 35fa2d88ca9481e5caf533d58b99ca259c63b2fe ("driver core: add a faux bus for use when a simple device/bus is needed")
Cc: [email protected]
Signed-off-by: Markus Elfring <[email protected]>
---
 drivers/base/faux.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/base/faux.c b/drivers/base/faux.c
index a8329f88222e..4eb22d082552 100644
--- a/drivers/base/faux.c
+++ b/drivers/base/faux.c
@@ -221,11 +221,12 @@ EXPORT_SYMBOL_GPL(faux_device_create);
  */
 void faux_device_destroy(struct faux_device *faux_dev)
 {
-	struct device *dev = &faux_dev->dev;
+	struct device *dev;
 
 	if (!faux_dev)
 		return;
 
+	dev = &faux_dev->dev;
 	device_del(dev);
 
 	/* The final put_device() will clean up the memory we allocated for this device. */
-- 
2.55.0