[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Use struct_magic_decl to verify magic numbers in config objects



commit 59317c8a238f49ad298d4f51f42a0f7b16c9cc3c
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date:   Wed Jun 19 08:34:20 2019 -0400

    Use struct_magic_decl to verify magic numbers in config objects
---
 src/app/config/config.c                   |  7 +++++--
 src/app/config/confparse.c                |  2 +-
 src/app/config/confparse.h                |  9 +++------
 src/app/config/statefile.c                |  7 +++++--
 src/feature/dirauth/shared_random_state.c |  7 +++++--
 src/test/test_confparse.c                 | 14 ++++++++++----
 6 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/src/app/config/config.c b/src/app/config/config.c
index 074df0705..37cbe6b2e 100644
--- a/src/app/config/config.c
+++ b/src/app/config/config.c
@@ -877,8 +877,11 @@ static void set_protocol_warning_severity_level(int warning_severity);
 /** Configuration format for or_options_t. */
 STATIC config_format_t options_format = {
   sizeof(or_options_t),
-  OR_OPTIONS_MAGIC,
-  offsetof(or_options_t, magic_),
+  {
+   "or_options_t",
+   OR_OPTIONS_MAGIC,
+   offsetof(or_options_t, magic_),
+  },
   option_abbrevs_,
   option_deprecation_notes_,
   option_vars_,
diff --git a/src/app/config/confparse.c b/src/app/config/confparse.c
index be4341e3f..752d16c84 100644
--- a/src/app/config/confparse.c
+++ b/src/app/config/confparse.c
@@ -39,7 +39,7 @@ void *
 config_new(const config_format_t *fmt)
 {
   void *opts = tor_malloc_zero(fmt->size);
-  *(uint32_t*)STRUCT_VAR_P(opts, fmt->magic_offset) = fmt->magic;
+  struct_set_magic(opts, &fmt->magic);
   CONFIG_CHECK(fmt, opts);
   return opts;
 }
diff --git a/src/app/config/confparse.h b/src/app/config/confparse.h
index 5897085e6..4ef4e708f 100644
--- a/src/app/config/confparse.h
+++ b/src/app/config/confparse.h
@@ -96,9 +96,7 @@ typedef void (*free_cfg_fn_t)(void*);
  * configuration or storage format. */
 typedef struct config_format_t {
   size_t size; /**< Size of the struct that everything gets parsed into. */
-  uint32_t magic; /**< Required 'magic value' to make sure we have a struct
-                   * of the right type. */
-  off_t magic_offset; /**< Offset of the magic value within the struct. */
+  struct_magic_decl_t magic; /**< Magic number info for this struct. */
   config_abbrev_t *abbrevs; /**< List of abbreviations that we expand when
                              * parsing this format. */
   const config_deprecation_t *deprecations; /** List of deprecated options */
@@ -114,9 +112,8 @@ typedef struct config_format_t {
 /** Macro: assert that <b>cfg</b> has the right magic field for format
  * <b>fmt</b>. */
 #define CONFIG_CHECK(fmt, cfg) STMT_BEGIN                               \
-    tor_assert(fmt && cfg);                                             \
-    tor_assert((fmt)->magic ==                                          \
-               *(uint32_t*)STRUCT_VAR_P(cfg,fmt->magic_offset));        \
+    tor_assert(fmt);                                                    \
+    struct_check_magic((cfg), &fmt->magic);                             \
   STMT_END
 
 #define CAL_USE_DEFAULTS      (1u<<0)
diff --git a/src/app/config/statefile.c b/src/app/config/statefile.c
index 358b02f60..331592c3a 100644
--- a/src/app/config/statefile.c
+++ b/src/app/config/statefile.c
@@ -166,8 +166,11 @@ static struct_member_t state_extra_var = {
 /** Configuration format for or_state_t. */
 static const config_format_t state_format = {
   sizeof(or_state_t),
-  OR_STATE_MAGIC,
-  offsetof(or_state_t, magic_),
+  {
+   "or_state_t",
+   OR_STATE_MAGIC,
+   offsetof(or_state_t, magic_),
+  },
   state_abbrevs_,
   NULL,
   state_vars_,
diff --git a/src/feature/dirauth/shared_random_state.c b/src/feature/dirauth/shared_random_state.c
index cf4a65432..da4187b38 100644
--- a/src/feature/dirauth/shared_random_state.c
+++ b/src/feature/dirauth/shared_random_state.c
@@ -94,8 +94,11 @@ static struct_member_t state_extra_var = {
 /* Configuration format of sr_disk_state_t. */
 static const config_format_t state_format = {
   sizeof(sr_disk_state_t),
-  SR_DISK_STATE_MAGIC,
-  offsetof(sr_disk_state_t, magic_),
+  {
+   "sr_disk_state_t",
+   SR_DISK_STATE_MAGIC,
+   offsetof(sr_disk_state_t, magic_),
+  },
   NULL,
   NULL,
   state_vars,
diff --git a/src/test/test_confparse.c b/src/test/test_confparse.c
index 27696a537..9e626356d 100644
--- a/src/test/test_confparse.c
+++ b/src/test/test_confparse.c
@@ -131,8 +131,11 @@ static void test_free_cb(void *options);
 
 static config_format_t test_fmt = {
   sizeof(test_struct_t),
-  TEST_MAGIC,
-  offsetof(test_struct_t, magic),
+  {
+   "test_struct_t",
+   TEST_MAGIC,
+   offsetof(test_struct_t, magic),
+  },
   test_abbrevs,
   test_deprecation_notes,
   test_vars,
@@ -774,8 +777,11 @@ static struct_member_t extra = {
 
 static config_format_t etest_fmt = {
   sizeof(test_struct_t),
-  ETEST_MAGIC,
-  offsetof(test_struct_t, magic),
+  {
+   "test_struct_t (with extra lines)",
+   ETEST_MAGIC,
+   offsetof(test_struct_t, magic),
+  },
   test_abbrevs,
   test_deprecation_notes,
   test_vars,



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits