Use struct_magic_decl to verify magic numbers in config objects

This commit is contained in:
Nick Mathewson 2019-06-19 08:34:20 -04:00
parent 3a4d67cf45
commit 59317c8a23
6 changed files with 29 additions and 17 deletions

View File

@ -877,8 +877,11 @@ static void set_protocol_warning_severity_level(int warning_severity);
/** Configuration format for or_options_t. */ /** Configuration format for or_options_t. */
STATIC config_format_t options_format = { STATIC config_format_t options_format = {
sizeof(or_options_t), 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_abbrevs_,
option_deprecation_notes_, option_deprecation_notes_,
option_vars_, option_vars_,

View File

@ -39,7 +39,7 @@ void *
config_new(const config_format_t *fmt) config_new(const config_format_t *fmt)
{ {
void *opts = tor_malloc_zero(fmt->size); 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); CONFIG_CHECK(fmt, opts);
return opts; return opts;
} }

View File

@ -96,9 +96,7 @@ typedef void (*free_cfg_fn_t)(void*);
* configuration or storage format. */ * configuration or storage format. */
typedef struct config_format_t { typedef struct config_format_t {
size_t size; /**< Size of the struct that everything gets parsed into. */ 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 struct_magic_decl_t magic; /**< Magic number info for this struct. */
* of the right type. */
off_t magic_offset; /**< Offset of the magic value within the struct. */
config_abbrev_t *abbrevs; /**< List of abbreviations that we expand when config_abbrev_t *abbrevs; /**< List of abbreviations that we expand when
* parsing this format. */ * parsing this format. */
const config_deprecation_t *deprecations; /** List of deprecated options */ 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 /** Macro: assert that <b>cfg</b> has the right magic field for format
* <b>fmt</b>. */ * <b>fmt</b>. */
#define CONFIG_CHECK(fmt, cfg) STMT_BEGIN \ #define CONFIG_CHECK(fmt, cfg) STMT_BEGIN \
tor_assert(fmt && cfg); \ tor_assert(fmt); \
tor_assert((fmt)->magic == \ struct_check_magic((cfg), &fmt->magic); \
*(uint32_t*)STRUCT_VAR_P(cfg,fmt->magic_offset)); \
STMT_END STMT_END
#define CAL_USE_DEFAULTS (1u<<0) #define CAL_USE_DEFAULTS (1u<<0)

View File

@ -166,8 +166,11 @@ static struct_member_t state_extra_var = {
/** Configuration format for or_state_t. */ /** Configuration format for or_state_t. */
static const config_format_t state_format = { static const config_format_t state_format = {
sizeof(or_state_t), 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_, state_abbrevs_,
NULL, NULL,
state_vars_, state_vars_,

View File

@ -94,8 +94,11 @@ static struct_member_t state_extra_var = {
/* Configuration format of sr_disk_state_t. */ /* Configuration format of sr_disk_state_t. */
static const config_format_t state_format = { static const config_format_t state_format = {
sizeof(sr_disk_state_t), 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,
NULL, NULL,
state_vars, state_vars,

View File

@ -131,8 +131,11 @@ static void test_free_cb(void *options);
static config_format_t test_fmt = { static config_format_t test_fmt = {
sizeof(test_struct_t), sizeof(test_struct_t),
TEST_MAGIC, {
offsetof(test_struct_t, magic), "test_struct_t",
TEST_MAGIC,
offsetof(test_struct_t, magic),
},
test_abbrevs, test_abbrevs,
test_deprecation_notes, test_deprecation_notes,
test_vars, test_vars,
@ -774,8 +777,11 @@ static struct_member_t extra = {
static config_format_t etest_fmt = { static config_format_t etest_fmt = {
sizeof(test_struct_t), 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_abbrevs,
test_deprecation_notes, test_deprecation_notes,
test_vars, test_vars,