From 0c5cfbe2cb3d8f80c0c3406dbc8f1a5d1dabd06f Mon Sep 17 00:00:00 2001 From: Bogomil Vasilev Date: Thu, 17 Jan 2019 14:31:50 +0200 Subject: [PATCH] RMPS: improve enumtostr() and confparser --- src/confparser.c | 45 ++++++++++++++++++++++---------------------- src/enum_functions.c | 37 ++++++++++++++++++++++++------------ src/enum_functions.h | 2 +- src/main.c | 2 +- 4 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/confparser.c b/src/confparser.c index 7fc0344..e864470 100644 --- a/src/confparser.c +++ b/src/confparser.c @@ -154,37 +154,41 @@ static void init_conf(void) //conf.nfs = {0}; /* TODO */ } +static void log_and_free(char **msg, const char *config, char *mem) +{ + log(ERROR, *msg, config); + if (*msg) /* in case enumtostr() fails */ + free(*msg); + free(mem); +} + static bool test_conf_perms(char *config) { struct stat s; - char confresult[128]; + char *confresult; char *config_copy = strdup(config); int err = stat(config, &s); if (err == -1) { if (errno == ENOENT) { - enumtostr(confresult, CONF_MISSING); - log(ERROR, confresult, config); - free(config_copy); + enumtostr(&confresult, CONF_MISSING); + log_and_free(&confresult, config, config_copy); return false; } } else { if (!S_ISREG(s.st_mode)) { - enumtostr(confresult, CONF_NOTFILE); - log(ERROR, confresult, config); - free(config_copy); + enumtostr(&confresult, CONF_NOTFILE); + log_and_free(&confresult, config, config_copy); return false; } if (!(0400 & s.st_mode)) { - enumtostr(confresult, CONF_PERM); - log(ERROR, confresult, config); - free(config_copy); + enumtostr(&confresult, CONF_PERM); + log_and_free(&confresult, config, config_copy); return false; } if (access(config, R_OK) != 0) { - enumtostr(confresult, CONF_NOT_READABLE); - log(ERROR, confresult, config); - free(config_copy); + enumtostr(&confresult, CONF_NOT_READABLE); + log_and_free(&confresult, config, config_copy); return false; } } @@ -193,23 +197,20 @@ static bool test_conf_perms(char *config) if (err == -1) { if (errno == ENOENT) { - enumtostr(confresult, CONF_DIR_MISSING); - log(ERROR, confresult, config_copy); - free(config_copy); + enumtostr(&confresult, CONF_DIR_MISSING); + log_and_free(&confresult, config, config_copy); return false; } } else { if (!S_ISDIR(s.st_mode)) { - enumtostr(confresult, CONF_DIR_NOTDIR); - log(ERROR, confresult, config_copy); - free(config_copy); + enumtostr(&confresult, CONF_DIR_NOTDIR); + log_and_free(&confresult, config, config_copy); return false; } if (!(0400 & s.st_mode) || !(0100 & s.st_mode)) { - enumtostr(confresult, CONF_DIR_PERM); - log(ERROR, confresult, config_copy); - free(config_copy); + enumtostr(&confresult, CONF_DIR_PERM); + log_and_free(&confresult, config, config_copy); return false; } } diff --git a/src/enum_functions.c b/src/enum_functions.c index 21ab35a..eee35af 100644 --- a/src/enum_functions.c +++ b/src/enum_functions.c @@ -19,35 +19,48 @@ * along with RMPS. If not, see . */ +#define _GNU_SOURCE #include #include #include +#include #include "enum_functions.h" #include "log.h" -void enumtostr(char *scode, int code) +void enumtostr(char **meaning, int code) { - char line[128]; + char *line = NULL, *val_ptr; + size_t len = 0; + ssize_t line_len; FILE *fp; - int bytes = 0; + bool found = false; - snprintf(scode, 10, "%d", code); + if (asprintf(meaning, "%d", code) == -1) { + log(ERROR, "asprintf() in enumtostr() OOM!"); + free(*meaning); + return; + } fp = fopen("/usr/lib/rmps/resources/enum_codes", "r"); if (fp == NULL) { log(ERROR, "Failed to fetch error enum code!"); return; } - while (fgets(line, sizeof(line), fp) != NULL) - if (strstr(line, scode) != NULL) { - char *byte; - - bytes = sprintf(scode, "%s", line); - byte = memchr(scode, '\n', bytes); - byte[-1] = '\0'; + while ((line_len = getline(&line, &len, fp)) != -1) { + if (line[line_len-2] == '"') + line[line_len-2] = '\0'; + if ((val_ptr = strstr(line, *meaning)) != NULL) { + found = true; + val_ptr = strchr(line, '"') + 1; + asprintf(meaning, "%s", val_ptr); break; } - strncpy(scode, (char *)memchr(scode, '\"', bytes) + 1, bytes); + } + + if (!found) + asprintf(meaning, "Unknown enum_code: %d", code); + if (line) + free(line); fclose(fp); } diff --git a/src/enum_functions.h b/src/enum_functions.h index 2b62d95..c74b22a 100644 --- a/src/enum_functions.h +++ b/src/enum_functions.h @@ -41,6 +41,6 @@ enum WARN_CODES { CONF_FILE_PERM_INSECURE, }; -extern void enumtostr(char *scode, int code); +extern void enumtostr(char **meaning, int code); #endif /* ENUM_FUNCTIONS_H */ diff --git a/src/main.c b/src/main.c index 67e51cf..026a6fd 100644 --- a/src/main.c +++ b/src/main.c @@ -106,7 +106,7 @@ int main(int argc, char *argv[]) config = calloc(sizeof(char), path_size); if (config == NULL) { - fprintf(stderr, "malloc() failed for config\n"); + fprintf(stderr, "calloc() failed for config\n"); exit(EXIT_FAILURE); } memcpy(config, path, path_size);