Re: [PATCH 01/13] device property: Add cleanup.h based fwnode_handle_put() scope based cleanup.

Lukas Wunner
2024-01-21T18:20:20Z

On Sun, Jan 21, 2024 at 07:06:03PM +0100, Lukas Wunner wrote:
> On Sun, Jan 14, 2024 at 05:19:57PM +0000, Jonathan Cameron wrote:
> > v1: Thanks to Andy for reviewing the RFC.
> > Add check for if (!IS_ERR_OR_NULL(_T)) to allow the compiler to optimize
> > cases where it knows the passed in parameter is NULL or an error pointer.
>
> Heads-up: Using IS_ERR_OR_NULL() in DEFINE_FREE() macros bloats
> the code with additional IS_ERR() checks and NULL pointer checks.
>
> See the detailed explanation in this patch which adds a DEFINE_FREE()
> macro for x509_free_certificate():
>
> https://lore.kernel.org/all/70ecd3904a70d2b92f8f1e04365a2b9ce66fac25.1705857475.git.lukas@wunner.de/
>
> I'm wondering if a solution might be to stop returning IS_ERR()
> from "constructors" such as x509_cert_parse() and instead assign
> the created "object" (x509_certificate) to a call-by-reference
> pointer and return an integer. If the returned integer is not 0,
> inhibit "destruction" of the "object" with no_free_ptr().

Another idea would be to use a call-by-reference pointer and check
the pointer instead of the return code.

E.g.:

DEFINE_FREE(x509_free_certificate, struct x509_certificate *,
if (_T) x509_free_certificate(_T))

...

struct x509_certificate __free(x509_free_certificate) = NULL;
int ret;

ret = x509_cert_parse(&cert, buf, len);
if (!cert)
return ret;