From 92253b3965cafcb481069fadff0021c2d36e1a06 Mon Sep 17 00:00:00 2001 From: Shinji Suzuki Date: Sat, 14 Nov 2020 14:44:37 +0900 Subject: [PATCH] Avoid accessing a trie node outside of the critical region. --- src/grib_itrie.c | 6 ++++-- src/grib_itrie_keys.c | 6 ++++-- src/grib_trie.c | 6 ++++-- src/grib_trie_with_rank.c | 3 ++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/grib_itrie.c b/src/grib_itrie.c index b6c0dc105..fbbd0e632 100644 --- a/src/grib_itrie.c +++ b/src/grib_itrie.c @@ -350,8 +350,9 @@ int grib_itrie_get_id(grib_itrie* t, const char* key) t = t->next[mapping[(int)*k++]]; if (t != NULL && t->id != -1) { + int id = t->id; GRIB_MUTEX_UNLOCK(&mutex); - return t->id; + return id; } else { int ret = grib_itrie_insert(last, key); @@ -401,11 +402,12 @@ int grib_itrie_insert(grib_itrie* t, const char* key) Assert(*(t->count) < MAX_NUM_CONCEPTS); } + int id = t->id; GRIB_MUTEX_UNLOCK(&mutex); /*printf("grib_itrie_get_id: %s -> %d\n",key,t->id);*/ - return t->id; + return id; } int grib_itrie_get_size(grib_itrie* t) diff --git a/src/grib_itrie_keys.c b/src/grib_itrie_keys.c index 64995dfa4..f972a85f2 100644 --- a/src/grib_itrie_keys.c +++ b/src/grib_itrie_keys.c @@ -375,11 +375,12 @@ static int grib_hash_keys_insert(grib_itrie* t, const char* key) Assert(*(t->count) + TOTAL_KEYWORDS < ACCESSORS_ARRAY_SIZE); } + int id = t->id; GRIB_MUTEX_UNLOCK(&mutex); /*printf("grib_hash_keys_get_id: %s -> %d\n",key,t->id);*/ - return t->id; + return id; } int grib_hash_keys_get_id(grib_itrie* t, const char* key) @@ -403,8 +404,9 @@ int grib_hash_keys_get_id(grib_itrie* t, const char* key) t = t->next[mapping[(int)*k++]]; if (t != NULL && t->id != -1) { + int id = t->id; GRIB_MUTEX_UNLOCK(&mutex); - return t->id + TOTAL_KEYWORDS + 1; + return id + TOTAL_KEYWORDS + 1; } else { int ret = grib_hash_keys_insert(last, key); diff --git a/src/grib_trie.c b/src/grib_trie.c index 7c122e062..1ddcff1e4 100644 --- a/src/grib_trie.c +++ b/src/grib_trie.c @@ -482,9 +482,11 @@ void* grib_trie_get(grib_trie* t, const char* key) t = t->next[mapping[(int)*k++]]; } - if (*k == 0 && t != NULL && t->data != NULL) { + if( t ) { + /* t!=NULL implies *k==0 */ + void * saved = t->data; GRIB_MUTEX_UNLOCK(&mutex); - return t->data; + return saved; } GRIB_MUTEX_UNLOCK(&mutex); return NULL; diff --git a/src/grib_trie_with_rank.c b/src/grib_trie_with_rank.c index 978499f99..67ef1084b 100644 --- a/src/grib_trie_with_rank.c +++ b/src/grib_trie_with_rank.c @@ -485,8 +485,9 @@ int grib_trie_with_rank_insert(grib_trie_with_rank* t, const char* key, void* da t->objs = grib_oarray_new(t->context, 100, 1000); grib_oarray_push(t->context, t->objs, data); /* grib_trie_with_rank_insert_in_list(t,data); */ + size_t n = t->objs->n; GRIB_MUTEX_UNLOCK(&mutex); - return t->objs->n; /* grib_oarray_used_size(t->objs) */ + return n; /* grib_oarray_used_size(t->objs) */ } /*