summaryrefslogtreecommitdiff
path: root/src/common/util.h
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2017-12-04 15:09:18 -0500
committerNick Mathewson <nickm@torproject.org>2017-12-08 14:47:19 -0500
commitdb024adc90069ce9961f3993aba1b7372f09d77a (patch)
treebf42a6612ab7f26e6886604ba08e924dca6a28c6 /src/common/util.h
parent176ad729d9b1ddeccdb3e721b5ab1bf64646223f (diff)
downloadtor-db024adc90069ce9961f3993aba1b7372f09d77a.tar.gz
tor-db024adc90069ce9961f3993aba1b7372f09d77a.zip
Switch to a safer FREE_AND_NULL implementation
This one only evaluates the input once, so it cannot mess up even if there are side effects.
Diffstat (limited to 'src/common/util.h')
-rw-r--r--src/common/util.h21
1 files changed, 16 insertions, 5 deletions
diff --git a/src/common/util.h b/src/common/util.h
index c5bd3f0bda..9ed11260dc 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -109,13 +109,24 @@ extern int dmalloc_free(const char *file, const int line, void *pnt,
void tor_log_mallinfo(int severity);
+/* Helper macro: free a variable of type 'typename' using freefn, and
+ * set the variable to NULL.
+ *
+ * We use this for legacy cases when freefn and typename don't line up
+ * perfectly.
+ */
+#define FREE_AND_NULL_UNMATCHED(typename, freefn, var) \
+ do { \
+ /* only evaluate (var) once. */ \
+ typename **tmp__free__ptr ## freefn = &(var); \
+ freefn(*tmp__free__ptr ## freefn); \
+ (*tmp__free__ptr ## freefn) = NULL; \
+ } while (0)
+
/* Helper macro: free a variable of type 'type' using type_free_, and
* set the variable to NULL. */
-#define FREE_AND_NULL(type, var) \
- do { \
- type ## _free_(var); \
- (var) = NULL; \
- } while (0)
+#define FREE_AND_NULL(type, var) \
+ FREE_AND_NULL_UNMATCHED(type ## _t, type ## _free_, (var))
/** Macro: yield a pointer to the field at position <b>off</b> within the
* structure <b>st</b>. Example: