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

From: Markus Elfring
Date: Fri Apr 19 2024 - 02:15:49 EST


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

Can a description variant (like the following) be more appropriate?

Free the allocated memory (after a device registration failure)
before returning.
Thus add a jump target so that a bit of exception handling can be better
reused at the end of this function implementation.


Would you like to replace the word “register” by “registration” also
in the summary phrase?



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

> @@ -140,6 +138,9 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
> kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
>
> return kunit_dev;
> +error:
> + kfree(kunit_dev);
> + return ERR_PTR(err);
> }


I find it nicer to use a label like free_device.

Regards,
Markus