From 419ba97df5bd168bf773478270c0043f20328e15 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sun, 15 Dec 2019 18:42:01 -0500 Subject: Allow struct_magic to be disabled. When a subsystem is disabled, there will be no corresponding object allocated, and no magic numbers on it. --- src/lib/confmgt/structvar.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/lib/confmgt') diff --git a/src/lib/confmgt/structvar.c b/src/lib/confmgt/structvar.c index de678d18c8..a2411477d6 100644 --- a/src/lib/confmgt/structvar.c +++ b/src/lib/confmgt/structvar.c @@ -29,14 +29,29 @@ #include +/** + * Return true iff all fields on decl 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 object 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, -- cgit v1.2.3-54-g00ecf