Re: cleanup: Make no_free_ptr() __must_check

From: Rasmus Villemoes
Date: Tue Aug 15 2023 - 07:29:32 EST


On 15/08/2023 12.52, Peter Zijlstra wrote:
>
> recent discussion brought about the realization that it makes sense for
> no_free_ptr() to have __must_check semantics in order to avoid leaking
> the resource.
>

> +static inline __must_check void * __no_free_ptr(void **pp)
> +{ void *p = *pp; *pp = NULL; return p; }
> +
> #define no_free_ptr(p) \
> - ({ __auto_type __ptr = (p); (p) = NULL; __ptr; })
> + (({ void * __maybe_unused ___t = (p); }), \
> + ((typeof(p))__no_free_ptr((void **)&(p))))

So this does seem to work as advertised, but it could perhaps use some
comments. Because at first I read this as one big statement expression,
and I had this memory of a __must_check function call being the last
expression in such had no effect at all [1]. But this is actually a
comma expression.

Also, isn't it more complicated than necessary? Can we get rid of the
inner stmt expr and tmp var by just making it

((void) (p), ((typeof(p))__no_free_ptr((void **)&(p)))

which is more or less the whole reason comma expressions is a thing.

Rasmus

[1]
https://lore.kernel.org/lkml/6d190601-68f1-c086-97ac-2ee1c08f5a34@xxxxxxxxxxxxxxxxxx/