Skip to contentSkip to conversation

kunit: fix minor error path mistakes

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

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover kunit: fix minor error path mistakes

Hi,

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

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 1/2 kunit: unregister the device on error

kunit_init_device() should unregister the device on bus register error.

---
 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

Re: [PATCH 1/2] kunit: unregister the device on error

Quoted 1 line

kunit_init_device() should unregister the device on bus register error.

* Would another imperative wording be desirable also for this change description?

* Will the tag “Fixes” become relevant here?

Regards,
Markus

#3 lore raw

Re: [PATCH 1/2] kunit: unregister the device on error

Quoted 6 lines · Markus Elfring
Quoted 2 lines

kunit_init_device() should unregister the device on bus register error.

* Would another imperative wording be desirable also for this change description?

It makes sense, I will change the comment description.

Quoted 2 lines

* Will the tag “Fixes” become relevant here?

I often forget this tag. I will add it.

Quoted 4 lines

Regards,
Markus

#4 lore raw

PATCH 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.

---
 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
#5 lore raw

Re: [PATCH 2/2] kunit: avoid memory leak on device register error

Quoted 2 lines

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

* I suggest to use the word “registration” (instead of “register”) in the commit message.

* Would you like to add the tag “Fixes” accordingly?

lib/kunit/device.c static struct kunit_device *kunit_device_register_internal(struct kunit *test, · line 136

Quoted 8 lines

+++ b/lib/kunit/device.c

lib/kunit/device.c +1 -0
@@ -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);
 	}

Common error handling code can be used instead
if an additional label would be applied for a corresponding jump target.

How do you think about to increase the application of scope-based resource management here?

Regards,
Markus

#6 lore raw

Re: [PATCH 2/2] kunit: avoid memory leak on device register error

Quoted 22 lines · Markus Elfring
Quoted 3 lines

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

* I suggest to use the word “registration” (instead of “register”) in the commit message.

* Would you like to add the tag “Fixes” accordingly?

lib/kunit/device.c static struct kunit_device *kunit_device_register_internal(struct kunit *test, · line 136

Quoted 8 lines

+++ b/lib/kunit/device.c

lib/kunit/device.c +1 -0
@@ -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);
      }

Common error handling code can be used instead
if an additional label would be applied for a corresponding jump target.

Quoted 2 lines

How do you think about to increase the application of scope-based resource management here?

I thought about that. But I think the code is simple enough (for now) to not require an exit label.

Quoted 3 lines

Regards,
Markus

#7 lore raw

Re: [ 2/2] kunit: avoid memory leak on device register error

Quoted 7 lines
Quoted 4 lines

Common error handling code can be used instead
if an additional label would be applied for a corresponding jump target.

How do you think about to increase the application of scope-based resource management here?

I thought about that. But I think the code is simple enough (for now) to not require an exit label.

Please follow a known advice (besides other recommended improvements).
https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources

Regards,
Markus