Skip to contentSkip to conversation

kunit: fix minor error path mistakes

PATCH Superseded Wander Lairson Costa 2 participants 4 messages Apr 18, 2024

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover kunit: fix minor error path mistakes

Hi,

These two patches fix some minor error path mistakes in the device module.

Changes:
--------

v1->v2:
* Add fixes tag.
* Add an imperative statement in the first commit descripton.

Wander Lairson Costa (2):
kunit: unregister the device on error
kunit: avoid memory leak on device register error

 lib/kunit/device.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Signature · 1 line
2.44.0
#1 lore raw

PATCH v2 1/2 kunit: unregister the device on error

kunit_init_device() should unregister the device on bus register error, but mistakenly it tries to unregister the bus.

Unregister the device instead of the bus.

Fixes d03c720e03bd ("kunit: Add APIs for managing devices")
---
 lib/kunit/device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/kunit/device.c +1 -1
index abc603730b8e..25c81ed465fb 100644
@@ -51,7 +51,7 @@ int kunit_bus_init(void)
 
 	error = bus_register(&kunit_bus_type);
 	if (error)
-		bus_unregister(&kunit_bus_type);
+		root_device_unregister(kunit_bus_device);
 	return error;
 }
 
Signature · 1 line
2.44.0
#2 lore raw

PATCH v2 2/2 kunit: avoid memory leak on device register error

In reply to Wander Lairson Costa

If the device register fails, free the allocated memory before returning.

Fixes d03c720e03bd ("kunit: Add APIs for managing devices")
---
 lib/kunit/device.c | 1 +
 1 file changed, 1 insertion(+)
lib/kunit/device.c +1 -0
index 25c81ed465fb..d8c09dcb3e79 100644
@@ -131,6 +131,7 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
 	err = device_register(&kunit_dev->dev);
 	if (err) {
 		put_device(&kunit_dev->dev);
+		kfree(kunit_dev);
 		return ERR_PTR(err);
 	}
 
Signature · 1 line
2.44.0