]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Allow struct_magic to be disabled.
authorNick Mathewson <nickm@torproject.org>
Sun, 15 Dec 2019 23:42:01 +0000 (18:42 -0500)
committerNick Mathewson <nickm@torproject.org>
Thu, 19 Dec 2019 12:54:56 +0000 (07:54 -0500)
When a subsystem is disabled, there will be no corresponding object
allocated, and no magic numbers on it.

src/lib/conf/conftypes.h
src/lib/confmgt/structvar.c

index 44171068a1cfda02f939b0a79f63e0cb21778a88..52f9fceb20e8476229cde59cd143c3565a07939b 100644 (file)
@@ -131,6 +131,9 @@ typedef struct struct_member_t {
  *
  * These 'magic numbers' are 32-bit values used to tag objects to make sure
  * that they have the correct type.
+ *
+ * If all fields in this structure are zero or 0, the magic-number check is
+ * not performed.
  */
 typedef struct struct_magic_decl_t {
   /** The name of the structure */
index de678d18c812ea5621d21322648343d2c4ff332b..a2411477d63e3203ecf1cfb436679ace934ba0d3 100644 (file)
 
 #include <stddef.h>
 
+/**
+ * Return true iff all fields on <b>decl</b> are NULL or 0, indicating that
+ * there is no object or no magic number to check.
+ **/
+static inline bool
+magic_is_null(const struct_magic_decl_t *decl)
+{
+  return decl->typename == NULL &&
+    decl->magic_offset == 0 &&
+    decl->magic_val == 0;
+}
+
 /**
  * Set the 'magic number' on <b>object</b> to correspond to decl.
  **/
 void
 struct_set_magic(void *object, const struct_magic_decl_t *decl)
 {
-  tor_assert(object);
   tor_assert(decl);
+  if (magic_is_null(decl))
+    return;
+
+  tor_assert(object);
   uint32_t *ptr = STRUCT_VAR_P(object, decl->magic_offset);
   *ptr = decl->magic_val;
 }
@@ -47,8 +62,11 @@ struct_set_magic(void *object, const struct_magic_decl_t *decl)
 void
 struct_check_magic(const void *object, const struct_magic_decl_t *decl)
 {
-  tor_assert(object);
   tor_assert(decl);
+  if (magic_is_null(decl))
+    return;
+
+  tor_assert(object);
 
   const uint32_t *ptr = STRUCT_VAR_P(object, decl->magic_offset);
   tor_assertf(*ptr == decl->magic_val,