Make config_var and config_fmt const.

Now that we have a reasonable implementation for overriding the
default options for TestingTorNetwork, we don't need to modify
config_var_t structs any more.  And therefore, we can have constant
format options, like reasonable people.
This commit is contained in:
Nick Mathewson 2019-06-21 11:20:21 -04:00
parent f306d12b58
commit f8b193a74a
7 changed files with 22 additions and 30 deletions

View File

@ -191,7 +191,7 @@ static const char unix_q_socket_prefix[] = "unix:\"";
/** A list of abbreviations and aliases to map command-line options, obsolete /** A list of abbreviations and aliases to map command-line options, obsolete
* option names, or alternative option names, to their current values. */ * option names, or alternative option names, to their current values. */
static config_abbrev_t option_abbrevs_[] = { static const config_abbrev_t option_abbrevs_[] = {
PLURAL(AuthDirBadDirCC), PLURAL(AuthDirBadDirCC),
PLURAL(AuthDirBadExitCC), PLURAL(AuthDirBadExitCC),
PLURAL(AuthDirInvalidCC), PLURAL(AuthDirInvalidCC),
@ -301,7 +301,7 @@ DUMMY_TYPECHECK_INSTANCE(or_options_t);
* abbreviations, order is significant, since the first matching option will * abbreviations, order is significant, since the first matching option will
* be chosen first. * be chosen first.
*/ */
static config_var_t option_vars_[] = { static const config_var_t option_vars_[] = {
V(AccountingMax, MEMUNIT, "0 bytes"), V(AccountingMax, MEMUNIT, "0 bytes"),
VAR("AccountingRule", STRING, AccountingRule_option, "max"), VAR("AccountingRule", STRING, AccountingRule_option, "max"),
V(AccountingStart, STRING, NULL), V(AccountingStart, STRING, NULL),
@ -851,7 +851,7 @@ static void set_protocol_warning_severity_level(int warning_severity);
#define OR_OPTIONS_MAGIC 9090909 #define OR_OPTIONS_MAGIC 9090909
/** Configuration format for or_options_t. */ /** Configuration format for or_options_t. */
STATIC config_format_t options_format = { STATIC const config_format_t options_format = {
sizeof(or_options_t), sizeof(or_options_t),
{ {
"or_options_t", "or_options_t",

View File

@ -248,7 +248,7 @@ int options_any_client_port_set(const or_options_t *options);
STATIC int options_act(const or_options_t *old_options); STATIC int options_act(const or_options_t *old_options);
#ifdef TOR_UNIT_TESTS #ifdef TOR_UNIT_TESTS
extern struct config_format_t options_format; extern const struct config_format_t options_format;
#endif #endif
STATIC port_cfg_t *port_cfg_new(size_t namelen); STATIC port_cfg_t *port_cfg_new(size_t namelen);

View File

@ -100,9 +100,13 @@ config_find_deprecation(const config_format_t *fmt, const char *key)
return NULL; return NULL;
} }
/** As config_find_option, but return a non-const pointer. */ /** If <b>key</b> is a configuration option, return the corresponding const
config_var_t * * config_var_t. Otherwise, if <b>key</b> is a non-standard abbreviation,
config_find_option_mutable(config_format_t *fmt, const char *key) * warn, and return the corresponding const config_var_t. Otherwise return
* NULL.
*/
const config_var_t *
config_find_option(const config_format_t *fmt, const char *key)
{ {
int i; int i;
size_t keylen = strlen(key); size_t keylen = strlen(key);
@ -127,17 +131,6 @@ config_find_option_mutable(config_format_t *fmt, const char *key)
return NULL; return NULL;
} }
/** If <b>key</b> is a configuration option, return the corresponding const
* config_var_t. Otherwise, if <b>key</b> is a non-standard abbreviation,
* warn, and return the corresponding const config_var_t. Otherwise return
* NULL.
*/
const config_var_t *
config_find_option(const config_format_t *fmt, const char *key)
{
return config_find_option_mutable((config_format_t*)fmt, key);
}
/** Return the number of option entries in <b>fmt</b>. */ /** Return the number of option entries in <b>fmt</b>. */
static int static int
config_count_options(const config_format_t *fmt) config_count_options(const config_format_t *fmt)

View File

@ -47,16 +47,17 @@ typedef void (*free_cfg_fn_t)(void*);
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. */
struct_magic_decl_t magic; /**< Magic number info for this struct. */ struct_magic_decl_t magic; /**< Magic number info for this struct. */
config_abbrev_t *abbrevs; /**< List of abbreviations that we expand when const config_abbrev_t *abbrevs; /**< List of abbreviations that we expand
* parsing this format. */ * when parsing this format. */
const config_deprecation_t *deprecations; /** List of deprecated options */ const config_deprecation_t *deprecations; /** List of deprecated options */
config_var_t *vars; /**< List of variables we recognize, their default const config_var_t *vars; /**< List of variables we recognize, their default
* values, and where we stick them in the structure. */ * values, and where we stick them in the
* structure. */
validate_fn_t validate_fn; /**< Function to validate config. */ validate_fn_t validate_fn; /**< Function to validate config. */
free_cfg_fn_t free_fn; /**< Function to free the configuration. */ free_cfg_fn_t free_fn; /**< Function to free the configuration. */
/** If present, extra denotes a LINELIST variable for unrecognized /** If present, extra denotes a LINELIST variable for unrecognized
* lines. Otherwise, unrecognized lines are an error. */ * lines. Otherwise, unrecognized lines are an error. */
struct_member_t *extra; const struct_member_t *extra;
} config_format_t; } 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
@ -93,8 +94,6 @@ bool config_check_ok(const config_format_t *fmt, const void *options,
int config_assign(const config_format_t *fmt, void *options, int config_assign(const config_format_t *fmt, void *options,
struct config_line_t *list, struct config_line_t *list,
unsigned flags, char **msg); unsigned flags, char **msg);
config_var_t *config_find_option_mutable(config_format_t *fmt,
const char *key);
const char *config_find_deprecation(const config_format_t *fmt, const char *config_find_deprecation(const config_format_t *fmt,
const char *key); const char *key);
const config_var_t *config_find_option(const config_format_t *fmt, const config_var_t *config_find_option(const config_format_t *fmt,

View File

@ -76,7 +76,7 @@ DUMMY_TYPECHECK_INSTANCE(or_state_t);
VAR(#member, conftype, member, initvalue) VAR(#member, conftype, member, initvalue)
/** Array of "state" variables saved to the ~/.tor/state file. */ /** Array of "state" variables saved to the ~/.tor/state file. */
static config_var_t state_vars_[] = { static const config_var_t state_vars_[] = {
/* Remember to document these in state-contents.txt ! */ /* Remember to document these in state-contents.txt ! */
V(AccountingBytesReadInInterval, MEMUNIT, NULL), V(AccountingBytesReadInInterval, MEMUNIT, NULL),

View File

@ -65,7 +65,7 @@ disk_state_validate_cb(void *old_state, void *state, void *default_state,
static void disk_state_free_cb(void *); static void disk_state_free_cb(void *);
/* Array of variables that are saved to disk as a persistent state. */ /* Array of variables that are saved to disk as a persistent state. */
static config_var_t state_vars[] = { static const config_var_t state_vars[] = {
V(Version, POSINT, "0"), V(Version, POSINT, "0"),
V(TorVersion, STRING, NULL), V(TorVersion, STRING, NULL),
V(ValidAfter, ISOTIME, NULL), V(ValidAfter, ISOTIME, NULL),
@ -81,7 +81,7 @@ static config_var_t state_vars[] = {
/* "Extra" variable in the state that receives lines we can't parse. This /* "Extra" variable in the state that receives lines we can't parse. This
* lets us preserve options from versions of Tor newer than us. */ * lets us preserve options from versions of Tor newer than us. */
static struct_member_t state_extra_var = { static const struct_member_t state_extra_var = {
.name = "__extra", .name = "__extra",
.type = CONFIG_TYPE_LINELIST, .type = CONFIG_TYPE_LINELIST,
.offset = offsetof(sr_disk_state_t, ExtraLines), .offset = offsetof(sr_disk_state_t, ExtraLines),

View File

@ -55,7 +55,7 @@ static test_struct_t test_struct_t_dummy;
#define OBSOLETE(varname) \ #define OBSOLETE(varname) \
CONFIG_VAR_OBSOLETE(varname) CONFIG_VAR_OBSOLETE(varname)
static config_var_t test_vars[] = { static const config_var_t test_vars[] = {
V(s, STRING, "hello"), V(s, STRING, "hello"),
V(fn, FILENAME, NULL), V(fn, FILENAME, NULL),
V(pos, POSINT, NULL), V(pos, POSINT, NULL),
@ -123,7 +123,7 @@ static void test_free_cb(void *options);
#define TEST_MAGIC 0x1337 #define TEST_MAGIC 0x1337
static config_format_t test_fmt = { static const config_format_t test_fmt = {
sizeof(test_struct_t), sizeof(test_struct_t),
{ {
"test_struct_t", "test_struct_t",