Merge pull request #51 from shinji-s/fix_multi_support_reset

Fix broken implementation of multi_support_reset()
This commit is contained in:
shahramn 2020-12-15 12:49:18 +00:00 committed by GitHub
commit 7ee4d02183
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 26 deletions

View File

@ -53,6 +53,7 @@ static int grib2_has_next_section(unsigned char* msgbegin, size_t msglen, unsign
static void grib2_build_message(grib_context* context, unsigned char* sections[], size_t sections_len[], void** data, size_t* msglen);
static grib_multi_support* grib_get_multi_support(grib_context* c, FILE* f);
static grib_multi_support* grib_multi_support_new(grib_context* c);
static void grib_multi_support_clear(grib_context* c, grib_multi_support* gm);
static grib_handle* grib_handle_new_multi(grib_context* c, unsigned char** idata, size_t* buflen, int* error);
/* Note: A fast cut-down version of strcmp which does NOT return -1 */
@ -172,7 +173,6 @@ int grib_handle_delete(grib_handle* h)
grib_context_log(ct, GRIB_LOG_DEBUG, "grib_handle_delete: deleting handle %p", (void*)h);
grib_context_free(ct, h);
h = NULL;
}
return GRIB_SUCCESS;
}
@ -1583,16 +1583,12 @@ static grib_multi_support* grib_get_multi_support(grib_context* c, FILE* f)
prev = gm;
gm = gm->next;
}
if (!gm) {
gm = grib_multi_support_new(c);
if (!c->multi_support) {
c->multi_support = gm;
}
else {
if (prev)
prev->next = gm;
}
gm = grib_multi_support_new(c);
if (prev==NULL) { // equivalent to 'if (!c->multi_support)'
c->multi_support = gm;
}
else {
prev->next = gm;
}
gm->next = 0;
@ -1612,23 +1608,14 @@ static grib_multi_support* grib_get_multi_support(grib_context* c, FILE* f)
void grib_multi_support_reset(grib_context* c)
{
grib_multi_support* gm = c->multi_support;
grib_multi_support* next = NULL;
int i = 0;
while (next) {
next = gm->next;
if (gm->file)
fclose(gm->file);
if (gm->message)
grib_context_free(c, gm->message);
gm->message = NULL;
for (i = 0; i < 8; i++)
gm->sections[i] = 0;
if (gm->bitmap_section)
grib_context_free(c, gm->bitmap_section);
gm->bitmap_section = NULL;
grib_multi_support* gm_next = NULL;
while (gm) {
gm_next = gm->next;
grib_multi_support_clear(c, gm);
grib_context_free(c, gm);
gm = NULL;
gm = gm_next;
}
c->multi_support = NULL;
}
static grib_multi_support* grib_multi_support_new(grib_context* c)
@ -1650,3 +1637,17 @@ static grib_multi_support* grib_multi_support_new(grib_context* c)
return gm;
}
static void grib_multi_support_clear(grib_context* c, grib_multi_support *gm)
{
/* Don't close gm->file. It is supposed to be owned by the client. */
/* At lease that is the case for cfgrib. */
if (gm->message) {
grib_context_free(c, gm->message);
gm->message = NULL;
}
if (gm->bitmap_section) {
grib_context_free(c, gm->bitmap_section);
gm->bitmap_section = NULL;
}
}