[PATCH v1] arcnet: Add NULL check in com20020pci_probe()

Henry Martin posted 1 patch 4 days, 17 hours ago
There is a newer version of this series
drivers/net/arcnet/com20020-pci.c | 7 +++++++
1 file changed, 7 insertions(+)
[PATCH v1] arcnet: Add NULL check in com20020pci_probe()
Posted by Henry Martin 4 days, 17 hours ago
devm_kasprintf() returns NULL when memory allocation fails. Currently,
com20020pci_probe() does not check for this case, which results in a
NULL pointer dereference.

Add NULL check after devm_kasprintf() to prevent this issue.

Fixes: 6b17a597fc2f ("arcnet: restoring support for multiple Sohard Arcnet cards")
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
 drivers/net/arcnet/com20020-pci.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/net/arcnet/com20020-pci.c b/drivers/net/arcnet/com20020-pci.c
index c5e571ec94c9..65c6fab0e359 100644
--- a/drivers/net/arcnet/com20020-pci.c
+++ b/drivers/net/arcnet/com20020-pci.c
@@ -264,6 +264,13 @@ static int com20020pci_probe(struct pci_dev *pdev,
 							"pci:red:recon:%d-%d",
 							dev->dev_id, i);
 			card->recon_led.dev = &dev->dev;
+			if (!card->tx_led.default_trigger ||
+			    !card->tx_led.name ||
+			    !card->recon_led.default_trigger ||
+			    !card->recon_led.name) {
+				ret = -ENOMEM;
+				goto err_free_arcdev;
+			}
 
 			ret = devm_led_classdev_register(&pdev->dev, &card->tx_led);
 			if (ret)
-- 
2.34.1
Re: [PATCH v1] arcnet: Add NULL check in com20020pci_probe()
Posted by Andrew Lunn 4 days, 16 hours ago
On Tue, Apr 01, 2025 at 09:49:03PM +0800, Henry Martin wrote:
> devm_kasprintf() returns NULL when memory allocation fails. Currently,
> com20020pci_probe() does not check for this case, which results in a
> NULL pointer dereference.
> 
> Add NULL check after devm_kasprintf() to prevent this issue.

It is more normal to add a test after each devm_kasprintf() rather
than one big one at the end. If the first fails, all the others are
going to fail as well, so you should not bother and just fail the
probe.

https://www.kernel.org/doc/html/latest/process/maintainer-netdev.html

    Andrew

---
pw-bot: cr