[PATCH] driver core: Don't match device with NULL of_node/fwnode

From: Rob Herring (Arm)
Date: Tue Dec 03 2024 - 19:03:43 EST


It recently came up that of_find_device_by_node() will match a device
with a NULL of_node pointer. This is not desired behavior. The returned
struct device is also not deterministic.

Cc: Bjorn Helgaas <bhelgaas@xxxxxxxxxx>
Signed-off-by: Rob Herring (Arm) <robh@xxxxxxxxxx>
---
It would be a bit more efficient to check this up front before we
iterate thru devices, but there's a number of users of these functions
and this isn't really a hot path.

I think at least device_match_acpi_dev() and device_match_acpi_handle()
should also be fixed, but am not sure about the ACPI side.
---
drivers/base/core.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index 94865c9d8adc..87d50c5f710f 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -5246,13 +5246,13 @@ EXPORT_SYMBOL_GPL(device_match_name);

int device_match_of_node(struct device *dev, const void *np)
{
- return dev->of_node == np;
+ return np && (dev->of_node == np);
}
EXPORT_SYMBOL_GPL(device_match_of_node);

int device_match_fwnode(struct device *dev, const void *fwnode)
{
- return dev_fwnode(dev) == fwnode;
+ return fwnode && (dev_fwnode(dev) == fwnode);
}
EXPORT_SYMBOL_GPL(device_match_fwnode);

--
2.45.2