From 786f4ac370a920aa5f51ee926914ec5085e767c3 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Thu, 9 Apr 2020 20:27:45 +0200 Subject: [PATCH] WIP: No more segfaults --- inc/ini/ini.h | 4 ++-- inc/lektor/config.h | 2 +- src/config.c | 57 +++++++++++++++++++-------------------------- src/net/listen.c | 4 ++-- 4 files changed, 29 insertions(+), 38 deletions(-) diff --git a/inc/ini/ini.h b/inc/ini/ini.h index c95bf778..646a145e 100644 --- a/inc/ini/ini.h +++ b/inc/ini/ini.h @@ -73,7 +73,7 @@ int ini_parse_string(const char *string, ini_handler handler, void *user); configparser. If allowed, ini_parse() will call the handler with the same name for each subsequent line parsed. */ #ifndef INI_ALLOW_MULTILINE -#define INI_ALLOW_MULTILINE 1 +#define INI_ALLOW_MULTILINE 0 #endif /* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of @@ -131,7 +131,7 @@ int ini_parse_string(const char *string, ini_handler handler, void *user); name and value NULL). Default is to only call the handler on each name=value pair. */ #ifndef INI_CALL_HANDLER_ON_NEW_SECTION -#define INI_CALL_HANDLER_ON_NEW_SECTION 1 +#define INI_CALL_HANDLER_ON_NEW_SECTION 0 #endif /* Nonzero to allow a name without a value (no '=' or ':' on the line) and diff --git a/inc/lektor/config.h b/inc/lektor/config.h index d253fcc8..56c28206 100644 --- a/inc/lektor/config.h +++ b/inc/lektor/config.h @@ -60,7 +60,7 @@ int config_open(sqlite3 *db); /* Get the path to the config file that may be red, taking into account the priority between existing files. The returned path is a path to an existing file. If no file is found, returns a non zero value. Returns 1 otherwise. */ -int config_detect_file(char **conf, size_t conf_len); +int config_detect_file(char *conf, size_t conf_len); /* Create and read the configuration in the conf file and write it into lkt_conf. The type is opaque because it's a C++ class. diff --git a/src/config.c b/src/config.c index 4a4771c0..37498d26 100644 --- a/src/config.c +++ b/src/config.c @@ -109,7 +109,7 @@ handler(void *user, const char *section, const char *name, const char *value) } int -config_detect_file(char **conf, size_t conf_len) +config_detect_file(char *conf, size_t conf_len) { bool is_malloc = false; struct passwd *pw = getpwuid(getuid()); @@ -118,20 +118,13 @@ config_detect_file(char **conf, size_t conf_len) if (conf == NULL) return 1; - if (*conf == NULL) { - *conf = (char *) calloc(conf_len, sizeof(char)); - - if (!*conf) - return ENOMEM; - } - - memset(*conf, 0, conf_len * sizeof(char)); + memset(conf, 0, conf_len * sizeof(char)); /* Try the current working dir config file. */ - if (getcwd(*conf, conf_len - 1) != NULL) { - strncat(*conf, "/lektor.ini", conf_len - 1); - fprintf(stderr, " . config_detect_file: trying %s\n", *conf); - if (!access(*conf, R_OK)) + if (getcwd(conf, conf_len - 1) != NULL) { + strncat(conf, "/lektor.ini", conf_len - 1); + fprintf(stderr, " . config_detect_file: trying %s\n", conf); + if (!access(conf, R_OK)) goto found; } @@ -143,39 +136,39 @@ config_detect_file(char **conf, size_t conf_len) home = pw->pw_dir; if (!home || (strlen(home) >= conf_len)) goto no_config_directory; - memcpy(*conf, home, (strlen(home) + 1) * sizeof(char)); - strncat(*conf, "/.config/lektor/lektor.ini", conf_len - 1); - fprintf(stderr, " . config_detect_file: trying %s\n", *conf); - if (!access(*conf, R_OK | F_OK)) + memcpy(conf, home, (strlen(home) + 1) * sizeof(char)); + strncat(conf, "/.config/lektor/lektor.ini", conf_len - 1); + fprintf(stderr, " . config_detect_file: trying %s\n", conf); + if (!access(conf, R_OK | F_OK)) goto found; no_config_directory: /* Try the '/opt/lektor' file. */ - memcpy(*conf, "/opt/lektor/lektor.ini", sizeof("/opt/lektor/lektor.ini")); - fprintf(stderr, " . config_detect_file: trying %s\n", *conf); - if (!access(*conf, R_OK)) + memcpy(conf, "/opt/lektor/lektor.ini", sizeof("/opt/lektor/lektor.ini")); + fprintf(stderr, " . config_detect_file: trying %s\n", conf); + if (!access(conf, R_OK)) goto found; /* Try the '/usr/local/etc/lektor.ini' file. */ - memcpy(*conf, "/usr/local/etc/lektor.ini", sizeof("/opt/lektor/lektor.ini")); - fprintf(stderr, " . config_detect_file: trying %s\n", *conf); - if (!access(*conf, R_OK)) + memcpy(conf, "/usr/local/etc/lektor.ini", sizeof("/opt/lektor/lektor.ini")); + fprintf(stderr, " . config_detect_file: trying %s\n", conf); + if (!access(conf, R_OK)) goto found; /* Try the '/etc/lektor.ini' file. */ - memcpy(*conf, "/etc/lektor.ini", sizeof("/etc/lektor.ini")); - fprintf(stderr, " . config_detect_file: trying %s\n", *conf); - if (!access(*conf, R_OK)) + memcpy(conf, "/etc/lektor.ini", sizeof("/etc/lektor.ini")); + fprintf(stderr, " . config_detect_file: trying %s\n", conf); + if (!access(conf, R_OK)) goto found; /* Error */ fprintf(stderr, "config_detect_file: an error occured with file %s: %s", - *conf, strerror(errno)); + conf, strerror(errno)); if (is_malloc) - free(*conf); + free(conf); return 1; found: - fprintf(stderr, " . config_detect_file: using file %s\n", *conf); + fprintf(stderr, " . config_detect_file: using file %s\n", conf); return 0; } @@ -198,10 +191,10 @@ config_new(sqlite3 *db, const char *conf) int config_open(sqlite3 *db) { - char *conf_file = NULL; + char conf_file[PATH_MAX]; int ret = 1; - if (config_detect_file(&conf_file, PATH_MAX)) { + if (config_detect_file(conf_file, PATH_MAX)) { fprintf(stderr, " ! config_open: error while searching for a config file\n"); goto error; } @@ -213,7 +206,5 @@ config_open(sqlite3 *db) ret = 0; error: - if (conf_file) - free(conf_file); return ret; } diff --git a/src/net/listen.c b/src/net/listen.c index 71072802..c51a2f3f 100644 --- a/src/net/listen.c +++ b/src/net/listen.c @@ -716,7 +716,7 @@ lkt_listen(void) char *host = (char *) calloc(HOST_NAME_MAX, sizeof(char)); char port[7]; /* Maximal port number is 65535, +2 for '\n' and '\0' */ char player_mod[INI_MAX_LINE]; - char *conf_file; + char conf_file[PATH_MAX]; memset(&srv, 0, sizeof(struct lkt_state)); /* Initialize the system. */ @@ -726,7 +726,7 @@ lkt_listen(void) return 1; } - if (config_detect_file(&conf_file, PATH_MAX)) { + if (config_detect_file(conf_file, PATH_MAX)) { fprintf(stderr, " ! error while searching for a config file\n"); return 1; } -- GitLab