Re: [PATCH v2 1/2] git-compat-util: add a FREEZ() wrapper around free(ptr); ptr = NULL
- From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
- Date
- Jun 15, 2017, 17:13 UTC
- Message-ID
- <CACBZZX7S9A=mPSgqtmVyZbLdR3GVH+ux3tGN1QXBMEzbg8Ffog@mail.gmail.com>
- In-Reply-To
- <xmqqfuf12o46.fsf@gitster.mtv.corp.google.com>
On Thu, Jun 15, 2017 at 6:48 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 40 quoted lines
> Jeff King <peff@peff.net> writes:
>
>> On Sat, Jun 10, 2017 at 03:21:43AM +0000, Eric Wong wrote:
>>
>>> > So make Jonathan's freez_impl a public API and rename it to
>>> > free_and_null(), perhaps?
>>>
>>> Perhaps... I think it needs to take "void *" to avoid warnings:
>>>
>>> static inline void free_and_null(void *ptrptr)
>>> {
>>> void **tmp = ptrptr;
>>>
>>> free(*tmp);
>>> *tmp = NULL;
>>> }
>>
>> That unfortunately makes it very easy to get it wrong in the callers.
>> Both:
>>
>> free_and_null(&p);
>>
>> and
>>
>> free_and_null(p);
>>
>> would be accepted by the compiler, but one of them causes undefined
>> behavior.
>>
>> Unfortunately using "void **" in the declaration doesn't work, because
>> C's implicit casting rules don't apply to pointer-to-pointer types.
>
> All true.
>
> I still think the macro FREEZ() is too confusing a name to live;
> perhaps we can take Ævar's patch with s/FREEZ/FREE_AND_NULL/ and be
> done with it? By spelling it in all caps, readers will know that
> there is something special going on in that macro, and Eric's
> "forcing the readers to type & in front to let them be aware that
> the ptr variable is being manipulated" may become less necessary.I'll change it to FREE_AND_NULL and submit my patch as-is, my reading of the rest of this thread is that making it a function instead of a macro would be interesting, but has its own caveats that are likely better considered as part of its own series, whereas this just changes existing code to its macro-expanded functional equivalent.