From e1192952036766cf01d4f0df33de06bf3d56fcbb Mon Sep 17 00:00:00 2001 From: Marco Aurelio da Costa Date: Tue, 16 Jan 2024 08:46:07 -0300 Subject: [PATCH] Fix memory leaks The field grib_samples_path from grib_context was being left in an inconsistent state, either with static memory or with heap allocated memory, depending on the existence of the env var ECCODES_SAMPLES_PATH and the preprocessor variable ECCODES_SAMPLES_PATH. Free the memory allocated for grib_samples_path and grib_definition_files_path when setting them from the API calls. --- src/grib_context.cc | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/grib_context.cc b/src/grib_context.cc index d77fd2c06..76ae03748 100644 --- a/src/grib_context.cc +++ b/src/grib_context.cc @@ -451,10 +451,16 @@ grib_context* grib_context_get_default() default_grib_context.log_stream = stdout; } + if (!default_grib_context.grib_samples_path) { #ifdef ECCODES_SAMPLES_PATH - if (!default_grib_context.grib_samples_path) - default_grib_context.grib_samples_path = ECCODES_SAMPLES_PATH; + default_grib_context.grib_samples_path = strdup(ECCODES_SAMPLES_PATH); +#else + default_grib_context.grib_samples_path = strdup(""); #endif + } + else { + default_grib_context.grib_samples_path = strdup(default_grib_context.grib_samples_path); + } default_grib_context.grib_definition_files_path = codes_getenv("ECCODES_DEFINITION_PATH"); #ifdef ECCODES_DEFINITION_PATH @@ -483,11 +489,12 @@ grib_context* grib_context_get_default() } if (test_samp) { char buffer[ECC_PATH_MAXLEN]= {0,}; - if (default_grib_context.grib_samples_path) { + if (default_grib_context.grib_samples_path[0]) { strcpy(buffer, default_grib_context.grib_samples_path); strcat(buffer, ":"); } strcat(buffer, test_samp); + free(default_grib_context.grib_samples_path); default_grib_context.grib_samples_path = strdup(buffer); } } @@ -519,9 +526,16 @@ grib_context* grib_context_get_default() { const char* samples_extra = getenv("ECCODES_EXTRA_SAMPLES_PATH"); if (samples_extra) { - char buffer[ECC_PATH_MAXLEN]; - snprintf(buffer, ECC_PATH_MAXLEN, "%s%c%s", samples_extra, ECC_PATH_DELIMITER_CHAR, default_grib_context.grib_samples_path); - default_grib_context.grib_samples_path = strdup(buffer); + if(default_grib_context.grib_samples_path[0]) { + char buffer[ECC_PATH_MAXLEN]; + snprintf(buffer, ECC_PATH_MAXLEN, "%s%c%s", samples_extra, ECC_PATH_DELIMITER_CHAR, default_grib_context.grib_samples_path); + free(default_grib_context.grib_samples_path); + default_grib_context.grib_samples_path = strdup(buffer); + } + else { + free(default_grib_context.grib_samples_path); + default_grib_context.grib_samples_path = strdup(samples_extra); + } } } #ifdef ECCODES_SAMPLES_PATH @@ -530,6 +544,7 @@ grib_context* grib_context_get_default() char buffer[ECC_PATH_MAXLEN]; snprintf(buffer, ECC_PATH_MAXLEN, "%s%c%s", default_grib_context.grib_samples_path, ECC_PATH_DELIMITER_CHAR, ECCODES_SAMPLES_PATH); + free(default_grib_context.grib_samples_path); default_grib_context.grib_samples_path = strdup(buffer); } } @@ -885,6 +900,7 @@ void grib_context_set_definitions_path(grib_context* c, const char* path) GRIB_MUTEX_INIT_ONCE(&once, &init); GRIB_MUTEX_LOCK(&mutex_c); + free(c->grib_definition_files_path); c->grib_definition_files_path = strdup(path); grib_context_log(c, GRIB_LOG_DEBUG, "Definitions path changed to: %s", c->grib_definition_files_path); @@ -897,6 +913,7 @@ void grib_context_set_samples_path(grib_context* c, const char* path) GRIB_MUTEX_INIT_ONCE(&once, &init); GRIB_MUTEX_LOCK(&mutex_c); + free(c->grib_samples_path); c->grib_samples_path = strdup(path); grib_context_log(c, GRIB_LOG_DEBUG, "Samples path changed to: %s", c->grib_samples_path);