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. */
STATIC config_format_t options_format = {
sizeof(or_options_t),
{
"or_options_t",
OR_OPTIONS_MAGIC,
offsetof(or_options_t, magic_),
},
option_abbrevs_,
option_deprecation_notes_,
option_vars_,

View 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;
}

View 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)

View 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_t",
OR_STATE_MAGIC,
offsetof(or_state_t, magic_),
},
state_abbrevs_,
NULL,
state_vars_,

View 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_t",
SR_DISK_STATE_MAGIC,
offsetof(sr_disk_state_t, magic_),
},
NULL,
NULL,
state_vars,

View File

@ -131,8 +131,11 @@ static void test_free_cb(void *options);
static config_format_t test_fmt = {
sizeof(test_struct_t),
{
"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),
{
"test_struct_t (with extra lines)",
ETEST_MAGIC,
offsetof(test_struct_t, magic),
},
test_abbrevs,
test_deprecation_notes,
test_vars,