From 22d42e2cb4dced153aa8ee785ea2a102962a31b4 Mon Sep 17 00:00:00 2001 From: Karl Koch Date: Fri, 5 Jun 2020 17:44:02 +0200 Subject: [PATCH] When env variable SCRIPTS_DIR is unset the code reads or writes from a path NULL. I suggest to fix this and use a default instead. Issue-ID: SIM-29 Signed-off-by: Karl Koch Change-Id: Icf890aa0a510373f0513f57eeb7e06b9d8c8d0f5 --- ntsimulator/src/utils/utils.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/ntsimulator/src/utils/utils.c b/ntsimulator/src/utils/utils.c index d98dac0..5242f6d 100644 --- a/ntsimulator/src/utils/utils.c +++ b/ntsimulator/src/utils/utils.c @@ -410,7 +410,26 @@ char* readConfigFileInString(void) char * buffer = 0; long length; char config_file[200]; - sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR")); + + //sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR")); + + // --> recommended fix, Karl Koch, Deutsche Telekom AG, 22. 5. 2020. + // Path to config_file contains NULL when env variable is unset. + char *scripts_dir = getenv("SCRIPTS_DIR"); + char *scripts_dir_default = "/opt/dev/ntsimulator/scripts"; + + if(NULL != scripts_dir) + { + sprintf(config_file, "%s/configuration.json", scripts_dir); + } + else + { + sprintf(config_file, "%s/configuration.json", scripts_dir_default); + printf("warning: opening config file in default path: <%s>\n", + config_file); + } + // end of fix <-- + FILE * f = fopen (config_file, "rb"); if (f) @@ -438,7 +457,26 @@ char* readConfigFileInString(void) void writeConfigFile(char *config) { char config_file[200]; - sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR")); + + //sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR")); + + // --> recommended fix, Karl Koch, Deutsche Telekom AG, 22. 5. 2020. + // Path to config_file contains NULL when env variable is unset. + char *scripts_dir = getenv("SCRIPTS_DIR"); + char *scripts_dir_default = "/opt/dev/ntsimulator/scripts"; + + if(NULL != scripts_dir) + { + sprintf(config_file, "%s/configuration.json", scripts_dir); + } + else + { + sprintf(config_file, "%s/configuration.json", scripts_dir_default); + printf("warning: opening config file in default path: <%s>\n", + config_file); + } + // end of fix <-- + FILE * f = fopen (config_file, "w"); if (f) -- 2.16.6