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

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Fri, 9 Jun 2017 22:04:19 +0000

Add a FREEZ() wrapper marco for the common pattern of freeing a
pointer and assigning NULL to it right afterwards.

The implementation is similar to the (currently unused) XDL_PTRFREE
macro in xdiff/xmacros.h added in commit 3443546f6e ("Use a *real*
built-in diff generator", 2006-03-24). The only difference is that
free() is called unconditionally, see [1].

1. <alpine.DEB.2.20.1608301948310.129229@virtualbox>
   (http://public-inbox.org/git/alpine.DEB.2.20.1608301948310.129229@virtualbox/)

Signed-off-by: Ævar Arnfjörð Bjarmason <[email-masked]>
---
 git-compat-util.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/git-compat-util.h b/git-compat-util.h
index 4b7dcf21ad..ba2d0c8c80 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -805,6 +805,12 @@ extern int xmkstemp_mode(char *template, int mode);
 extern char *xgetcwd(void);
 extern FILE *fopen_for_writing(const char *path);
 
+/*
+ * FREEZ(ptr) is like free(ptr) followed by ptr = NULL. Note that ptr
+ * is used twice, so don't pass e.g. ptr++.
+ */
+#define FREEZ(p) do { free(p); (p) = NULL; } while (0)
+
 #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
 
-- 
2.13.1.508.gb3defc5cc