From 3f8355762eee03c4a8366212fa92f521d029ff18 Mon Sep 17 00:00:00 2001 From: Stefan Schubert Date: Mon, 9 Dec 2024 16:49:52 +0100 Subject: [PATCH] Removed PATH_MAX (#226) * PATH_MAX removed * using econf_readConfig in the example --- example/CMakeLists.txt | 1 + example/example.c | 29 +++-- include/libeconf.h | 6 +- lib/getfilecontents.c | 16 ++- lib/helpers.c | 5 +- lib/libeconf.c | 70 +++++++++--- meson.build | 5 +- tests/tst-arguments1.c | 2 +- tests/tst-arguments2.c | 2 +- tests/tst-arguments3.c | 2 +- tests/tst-arguments4.c | 2 +- tests/tst-arguments5.c | 2 +- tests/tst-checkfiles.c | 2 +- tests/tst-comments.c | 2 +- tests/tst-delimiter-comment.c | 2 +- tests/tst-extvalue.c | 2 +- tests/tst-filedoesnotexit1.c | 2 +- tests/tst-getconfdirs1.c | 2 +- tests/tst-getconfdirs3.c | 2 +- tests/tst-getconfdirs4.c | 2 +- tests/tst-getconfdirs5.c | 2 +- tests/tst-getconfdirs6.c | 2 +- tests/tst-getconfdirs7.c | 2 +- tests/tst-getconfdirs8.c | 2 +- tests/tst-getconfdirs9.c | 2 +- tests/tst-getpath.c | 2 +- tests/tst-groups1.c | 2 +- tests/tst-groups2.c | 2 +- tests/tst-groups3.c | 2 +- tests/tst-groups4.c | 2 +- tests/tst-groups5.c | 2 +- tests/tst-groups6.c | 2 +- tests/tst-logindefs1.c | 2 +- tests/tst-logindefs2.c | 2 +- tests/tst-long-name.c | 2 +- tests/tst-man-dir.c | 2 +- tests/tst-merge1.c | 2 +- tests/tst-merge2.c | 2 +- tests/tst-merge3.c | 2 +- tests/tst-merge4.c | 2 +- tests/tst-merge5.c | 2 +- tests/tst-options.c | 2 +- tests/tst-parse-error.c | 2 +- tests/tst-parseconfig1.c | 2 +- tests/tst-python.c | 2 +- tests/tst-quote1.c | 2 +- tests/tst-security.c | 2 +- tests/tst-setgetvalues1.c | 2 +- tests/tst-shells1.c | 4 +- tests/tst-shells2.c | 2 +- tests/tst-string.c | 2 +- tests/tst-without-suffix.c | 2 +- tests/tst-write-comments.c | 4 +- util/econftool.c | 201 +++++++++++++++++++++------------- 54 files changed, 274 insertions(+), 155 deletions(-) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 22c66d5..5bec267 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,3 +1,4 @@ # Create the binary/executable add_executable(example EXCLUDE_FROM_ALL example.c) +target_compile_options(example PRIVATE -DDEXAMPLEDIR=\"${PROJECT_SOURCE_DIR}/example/\") target_link_libraries(example PRIVATE econf) diff --git a/example/example.c b/example/example.c index 3c442c8..b89c992 100644 --- a/example/example.c +++ b/example/example.c @@ -36,15 +36,28 @@ int main(void) { printf("------------------------ OUTPUT START ------------------------\n"); clock_t begin = clock(); - - //econf_file *key_file = econf_newIniFile(); - //econf_file *key_file = econf_readFile("example/etc/example/example.ini", "=", "#"); - econf_file *key_file; - if (econf_readDirs(&key_file, "example/usr/etc", "example/etc", - "example", ".ini", "=", "#")) - return 1; /* XXX better error handling */ + // econf_newKeyFile_with_options is needed only for this example which has own + // test data structure. In normal cases it is NOT needed because the configuration + // files in the default directories on your system will be parsed. + econf_err ret = econf_newKeyFile_with_options(&key_file, "ROOT_PREFIX="EXAMPLEDIR); + if (ret != ECONF_SUCCESS) { + fprintf (stderr, "ERROR: couldn't allocate new key_file: %s\n", + econf_errString(ret)); + return 1; + } + + ret = econf_readConfig (&key_file, + NULL, + "/usr/etc", + "example", + ".ini", "=", "#"); + if (ret != ECONF_SUCCESS) { + fprintf (stderr, "ERROR: couldn't read data: %s\n", + econf_errString(ret)); + return 1; + } econf_setInt64Value(key_file, "[Basic Types]", "Int", INT64_MAX); int64_t i64val; @@ -92,7 +105,7 @@ int main(void) { puts("\n"); econf_free(keys); - econf_writeFile(key_file, "example/", "test.ini"); + econf_writeFile(key_file, "./", "test.ini"); econf_free(key_file); diff --git a/include/libeconf.h b/include/libeconf.h index e88c4a2..95fbbd5 100644 --- a/include/libeconf.h +++ b/include/libeconf.h @@ -1096,7 +1096,11 @@ static __inline__ void econf_freeArrayp(char ***array) { *array = econf_freeArray(*array); } -/** @brief Free memory allocated by e.g. econf_readFile(), econf_readDirs(),... +/** @brief Free memory allocated and returned by + * econf_readFile(), econf_readFileWithCallback, + * econf_readDirs(), econf_readDirsWithCallback, + * econf_readDirsHistory, econf_readDirsHistoryWithCallback, + * econf_readConfig, econf_readConfigWithCallback * * @param key_file allocated data * @return void diff --git a/lib/getfilecontents.c b/lib/getfilecontents.c index 29e1b46..168429a 100644 --- a/lib/getfilecontents.c +++ b/lib/getfilecontents.c @@ -38,8 +38,7 @@ /*info for reporting scan errors (line Nr, filename) */ static uint64_t last_scanned_line_nr = 0; -static char last_scanned_filename[PATH_MAX]; - +static char *last_scanned_filename = NULL; // Checking file permissions, uid, group,... bool file_owner_set = false; @@ -338,8 +337,6 @@ read_file(econf_file *ef, const char *file, if (kf == NULL) return ECONF_NOFILE; - snprintf(last_scanned_filename, sizeof(last_scanned_filename), "%s", file); - check_delim(delim, &has_wsp, &has_nonwsp); ef->path = strdup (file); @@ -641,11 +638,20 @@ read_file(econf_file *ef, const char *file, join_same_entries(ef); } + if (retval != ECONF_SUCCESS && retval != ECONF_NOFILE) { + free(last_scanned_filename); + last_scanned_filename = strdup(file); + if (last_scanned_filename == NULL) { + return ECONF_NOMEM; + } + } + return retval; } void last_scanned_file(char **filename, uint64_t *line_nr) { *line_nr = last_scanned_line_nr; - *filename = strdup(last_scanned_filename); + *filename = last_scanned_filename; + last_scanned_filename = NULL; /* Freeing is the responsilble of econf_errLocation now */ } diff --git a/lib/helpers.c b/lib/helpers.c index 4704119..06724df 100644 --- a/lib/helpers.c +++ b/lib/helpers.c @@ -53,13 +53,12 @@ void initialize(econf_file *key_file, size_t num) { char *get_absolute_path(const char *path, econf_err *error) { char *absolute_path; if(*path != '/') { - char buffer[PATH_MAX]; - if(!realpath(path, buffer)) { + absolute_path = realpath(path, NULL); + if(absolute_path == NULL) { if (error) *error = ECONF_NOFILE; return NULL; } - absolute_path = strdup(buffer); } else { absolute_path = strdup(path); } diff --git a/lib/libeconf.c b/lib/libeconf.c index 66b3aa2..5122955 100644 --- a/lib/libeconf.c +++ b/lib/libeconf.c @@ -331,9 +331,9 @@ econf_err econf_readConfigWithCallback(econf_file **key_file, bool (*callback)(const char *filename, const void *data), const void *callback_data) { - char usr_dir[PATH_MAX]; - char run_dir[PATH_MAX]; - char etc_dir[PATH_MAX]; + char *usr_dir = NULL; + char *run_dir = NULL; + char *etc_dir = NULL; econf_err ret = ECONF_SUCCESS; int init_keyfile = 0; @@ -359,28 +359,63 @@ econf_err econf_readConfigWithCallback(econf_file **key_file, if (usr_subdir == NULL) usr_subdir = ""; + int re = 0; if ((*key_file)->root_prefix) { if (project != NULL) { - snprintf(usr_dir, sizeof(usr_dir), "%s/%s/%s", (*key_file)->root_prefix, usr_subdir, project); - snprintf(run_dir, sizeof(run_dir), "%s/%s/%s", (*key_file)->root_prefix, DEFAULT_RUN_SUBDIR, project); - snprintf(etc_dir, sizeof(etc_dir), "%s/%s/%s", (*key_file)->root_prefix, DEFAULT_ETC_SUBDIR, project); + re = asprintf(&usr_dir, "%s/%s/%s", (*key_file)->root_prefix, usr_subdir, project); } else { - snprintf(usr_dir, sizeof(usr_dir), "%s%s", (*key_file)->root_prefix, usr_subdir); - snprintf(run_dir, sizeof(run_dir), "%s%s", (*key_file)->root_prefix, DEFAULT_RUN_SUBDIR); - snprintf(etc_dir, sizeof(etc_dir), "%s%s", (*key_file)->root_prefix, DEFAULT_ETC_SUBDIR); + re = asprintf(&usr_dir, "%s%s", (*key_file)->root_prefix, usr_subdir); } } else { if (project != NULL) { - snprintf(usr_dir, sizeof(usr_dir), "%s/%s", usr_subdir, project); - snprintf(run_dir, sizeof(run_dir), "%s/%s", DEFAULT_RUN_SUBDIR, project); - snprintf(etc_dir, sizeof(etc_dir), "%s/%s", DEFAULT_ETC_SUBDIR, project); + re = asprintf(&usr_dir, "%s/%s", usr_subdir, project); } else { - snprintf(usr_dir, sizeof(usr_dir), "%s", usr_subdir); - snprintf(run_dir, sizeof(run_dir), "%s", DEFAULT_RUN_SUBDIR); - snprintf(etc_dir, sizeof(etc_dir), "%s", DEFAULT_ETC_SUBDIR); + re = asprintf(&usr_dir, "%s", usr_subdir); } } + if (re < 0) + return ECONF_NOMEM; + + if ((*key_file)->root_prefix) { + if (project != NULL) { + re = asprintf(&run_dir, "%s/%s/%s", (*key_file)->root_prefix, DEFAULT_RUN_SUBDIR, project); + } else { + re = asprintf(&run_dir, "%s%s", (*key_file)->root_prefix, DEFAULT_RUN_SUBDIR); + } + } else { + if (project != NULL) { + re = asprintf(&run_dir, "%s/%s", DEFAULT_RUN_SUBDIR, project); + } else { + re = asprintf(&run_dir, "%s", DEFAULT_RUN_SUBDIR); + } + } + + if (re < 0) { + free(usr_dir); + return ECONF_NOMEM; + } + + if ((*key_file)->root_prefix) { + if (project != NULL) { + re = asprintf(&etc_dir, "%s/%s/%s", (*key_file)->root_prefix, DEFAULT_ETC_SUBDIR, project); + } else { + re = asprintf(&etc_dir, "%s%s", (*key_file)->root_prefix, DEFAULT_ETC_SUBDIR); + } + } else { + if (project != NULL) { + re = asprintf(&etc_dir, "%s/%s", DEFAULT_ETC_SUBDIR, project); + } else { + re = asprintf(&etc_dir, "%s", DEFAULT_ETC_SUBDIR); + } + } + + if (re < 0) { + free(usr_dir); + free(run_dir); + return ECONF_NOMEM; + } + if ((*key_file)->parse_dirs_count == 0) { /* taking default */ (*key_file)->parse_dirs_count = 3; @@ -404,6 +439,10 @@ econf_err econf_readConfigWithCallback(econf_file **key_file, if (init_keyfile && ret != ECONF_SUCCESS) *key_file = econf_free(*key_file); + free(usr_dir); + free(run_dir); + free(etc_dir); + return ret; } @@ -824,5 +863,6 @@ econf_file *econf_freeFile(econf_file *key_file) { econf_freeArray(key_file->conf_dirs); free(key_file->root_prefix); free(key_file); + return NULL; } diff --git a/meson.build b/meson.build index 49b90ef..e8a8383 100644 --- a/meson.build +++ b/meson.build @@ -88,8 +88,11 @@ libeconf_dep = declare_dependency( ) # Example +exampledir = join_paths(meson.source_root(), 'example/') +example_args = ['-DEXAMPLEDIR="' + exampledir + '"'] +executable('example', example_src, c_args: example_args, dependencies : libeconf_dep) -executable('example', example_src, dependencies : libeconf_dep) +# Commandline interface executable('econftool', econftool_src, dependencies : libeconf_dep, install : true, ) # Unit tests diff --git a/tests/tst-arguments1.c b/tests/tst-arguments1.c index b6fde4d..ab017bd 100644 --- a/tests/tst-arguments1.c +++ b/tests/tst-arguments1.c @@ -15,7 +15,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; char *val; econf_err error; diff --git a/tests/tst-arguments2.c b/tests/tst-arguments2.c index 72f576f..77d2314 100644 --- a/tests/tst-arguments2.c +++ b/tests/tst-arguments2.c @@ -14,7 +14,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; char *val; econf_err error; diff --git a/tests/tst-arguments3.c b/tests/tst-arguments3.c index 59a30e4..058cd9d 100644 --- a/tests/tst-arguments3.c +++ b/tests/tst-arguments3.c @@ -14,7 +14,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; char *val; econf_err error; diff --git a/tests/tst-arguments4.c b/tests/tst-arguments4.c index 485de6b..e02cd3d 100644 --- a/tests/tst-arguments4.c +++ b/tests/tst-arguments4.c @@ -14,7 +14,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; char *val; econf_err error; diff --git a/tests/tst-arguments5.c b/tests/tst-arguments5.c index c8dfdd0..aefbfdb 100644 --- a/tests/tst-arguments5.c +++ b/tests/tst-arguments5.c @@ -14,7 +14,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; if ((error = econf_readFile (&key_file, TESTSDIR"tst-arguments5-data/etc/arguments5.conf", NULL, NULL))) diff --git a/tests/tst-checkfiles.c b/tests/tst-checkfiles.c index 8c0f887..4355781 100644 --- a/tests/tst-checkfiles.c +++ b/tests/tst-checkfiles.c @@ -27,7 +27,7 @@ bool checkLink(const char *filename, const void *data) { int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; /* checking not allowed links*/ diff --git a/tests/tst-comments.c b/tests/tst-comments.c index e643768..c722563 100644 --- a/tests/tst-comments.c +++ b/tests/tst-comments.c @@ -64,7 +64,7 @@ check_comments (econf_file *key_file, const char *key, int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; int retval = 0; diff --git a/tests/tst-delimiter-comment.c b/tests/tst-delimiter-comment.c index e6a7f85..04fd16e 100644 --- a/tests/tst-delimiter-comment.c +++ b/tests/tst-delimiter-comment.c @@ -16,7 +16,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; /* no security restrictions are set */ diff --git a/tests/tst-extvalue.c b/tests/tst-extvalue.c index 5299934..6268bde 100644 --- a/tests/tst-extvalue.c +++ b/tests/tst-extvalue.c @@ -82,7 +82,7 @@ check_StringArray (econf_file *key_file, const char *group, int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; int retval = 0; diff --git a/tests/tst-filedoesnotexit1.c b/tests/tst-filedoesnotexit1.c index c1bfa4e..1c45491 100644 --- a/tests/tst-filedoesnotexit1.c +++ b/tests/tst-filedoesnotexit1.c @@ -13,7 +13,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; error = econf_readFile (&key_file, "doesnotexist1.conf", "=", "#"); diff --git a/tests/tst-getconfdirs1.c b/tests/tst-getconfdirs1.c index 8163846..ba399f0 100644 --- a/tests/tst-getconfdirs1.c +++ b/tests/tst-getconfdirs1.c @@ -51,7 +51,7 @@ check_key(econf_file *key_file, char *key, char *expected_val) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; int retval = 0; econf_err error; diff --git a/tests/tst-getconfdirs3.c b/tests/tst-getconfdirs3.c index 8f56341..1ad4d49 100644 --- a/tests/tst-getconfdirs3.c +++ b/tests/tst-getconfdirs3.c @@ -50,7 +50,7 @@ check_key(econf_file *key_file, char *key, char *expected_val) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; int retval = 0; econf_err error; diff --git a/tests/tst-getconfdirs4.c b/tests/tst-getconfdirs4.c index 4c8241f..0f07cc3 100644 --- a/tests/tst-getconfdirs4.c +++ b/tests/tst-getconfdirs4.c @@ -50,7 +50,7 @@ check_key(econf_file *key_file, char *key, char *expected_val) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; int retval = 0; econf_err error; diff --git a/tests/tst-getconfdirs5.c b/tests/tst-getconfdirs5.c index a513420..89ebb14 100644 --- a/tests/tst-getconfdirs5.c +++ b/tests/tst-getconfdirs5.c @@ -48,7 +48,7 @@ check_key(econf_file *key_file, char *key, char *expected_val) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; int retval = 0; econf_err error; diff --git a/tests/tst-getconfdirs6.c b/tests/tst-getconfdirs6.c index 331027a..9c83ac4 100644 --- a/tests/tst-getconfdirs6.c +++ b/tests/tst-getconfdirs6.c @@ -51,7 +51,7 @@ check_key(econf_file *key_file, char *key, char *expected_val) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; int retval = 0; econf_err error; diff --git a/tests/tst-getconfdirs7.c b/tests/tst-getconfdirs7.c index ea984bb..3b7c157 100644 --- a/tests/tst-getconfdirs7.c +++ b/tests/tst-getconfdirs7.c @@ -48,7 +48,7 @@ check_key(econf_file *key_file, char *key, char *expected_val) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; int retval = 0; econf_err error; diff --git a/tests/tst-getconfdirs8.c b/tests/tst-getconfdirs8.c index d91ace7..2c71770 100644 --- a/tests/tst-getconfdirs8.c +++ b/tests/tst-getconfdirs8.c @@ -54,7 +54,7 @@ check_key(econf_file *key_file, char *key, char *expected_val) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; int retval = 0; econf_err error; diff --git a/tests/tst-getconfdirs9.c b/tests/tst-getconfdirs9.c index 462edb7..2e539ff 100644 --- a/tests/tst-getconfdirs9.c +++ b/tests/tst-getconfdirs9.c @@ -47,7 +47,7 @@ check_key(econf_file *key_file, char *key, char *expected_val) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; int retval = 0; econf_err error; diff --git a/tests/tst-getpath.c b/tests/tst-getpath.c index dd472bd..d9cc009 100644 --- a/tests/tst-getpath.c +++ b/tests/tst-getpath.c @@ -17,7 +17,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; char *path; diff --git a/tests/tst-groups1.c b/tests/tst-groups1.c index 62ee0b6..39b6dcd 100644 --- a/tests/tst-groups1.c +++ b/tests/tst-groups1.c @@ -64,7 +64,7 @@ check_String (econf_file *key_file, const char *value, int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; int retval = 0; diff --git a/tests/tst-groups2.c b/tests/tst-groups2.c index d6b4feb..faef0b2 100644 --- a/tests/tst-groups2.c +++ b/tests/tst-groups2.c @@ -15,7 +15,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; char **groups; size_t group_number; const char *group = "towel"; diff --git a/tests/tst-groups3.c b/tests/tst-groups3.c index 5641560..4ba086a 100644 --- a/tests/tst-groups3.c +++ b/tests/tst-groups3.c @@ -14,7 +14,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; char **groups; size_t group_number; char *val; diff --git a/tests/tst-groups4.c b/tests/tst-groups4.c index 266d6e6..24c5c5a 100644 --- a/tests/tst-groups4.c +++ b/tests/tst-groups4.c @@ -15,7 +15,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; const char *group = "towel"; const char *key = "42"; const char *val = "The answer, not question"; diff --git a/tests/tst-groups5.c b/tests/tst-groups5.c index af9198b..ceac2ca 100644 --- a/tests/tst-groups5.c +++ b/tests/tst-groups5.c @@ -14,7 +14,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; char **groups; size_t group_number; econf_err error; diff --git a/tests/tst-groups6.c b/tests/tst-groups6.c index 3d6eaf4..7e0be7f 100644 --- a/tests/tst-groups6.c +++ b/tests/tst-groups6.c @@ -45,7 +45,7 @@ check_key(econf_file *key_file, char *group, char *key, char *expected_val) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; char **groups; size_t group_number; econf_err error; diff --git a/tests/tst-logindefs1.c b/tests/tst-logindefs1.c index 92be038..d40fc1d 100644 --- a/tests/tst-logindefs1.c +++ b/tests/tst-logindefs1.c @@ -14,7 +14,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; char **keys; size_t key_number; char *val; diff --git a/tests/tst-logindefs2.c b/tests/tst-logindefs2.c index f366abf..8d68d30 100644 --- a/tests/tst-logindefs2.c +++ b/tests/tst-logindefs2.c @@ -16,7 +16,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; char **keys; size_t key_number; econf_err error; diff --git a/tests/tst-long-name.c b/tests/tst-long-name.c index eed30e4..190d35e 100644 --- a/tests/tst-long-name.c +++ b/tests/tst-long-name.c @@ -15,7 +15,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; int retval = 0; char **keys = NULL; diff --git a/tests/tst-man-dir.c b/tests/tst-man-dir.c index 080c202..dbaa8f5 100644 --- a/tests/tst-man-dir.c +++ b/tests/tst-man-dir.c @@ -44,7 +44,7 @@ check_key(econf_file *key_file, char *key, char *expected_val) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; int retval = 0; econf_err error; diff --git a/tests/tst-merge1.c b/tests/tst-merge1.c index beffd3f..ed66c7e 100644 --- a/tests/tst-merge1.c +++ b/tests/tst-merge1.c @@ -15,7 +15,7 @@ int main(void) { - econf_file *key_file_1 = (econf_file *)-1, *key_file_2 = (econf_file *)-1, *key_file_m = (econf_file *)-1; + econf_file *key_file_1 = NULL, *key_file_2 = NULL, *key_file_m = NULL; econf_err error; error = econf_readFile (&key_file_1, TESTSDIR"tst-merge1-data/etc/tst-merge1.conf", "=", "#"); diff --git a/tests/tst-merge2.c b/tests/tst-merge2.c index b71ede5..965a276 100644 --- a/tests/tst-merge2.c +++ b/tests/tst-merge2.c @@ -15,7 +15,7 @@ int main(void) { - econf_file *key_file_1 = (econf_file *)-1, *key_file_2 = (econf_file *)-1, *key_file_m = (econf_file *)-1; + econf_file *key_file_1 = NULL, *key_file_2 = NULL, *key_file_m = NULL; econf_err error; error = econf_readFile (&key_file_1, TESTSDIR"tst-merge2-data/etc/tst-merge2.conf", "=", "#"); diff --git a/tests/tst-merge3.c b/tests/tst-merge3.c index 77ecd0d..0c7aad2 100644 --- a/tests/tst-merge3.c +++ b/tests/tst-merge3.c @@ -15,7 +15,7 @@ int main(void) { - econf_file *key_file_1 = (econf_file *)-1, *key_file_2 = (econf_file *)-1, *key_file_m = (econf_file *)-1; + econf_file *key_file_1 = NULL, *key_file_2 = NULL, *key_file_m = NULL; econf_err error; error = econf_readFile (&key_file_1, TESTSDIR"tst-merge3-data/etc/login.defs", " \t=", "#"); diff --git a/tests/tst-merge4.c b/tests/tst-merge4.c index a5bf24c..5184ad2 100644 --- a/tests/tst-merge4.c +++ b/tests/tst-merge4.c @@ -41,7 +41,7 @@ check_string (econf_file *key_file, const char *group, const char *key, int main(void) { - econf_file *key_file_1 = (econf_file *)-1, *key_file_2 = (econf_file *)-1, *key_file_m = (econf_file *)-1; + econf_file *key_file_1 = NULL, *key_file_2 = NULL, *key_file_m = NULL; econf_err error; int retval = 0; diff --git a/tests/tst-merge5.c b/tests/tst-merge5.c index f51124d..97305ea 100644 --- a/tests/tst-merge5.c +++ b/tests/tst-merge5.c @@ -41,7 +41,7 @@ check_string (econf_file *key_file, const char *group, const char *key, int main(void) { - econf_file *key_file_1 = (econf_file *)-1, *key_file_2 = (econf_file *)-1, *key_file_m = (econf_file *)-1; + econf_file *key_file_1 = NULL, *key_file_2 = NULL, *key_file_m = NULL; econf_err error; int retval = 0; diff --git a/tests/tst-options.c b/tests/tst-options.c index 673f079..eee7ea4 100644 --- a/tests/tst-options.c +++ b/tests/tst-options.c @@ -15,7 +15,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; int retval = 0; econf_err error; diff --git a/tests/tst-parse-error.c b/tests/tst-parse-error.c index db6638e..42c7c04 100644 --- a/tests/tst-parse-error.c +++ b/tests/tst-parse-error.c @@ -16,7 +16,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; char *filename = NULL; uint64_t line_nr = 0; diff --git a/tests/tst-parseconfig1.c b/tests/tst-parseconfig1.c index 85e81bb..bcc051f 100644 --- a/tests/tst-parseconfig1.c +++ b/tests/tst-parseconfig1.c @@ -14,7 +14,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; if ((error = econf_readFile (&key_file, TESTSDIR"tst-parseconfig-data/empty-group.conf", "=", "#"))) diff --git a/tests/tst-python.c b/tests/tst-python.c index 348f66e..f4a07cb 100644 --- a/tests/tst-python.c +++ b/tests/tst-python.c @@ -82,7 +82,7 @@ check (econf_file *key_file, const char *key, int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; int retval = 0; diff --git a/tests/tst-quote1.c b/tests/tst-quote1.c index 4de7481..06c1100 100644 --- a/tests/tst-quote1.c +++ b/tests/tst-quote1.c @@ -48,7 +48,7 @@ check_String (econf_file *key_file, const char *group, int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; const char *group="quote"; econf_err error; int retval = 0; diff --git a/tests/tst-security.c b/tests/tst-security.c index 26da5b2..6c38e52 100644 --- a/tests/tst-security.c +++ b/tests/tst-security.c @@ -18,7 +18,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; /* no security restrictions are set */ diff --git a/tests/tst-setgetvalues1.c b/tests/tst-setgetvalues1.c index f2d6a32..ad92fb0 100644 --- a/tests/tst-setgetvalues1.c +++ b/tests/tst-setgetvalues1.c @@ -105,7 +105,7 @@ check_Bool (econf_file *key_file, const char *value, bool expect) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; int retval = 0; diff --git a/tests/tst-shells1.c b/tests/tst-shells1.c index 6b6dca4..4729b97 100644 --- a/tests/tst-shells1.c +++ b/tests/tst-shells1.c @@ -15,7 +15,7 @@ int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; char **keys; size_t key_number; char *val; @@ -108,7 +108,7 @@ main(void) econf_writeFile(key_file, TESTSDIR"tst-shells1-data/", "out.ini"); // And reading it again - econf_file *key_compare = (econf_file *)-1; + econf_file *key_compare = NULL; error = econf_readFile(&key_compare, TESTSDIR"tst-shells1-data/out.ini", "", "#"); if (error || key_compare == NULL) { diff --git a/tests/tst-shells2.c b/tests/tst-shells2.c index 6ed863c..406e64c 100644 --- a/tests/tst-shells2.c +++ b/tests/tst-shells2.c @@ -50,7 +50,7 @@ check_shell(econf_file *key_file, char *shell, int expected) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; int retval = 0; econf_err error; char **keys; diff --git a/tests/tst-string.c b/tests/tst-string.c index 7a8ceba..065939b 100644 --- a/tests/tst-string.c +++ b/tests/tst-string.c @@ -48,7 +48,7 @@ check_String (econf_file *key_file, const char *group, int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; int retval = 0; diff --git a/tests/tst-without-suffix.c b/tests/tst-without-suffix.c index 8ce413a..05c25c5 100644 --- a/tests/tst-without-suffix.c +++ b/tests/tst-without-suffix.c @@ -47,7 +47,7 @@ check_key(econf_file *key_file, char *key, char *expected_val) int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; int retval = 0; econf_err error; diff --git a/tests/tst-write-comments.c b/tests/tst-write-comments.c index 728ea7c..0707a42 100644 --- a/tests/tst-write-comments.c +++ b/tests/tst-write-comments.c @@ -63,7 +63,7 @@ check_comments (econf_file *key_file, const char *key, int main(void) { - econf_file *key_file = (econf_file *)-1; + econf_file *key_file = NULL; econf_err error; int retval = 0; @@ -90,7 +90,7 @@ main(void) econf_writeFile(key_file, TESTSDIR"tst-write-comments/", "out.ini"); // And reading it again - econf_file *key_compare = (econf_file *)-1; + econf_file *key_compare = NULL; error = econf_readFile(&key_compare, TESTSDIR"tst-write-comments/out.ini", "=", "#"); if (error || key_compare == NULL) { diff --git a/util/econftool.c b/util/econftool.c index 9fff745..07ef3aa 100644 --- a/util/econftool.c +++ b/util/econftool.c @@ -40,13 +40,13 @@ static const char *utilname = "econftool"; static bool non_interactive = false; static char *conf_suffix = NULL; /* the suffix of the filename e.g. .conf */ -static char conf_dir[PATH_MAX] = {0}; /* the directory of the config file */ -static char conf_basename[PATH_MAX] = {0}; /* the filename without the suffix */ -static char conf_filename[PATH_MAX] = {0}; /* the filename including the suffix */ -static char conf_path[PATH_MAX] = {0}; /* the path concatenated with the filename and the suffix */ +static char *conf_dir = NULL; /* the directory of the config file */ +static char *conf_basename = NULL; /* the filename without the suffix */ +static char *conf_filename = NULL; /* the filename including the suffix */ +static char *conf_path = NULL; /* the path concatenated with the filename and the suffix */ static char *xdg_config_dir = NULL; -static char root_dir[PATH_MAX] = "/etc"; -static char usr_root_dir[PATH_MAX] = "/usr/etc"; +static char *root_dir = NULL; +static char *usr_root_dir = NULL; /** * @brief Shows the usage. @@ -112,26 +112,32 @@ static char *replace_str(char *str, char *orig, char *rep) * * @param change_path the path to be changed */ -static void change_root_dir(char change_path[PATH_MAX]) +static void change_root_dir(char **change_path) { if (getenv("ECONFTOOL_ROOT") != NULL) { int strlen_econftool_root = strlen(getenv("ECONFTOOL_ROOT")); /* check if ECONFTOOL_ROOT has already been added */ - if (strncmp(change_path, getenv("ECONFTOOL_ROOT"), strlen_econftool_root) == 0) + if (*change_path != NULL && + strncmp(*change_path, getenv("ECONFTOOL_ROOT"), strlen_econftool_root) == 0) return; - if ((strlen(change_path) + strlen_econftool_root) >= PATH_MAX) { - fprintf(stderr, "ECONFTOOL_ROOT + change_path is too long\n"); - exit(EXIT_FAILURE); - } - - char *tmp = strdup(change_path); - - strcpy(change_path, getenv("ECONFTOOL_ROOT")); - strcat(change_path, tmp); + if (*change_path != NULL) { + char *tmp = strdup(*change_path); + free(*change_path); + if (asprintf(change_path, "%s%s", getenv("ECONFTOOL_ROOT"), tmp) < 0) { + fprintf(stderr, "Out of memory!\n"); + exit(EXIT_FAILURE); + } - free(tmp); + free(tmp); + } else { + free(*change_path); + if (asprintf(change_path, "%s", getenv("ECONFTOOL_ROOT")) < 0) { + fprintf(stderr, "Out of memory!\n"); + exit(EXIT_FAILURE); + } + } } } @@ -150,7 +156,7 @@ static int nftw_remove(const char *path, const struct stat *sb, int flag, struct /** * @brief Generate a tmpfile using mkstemp() and write its name into tmp_name */ -static int generate_tmp_file(char *tmp_name) +static int generate_tmp_file(char **tmp_name) { int filedes; char tmp_filename[] = "/tmp/econftool-XXXXXX"; @@ -160,7 +166,11 @@ static int generate_tmp_file(char *tmp_name) return -1; } - strcpy(tmp_name, tmp_filename); + *tmp_name = strdup(tmp_filename); + if (*tmp_name == NULL) { + perror("out of memory!"); + return -1; + } return 0; } @@ -346,17 +356,26 @@ static int econf_edit_editor(struct econf_file **key_file_edit, struct econf_fil econf_err econf_error; int wstatus; char *editor; - char tmpfile_edit[FILENAME_MAX]; - char path_tmpfile_edit[PATH_MAX]; - char tmp_name[PATH_MAX]; + char *tmpfile_edit = NULL; + char *path_tmpfile_edit = NULL; + char *tmp_name = NULL; char *tmp_path = getenv("TMPDIR"); if (tmp_path == NULL) tmp_path = "/tmp"; - if (generate_tmp_file(tmp_name)) + if (generate_tmp_file(&tmp_name)) return -1; - snprintf(path_tmpfile_edit, sizeof(path_tmpfile_edit), "%s", tmp_name); - snprintf(tmpfile_edit, sizeof(tmpfile_edit), "%s", strrchr(path_tmpfile_edit, '/') + 1); + + path_tmpfile_edit = strdup(tmp_name); + if (path_tmpfile_edit == NULL) { + fprintf(stderr, "Out of memory!\n"); + return -1; + } + tmpfile_edit = strdup(strrchr(path_tmpfile_edit, '/') + 1); + if (tmpfile_edit == NULL) { + fprintf(stderr, "Out of memory!\n"); + return -1; + } editor = getenv("EDITOR"); if (editor == NULL) { @@ -537,18 +556,17 @@ static int econf_revert(bool is_root, bool use_homedir) char input[3] = ""; int status = 0; + free(conf_path); if (!is_root || use_homedir) { - int ret = snprintf(conf_path, sizeof(conf_path), "%s/%s", xdg_config_dir, conf_filename); - if (ret < 0 || ret >= (int)(sizeof(conf_path))) { /* check truncation */ - fprintf(stderr, "Filename too long\n"); - return -1; - } + if (asprintf(&conf_path, "%s/%s", xdg_config_dir, conf_filename) < 0) { + fprintf(stderr, "Out of memory!\n"); + return -1; + } } else { - int ret = snprintf(conf_path, sizeof(conf_path), "/etc/%s", conf_filename); - if (ret < 0 || ret >= (int)(sizeof(conf_path))) { /* check truncation */ - fprintf(stderr, "Filename too long\n"); - return -1; - } + if (asprintf(&conf_path, "/etc/%s", conf_filename) < 0) { + fprintf(stderr, "Out of memory!\n"); + return -1; + } } if (access(conf_path, F_OK) == -1 && (access(conf_dir, F_OK) == -1 || !is_root)) { @@ -609,7 +627,7 @@ int main (int argc, char *argv[]) { static const char *dropin_filename = "90_econftool.conf"; static econf_file *key_file = NULL; - char home_dir[PATH_MAX] = {0}; /* the path of the home directory */ + char *home_dir = NULL; /* the path of the home directory */ bool is_dropin_file = true; bool is_root = false; bool use_homedir = false; @@ -631,12 +649,21 @@ int main (int argc, char *argv[]) {0, 0, 0, 0 } }; + root_dir = strdup("/etc"); + usr_root_dir = strdup("/usr/etc"); + conf_dir = strdup(""); + conf_basename = strdup(""); + while ((opt = getopt_long(argc, argv, "hfyuc:d:", longopts, &index)) != -1) { switch(opt) { case 'f': /* overwrite path */ - snprintf(conf_dir, sizeof(conf_dir), "%s", "/etc"); - change_root_dir(conf_dir); + free(conf_dir); + if (asprintf(&conf_dir, "%s", "/etc") < 0) { + fprintf(stderr, "Out of memory!\n"); + exit(EXIT_FAILURE); + } + change_root_dir(&conf_dir); is_dropin_file = false; break; case 'h': @@ -701,40 +728,57 @@ int main (int argc, char *argv[]) exit(1); } else { /* set filename to the proper argv argument */ - int ret = snprintf(conf_basename, strlen(argv[optind + 1]) - strlen(conf_suffix) + 1, "%s", argv[optind + 1]); - if (ret < 0) { /* Do not check truncation because it is intent */ - fprintf(stderr, "snprintf general error\n"); + free(conf_basename); + conf_basename = strndup(argv[optind + 1], strlen(argv[optind + 1]) - strlen(conf_suffix)); + if (conf_basename == NULL) { + fprintf(stderr, "Out of memory!\n"); return EXIT_FAILURE; } } } - snprintf(conf_filename, sizeof(conf_filename), "%s" , argv[optind + 1]); + conf_filename = strdup(argv[optind + 1]); + if (conf_filename == NULL) { + fprintf(stderr, "Out of memory!\n"); + exit(EXIT_FAILURE); + } if (is_dropin_file) { - int ret = snprintf(conf_dir, sizeof(conf_dir), "/etc/%s.d", conf_filename); - if (ret < 0 || ret >= (int)(sizeof(conf_dir))) { /* check truncation */ - fprintf(stderr, "Filename too long\n"); - return EXIT_FAILURE; - } - change_root_dir(conf_dir); + free(conf_dir); + if (asprintf(&conf_dir, "/etc/%s.d", conf_filename) < 0) { + fprintf(stderr, "Out of memory!\n"); + exit(EXIT_FAILURE); + } + change_root_dir(&conf_dir); } - int ret_snprintf = snprintf(conf_path, sizeof(conf_path), "%s/%s", conf_dir, conf_filename); - if (ret_snprintf < 0 || ret_snprintf >= (int)(sizeof(conf_path))) { /* check truncation */ - fprintf(stderr, "Filename too long\n"); - return EXIT_FAILURE; + + free(conf_path); + if (asprintf(&conf_path, "%s/%s", conf_dir, conf_filename) < 0) { + fprintf(stderr, "Out of memory!\n"); + return EXIT_FAILURE; } if (getenv("ECONFTOOL_ROOT") == NULL) if (getenv("HOME") != NULL) { - snprintf(home_dir, sizeof(home_dir), "%s", getenv("HOME")); + free(home_dir); + home_dir = strdup(getenv("HOME")); + if (home_dir == NULL) { + fprintf(stderr, "Out of memory!\n"); + return EXIT_FAILURE; + } } else { struct passwd *pw = getpwuid(getuid()); - if(pw) - strcpy(home_dir, pw->pw_dir); + if(pw) { + free (home_dir); + home_dir = strdup(pw->pw_dir); + if (home_dir == NULL) { + fprintf(stderr, "Out of memory!\n"); + return EXIT_FAILURE; + } + } } else - change_root_dir(home_dir); + change_root_dir(&home_dir); if (getenv("XDG_CONFIG_HOME") == NULL) { /* if no XDG_CONFIG_HOME is specified take ~/.config as default */ @@ -750,12 +794,13 @@ int main (int argc, char *argv[]) } } + free(home_dir); + /* Change Root dirs */ - change_root_dir(root_dir); - change_root_dir(usr_root_dir); + change_root_dir(&root_dir); + change_root_dir(&usr_root_dir); int ret = 0; - if (strcmp(argv[optind], "show") == 0) { ret = econf_read(&key_file, delimiters, comment, true); } else if (strcmp(argv[optind], "syntax") == 0) { @@ -763,23 +808,31 @@ int main (int argc, char *argv[]) } else if (strcmp(argv[optind], "edit") == 0) { if (!is_root || use_homedir) { /* adjust path to home directory of the user.*/ - snprintf(conf_dir, sizeof(conf_dir), "%s", xdg_config_dir); - change_root_dir(conf_dir); - ret_snprintf = snprintf(conf_path, sizeof(conf_path), "%s/%s", conf_dir, - conf_filename); - if (ret_snprintf < 0 || ret_snprintf >= (int)(sizeof(conf_path))) { /* check truncation */ - fprintf(stderr, "Filename too long\n"); - return EXIT_FAILURE; + free(conf_dir); + conf_dir = strdup(xdg_config_dir); + if (conf_dir == NULL) { + fprintf(stderr, "Out of memory!\n"); + exit(EXIT_FAILURE); + } + change_root_dir(&conf_dir); + free(conf_path); + if (asprintf(&conf_path, "%s/%s", conf_dir, conf_filename) < 0) { + fprintf(stderr, "Out of memory!\n"); + return EXIT_FAILURE; } } else if(is_dropin_file) { - snprintf(conf_filename, sizeof(conf_filename), "%s", dropin_filename); - ret_snprintf = snprintf(conf_path, sizeof(conf_path), "%s/%s", conf_dir, - conf_filename); - if (ret_snprintf < 0 || ret_snprintf >= (int)(sizeof(conf_path))) { /* check truncation */ - fprintf(stderr, "Filename too long\n"); - return EXIT_FAILURE; + free(conf_filename); + conf_filename = strdup(dropin_filename); + if (conf_filename == NULL) { + fprintf(stderr, "Out of memory!\n"); + exit(EXIT_FAILURE); } - } + free(conf_path); + if (asprintf(&conf_path, "%s/%s", conf_dir, conf_filename) < 0) { + fprintf(stderr, "Out of memory!\n"); + return EXIT_FAILURE; + } + } ret = econf_edit(&key_file, delimiters, comment); } else if (strcmp(argv[optind], "revert") == 0) {