]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Use struct_magic_decl to verify magic numbers in config objects
authorNick Mathewson <nickm@torproject.org>
Wed, 19 Jun 2019 12:34:20 +0000 (08:34 -0400)
committerNick Mathewson <nickm@torproject.org>
Tue, 25 Jun 2019 16:51:25 +0000 (12:51 -0400)
src/app/config/config.c
src/app/config/confparse.c
src/app/config/confparse.h
src/app/config/statefile.c
src/feature/dirauth/shared_random_state.c
src/test/test_confparse.c

index 074df070578e3f005b050652b7e5415d7e990a4f..37cbe6b2efccc29e518d1cb3622b3d01abe5dc8c 100644 (file)
@@ -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_,
index be4341e3f64d1a84a45e689fa8d4a89d212084f9..752d16c844b569bb36627f865ebca6bfa250ce54 100644 (file)
@@ -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;
 }
index 5897085e63a02d7e038e1b51fba98dfe240487d3..4ef4e708f3f0bb61aa330ca235463e3dda9a1569 100644 (file)
@@ -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)
index 358b02f602b189f165cbea9f1a2fc6c5be04b4ba..331592c3a6b2cfcaa34dc7865368d7632e23cc76 100644 (file)
@@ -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_,
index cf4a6543253e10dd51fed097ef0268d487528cc9..da4187b38af52bee775691d78d953c5c75cac998 100644 (file)
@@ -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,
index 27696a537aabadbce647615e7b57eab94e75e6aa..9e626356d3e93f369ad1b61617aa5c00198a2ca7 100644 (file)
@@ -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,