From 67277fb1d1ca0bd8b36ca66185ea7a475968f2de Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Tue, 4 Jul 2017 11:42:05 +0200 Subject: [PATCH] followup for e2df6d2: default directories, files, and groups * do not read ini_exts array beyond the end * do not use local variable (val) address outside of the variable's scope * do not read $HOME/my.cnf * do not create files in the user's $HOME, use $MYSQL_TMP_DIR * test reading of the executable name group * fail the test if .my.cnf cannot be opened --- libmariadb/ma_default.c | 9 ++------ libmariadb/mariadb_lib.c | 46 +++++++++++++++++++------------------- unittest/libmariadb/misc.c | 34 +++++++++++++--------------- 3 files changed, 41 insertions(+), 48 deletions(-) diff --git a/libmariadb/ma_default.c b/libmariadb/ma_default.c index 84c790e9..59d811d0 100644 --- a/libmariadb/ma_default.c +++ b/libmariadb/ma_default.c @@ -118,11 +118,6 @@ char **get_default_configuration_dirs() if ((env= getenv("MYSQL_HOME")) && add_cfg_dir(configuration_dirs, env)) goto error; -#ifndef _WIN32 - if ((env= getenv("HOME")) && - add_cfg_dir(configuration_dirs, env)) - goto error; -#endif end: return configuration_dirs; error: @@ -304,7 +299,7 @@ my_bool _mariadb_read_options(MYSQL *mysql, for (i=0; i < MAX_CONFIG_DIRS && configuration_dirs[i]; i++) { - for (exts= 0; exts < 2; exts++) + for (exts= 0; ini_exts[exts]; exts++) { snprintf(filename, FN_REFLEN, "%s%cmy.%s", configuration_dirs[i], FN_LIBCHAR, ini_exts[exts]); @@ -316,7 +311,7 @@ my_bool _mariadb_read_options(MYSQL *mysql, /* special case: .my.cnf in Home directory */ if ((env= getenv("HOME"))) { - for (exts= 0; exts < 2; exts++) + for (exts= 0; ini_exts[exts]; exts++) { snprintf(filename, FN_REFLEN, "%s%c.my.%s", env, FN_LIBCHAR, ini_exts[exts]); diff --git a/libmariadb/mariadb_lib.c b/libmariadb/mariadb_lib.c index 93a202b3..7d942346 100644 --- a/libmariadb/mariadb_lib.c +++ b/libmariadb/mariadb_lib.c @@ -701,35 +701,35 @@ my_bool _mariadb_set_conf_option(MYSQL *mysql, const char *config_option, const { if (!strcmp(mariadb_defaults[i].conf_key, config_option)) { + my_bool val_bool; + int val_int; + size_t val_sizet; int rc; void *option_val= NULL; switch (mariadb_defaults[i].type) { case MARIADB_OPTION_BOOL: - { - my_bool val= 0; - if (config_value) - val= atoi(config_value); - option_val= &val; - break; - } + val_bool= 0; + if (config_value) + val_bool= atoi(config_value); + option_val= &val_bool; + break; case MARIADB_OPTION_INT: - { - int val= 0; - if (config_value) - val= atoi(config_value); - option_val= &val; - break; - } + val_int= 0; + if (config_value) + val_int= atoi(config_value); + option_val= &val_int; + break; case MARIADB_OPTION_SIZET: - { - size_t val= 0; - if (config_value) - val= strtol(config_value, NULL, 10); - option_val= &val; - break; - } - default: - option_val= (void *)config_value; + val_sizet= 0; + if (config_value) + val_sizet= strtol(config_value, NULL, 10); + option_val= &val_sizet; + break; + case MARIADB_OPTION_STR: + option_val= (void*)config_value; + break; + case MARIADB_OPTION_NONE: + break; } rc= mysql_optionsv(mysql, mariadb_defaults[i].option, option_val); return(test(rc)); diff --git a/unittest/libmariadb/misc.c b/unittest/libmariadb/misc.c index b2d604b6..d3de3382 100644 --- a/unittest/libmariadb/misc.c +++ b/unittest/libmariadb/misc.c @@ -1060,34 +1060,32 @@ static int test_mdev12965(MYSQL *unused __attribute__((unused))) MYSQL *mysql; my_bool reconnect = 0; FILE *fp= NULL; - char *env= getenv("HOME"); - char cnf_file1[FN_REFLEN + 1], - cnf_file2[FN_REFLEN + 1]; + const char *env= getenv("MYSQL_TMP_DIR"); + char cnf_file1[FN_REFLEN + 1]; + + if (!env) + env= "/tmp"; + + setenv("HOME", env, 1); snprintf(cnf_file1, FN_REFLEN, "%s%c.my.cnf", env, FN_LIBCHAR); - snprintf(cnf_file2, FN_REFLEN, "%s%cmy.cnf", env, FN_LIBCHAR); - if (!access(cnf_file1, R_OK) || !access(cnf_file2, R_OK)) - { - diag("Skip this test, it would overwrite configuration files in your home directory"); - return SKIP; - } + diag("Config file: %s", cnf_file1); + + FAIL_IF(!access(cnf_file1, R_OK), "access"); mysql= mysql_init(NULL); fp= fopen(cnf_file1, "w"); - fprintf(fp, "[test]\ndefault-character-set=latin2"); + FAIL_IF(!fp, "fopen"); + + fprintf(fp, "[misc]\ndefault-character-set=latin2\n[client]\nreconnect=1\n"); fclose(fp); - fp= fopen(cnf_file2, "w"); - fprintf(fp, "[test]\nreconnect=1"); - fclose(fp); - - mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "test"); - my_test_connect(mysql, hostname, username, password, schema, - 0, socketname, 0), mysql_error(mysql); + mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, NULL); + my_test_connect(mysql, hostname, username, password, + schema, 0, socketname, 0); remove(cnf_file1); - remove(cnf_file2); FAIL_IF(strcmp(mysql_character_set_name(mysql), "latin2"), "expected charset latin2"); mysql_get_optionv(mysql, MYSQL_OPT_RECONNECT, &reconnect);