Re: [PATCH v2 1/2] git-compat-util: add a FREEZ() wrapper around free(ptr); ptr = NULL

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

-Peff



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]