Messages in this thread | | | Date | Tue, 15 Aug 2023 15:53:39 +0200 | From | Peter Zijlstra <> | Subject | Re: cleanup: Make no_free_ptr() __must_check |
| |
On Tue, Aug 15, 2023 at 01:28:37PM +0200, Rasmus Villemoes wrote: > 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.
Right, I can into that as well, that was infact the first thing I tried. Most vexing indeed.
> > 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.
Ah, so the point of the statement expression before the comma is to validate that (p) is in fact a pointer, and to that effect we assign it to a 'void *' temporary.
If that case is invalid, we'll get a compile fail with a dodgy message.
I did this, because (void **)&(p) looses type integrity due to the cast.
But yeah, I suppose it all needs a wee comment.
| |