Merge pull request #677 from ennorehling/develop

BUG 2301: validate entries in eressea.ini
This commit is contained in:
Enno Rehling 2017-03-12 21:10:59 +01:00 committed by GitHub
commit 907f07aada
4 changed files with 48 additions and 27 deletions

View File

@ -733,7 +733,7 @@ bool config_changed(int *cache_key) {
}
#define MAXKEYS 16
void config_set_from(const dictionary *d)
void config_set_from(const dictionary *d, const char *valid_keys[])
{
int s, nsec = iniparser_getnsec(d);
for (s=0;s!=nsec;++s) {
@ -742,6 +742,7 @@ void config_set_from(const dictionary *d)
int k, nkeys = iniparser_getsecnkeys(d, sec);
const char *keys[MAXKEYS];
size_t slen = strlen(sec);
assert(nkeys <= MAXKEYS);
assert(slen<sizeof(key));
memcpy(key, sec, slen);
@ -756,6 +757,16 @@ void config_set_from(const dictionary *d)
val = iniparser_getstring(d, keys[k], NULL);
if (!orig) {
if (val) {
if (valid_keys) {
int i;
for (i = 0; valid_keys[i]; ++i) {
size_t vlen = strlen(valid_keys[i]);
if (strncmp(key, valid_keys[i], vlen) == 0) break;
}
if (!valid_keys[i]) {
log_error("unknown key in ini-section %s: %s = %s", sec, key+slen+1, val);
}
}
config_set(key, val);
}
} else {

View File

@ -126,7 +126,7 @@ extern "C" {
void free_params(struct param **pp);
void config_set(const char *key, const char *value);
void config_set_from(const struct _dictionary_ *d);
void config_set_from(const struct _dictionary_ *d, const char *keys[]);
const char *config_get(const char *key);
int config_get_int(const char *key, int def);
double config_get_flt(const char *key, double def);

View File

@ -242,7 +242,7 @@ static void test_config_inifile(CuTest *tc) {
dictionary_set(ini, "game", NULL);
iniparser_set(ini, "game:id", "42");
iniparser_set(ini, "game:name", "Eressea");
config_set_from(ini);
config_set_from(ini, NULL);
CuAssertStrEquals(tc, "Eressea", config_get("game.name"));
CuAssertStrEquals(tc, "Eressea", game_name());
CuAssertIntEquals(tc, 42, game_id());

View File

@ -45,33 +45,50 @@ static const char *inifile = "eressea.ini";
static int memdebug = 0;
static int verbosity = 1;
static void load_inifile(dictionary * d)
static void load_inifile(void)
{
const char *reportdir = reportpath();
const char *datadir = datapath();
const char *basedir = basepath();
const char *str;
assert(d);
str = iniparser_getstring(d, "game:base", basedir);
if (str != basedir) {
str = config_get("game.base");
if (str) {
set_basepath(str);
}
str = iniparser_getstring(d, "game:report", reportdir);
if (str != reportdir) {
str = config_get("game.report");
if (str) {
set_reportpath(str);
}
str = iniparser_getstring(d, "game:data", datadir);
if (str != datadir) {
str = config_get("game.data");
if (str) {
set_datapath(str);
}
lomem = iniparser_getint(d, "game:lomem", lomem) ? 1 : 0;
verbosity = iniparser_getint(d, "game:verbose", 2);
lomem = config_get_int("game.lomem", lomem) ? 1 : 0;
verbosity = config_get_int("game.verbose", 2);
memdebug = config_get_int("game.memcheck", memdebug);
#ifdef USE_CURSES
/* only one value in the [editor] section */
force_color = config_get_int("editor.color", force_color);
gm_codepage = config_get_int("editor.codepage", gm_codepage);
#endif
}
static const char * valid_keys[] = {
"game.id",
"game.name",
"game.locale",
"game.verbose",
"game.report",
"game.lomem",
"game.memcheck",
"game.email",
"game.mailcmd",
"game.sender",
"editor.color",
"editor.codepage",
"lua.",
NULL
};
static dictionary *parse_config(const char *filename)
{
char path[MAX_PATH];
@ -87,15 +104,8 @@ static dictionary *parse_config(const char *filename)
d = iniparser_load(filename);
}
if (d) {
load_inifile(d);
config_set_from(d);
memdebug = iniparser_getint(d, "game:memcheck", memdebug);
#ifdef USE_CURSES
/* only one value in the [editor] section */
force_color = iniparser_getint(d, "editor:color", force_color);
gm_codepage = iniparser_getint(d, "editor:codepage", gm_codepage);
#endif
config_set_from(d, valid_keys);
load_inifile();
}
str = config_get("game.locales");
make_locales(str ? str : "de,en");