From 7a8b2fe4a9118829cd4bf815052586c640c701c8 Mon Sep 17 00:00:00 2001 From: Robert Schmidt <robert.schmidt@openairinterface.org> Date: Sat, 13 Apr 2024 12:23:09 +0200 Subject: [PATCH] config module: print clear error when config file does not exist Print a clear error if a config file does not exist (unclear previously, when the user would get a confusing, unspecific, "file I/O error" on line 0) I tried to free the memory that had been allocated at that point, and verified that using the address sanitizer. Nevertheless, the function does too much, and is too complicated for refactoring. --- common/config/config_load_configmodule.c | 15 +++++++++++++-- common/config/libconfig/config_libconfig.c | 6 ++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/common/config/config_load_configmodule.c b/common/config/config_load_configmodule.c index 5d6fb5de1ff..411e737976d 100644 --- a/common/config/config_load_configmodule.c +++ b/common/config/config_load_configmodule.c @@ -343,9 +343,20 @@ configmodule_interface_t *load_configmodule(int argc, atoken = strtok_r(NULL,":",&strtokctx); } - printf("[CONFIG] get parameters from %s ", cfgmode); for (i = 0; i < cfgptr->num_cfgP; i++) { - printf("%s ", cfgptr->cfgP[i]); + /* check if that file actually exists */ + if (access(cfgptr->cfgP[i], F_OK) != 0) { + fprintf(stderr, "error: file %s does not exist\n", cfgptr->cfgP[i]); + for (int j = 0; j < cfgptr->num_cfgP; ++j) + free(cfgptr->cfgP[j]); + free(modeparams); + free(cfgptr->cfgmode); + free(cfgptr->argv_info); + free(cfgptr); + if (cfgmode != NULL) + free(cfgmode); + return NULL; + } } if (cfgptr->rtflags & CONFIG_PRINTPARAMS) { diff --git a/common/config/libconfig/config_libconfig.c b/common/config/libconfig/config_libconfig.c index 64737e40673..a41a4dab336 100644 --- a/common/config/libconfig/config_libconfig.c +++ b/common/config/libconfig/config_libconfig.c @@ -576,8 +576,10 @@ int config_libconfig_init(configmodule_interface_t *cfg) config_set_auto_convert (&(libconfig_privdata.cfg), CONFIG_TRUE); /* Read the file. If there is an error, report it and exit. */ if( config_read_file(&(libconfig_privdata.cfg), libconfig_privdata.configfile) == CONFIG_FALSE) { - fprintf(stderr,"[LIBCONFIG] %s %d file %s - line %d: %s\n",__FILE__, __LINE__, - libconfig_privdata.configfile, config_error_line(&(libconfig_privdata.cfg)), + fprintf(stderr, + "[LIBCONFIG] file %s - line %d: %s\n", + libconfig_privdata.configfile, + config_error_line(&(libconfig_privdata.cfg)), config_error_text(&(libconfig_privdata.cfg))); config_destroy(&(libconfig_privdata.cfg)); printf( "\n"); -- GitLab