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 #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); int s, nsec = iniparser_getnsec(d);
for (s=0;s!=nsec;++s) { for (s=0;s!=nsec;++s) {
@ -742,6 +742,7 @@ void config_set_from(const dictionary *d)
int k, nkeys = iniparser_getsecnkeys(d, sec); int k, nkeys = iniparser_getsecnkeys(d, sec);
const char *keys[MAXKEYS]; const char *keys[MAXKEYS];
size_t slen = strlen(sec); size_t slen = strlen(sec);
assert(nkeys <= MAXKEYS); assert(nkeys <= MAXKEYS);
assert(slen<sizeof(key)); assert(slen<sizeof(key));
memcpy(key, sec, slen); memcpy(key, sec, slen);
@ -756,6 +757,16 @@ void config_set_from(const dictionary *d)
val = iniparser_getstring(d, keys[k], NULL); val = iniparser_getstring(d, keys[k], NULL);
if (!orig) { if (!orig) {
if (val) { 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); config_set(key, val);
} }
} else { } else {

View File

@ -126,7 +126,7 @@ extern "C" {
void free_params(struct param **pp); void free_params(struct param **pp);
void config_set(const char *key, const char *value); 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); const char *config_get(const char *key);
int config_get_int(const char *key, int def); int config_get_int(const char *key, int def);
double config_get_flt(const char *key, double 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); dictionary_set(ini, "game", NULL);
iniparser_set(ini, "game:id", "42"); iniparser_set(ini, "game:id", "42");
iniparser_set(ini, "game:name", "Eressea"); 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", config_get("game.name"));
CuAssertStrEquals(tc, "Eressea", game_name()); CuAssertStrEquals(tc, "Eressea", game_name());
CuAssertIntEquals(tc, 42, game_id()); CuAssertIntEquals(tc, 42, game_id());

View File

@ -45,33 +45,50 @@ static const char *inifile = "eressea.ini";
static int memdebug = 0; static int memdebug = 0;
static int verbosity = 1; 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; const char *str;
assert(d); str = config_get("game.base");
if (str) {
str = iniparser_getstring(d, "game:base", basedir);
if (str != basedir) {
set_basepath(str); set_basepath(str);
} }
str = iniparser_getstring(d, "game:report", reportdir); str = config_get("game.report");
if (str != reportdir) { if (str) {
set_reportpath(str); set_reportpath(str);
} }
str = iniparser_getstring(d, "game:data", datadir); str = config_get("game.data");
if (str != datadir) { if (str) {
set_datapath(str); set_datapath(str);
} }
lomem = iniparser_getint(d, "game:lomem", lomem) ? 1 : 0; lomem = config_get_int("game.lomem", lomem) ? 1 : 0;
verbosity = config_get_int("game.verbose", 2);
verbosity = iniparser_getint(d, "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) static dictionary *parse_config(const char *filename)
{ {
char path[MAX_PATH]; char path[MAX_PATH];
@ -87,15 +104,8 @@ static dictionary *parse_config(const char *filename)
d = iniparser_load(filename); d = iniparser_load(filename);
} }
if (d) { if (d) {
load_inifile(d); config_set_from(d, valid_keys);
config_set_from(d); load_inifile();
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
} }
str = config_get("game.locales"); str = config_get("game.locales");
make_locales(str ? str : "de,en"); make_locales(str ? str : "de,en");