diff options
author | Koichi Sasada <ko1@atdot.net> | 2020-03-07 03:32:15 +0900 |
---|---|---|
committer | NARUSE, Yui <naruse@airemix.jp> | 2020-03-26 21:18:26 +0900 |
commit | ab6f78bc926f6fc12dc8d7846056fc9c04d63ead (patch) | |
tree | 9dbc820d9d6a7ad3905822703db3c5c380f886fd /hash.c | |
parent | e4efca87ba1aa5b6a94b9007040ac3e783c26b43 (diff) |
check ar_table after `#hash` call
ar_table can be converted to st_table just after `ar_do_hash()`
function which calls `#hash` method. We need to check
the representation to detect this mutation.
[Bug #16676]
(cherry picked from commit 4c019f5a626523e99e2827ed917802e3097c380d)
Diffstat (limited to 'hash.c')
-rw-r--r-- | hash.c | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -996,6 +996,11 @@ ar_update(VALUE hash, st_data_t key, st_data_t value = 0, old_key; st_hash_t hash_value = ar_do_hash(key); + if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) { + // `#hash` changes ar_table -> st_table + return -1; + } + if (RHASH_AR_TABLE_SIZE(hash) > 0) { bin = ar_find_entry(hash, hash_value, key); existing = (bin != RHASH_AR_TABLE_MAX_BOUND) ? TRUE : FALSE; @@ -1045,6 +1050,11 @@ ar_insert(VALUE hash, st_data_t key, st_data_t value) unsigned bin = RHASH_AR_TABLE_BOUND(hash); st_hash_t hash_value = ar_do_hash(key); + if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) { + // `#hash` changes ar_table -> st_table + return -1; + } + hash_ar_table(hash); /* prepare ltbl */ bin = ar_find_entry(hash, hash_value, key); @@ -1077,6 +1087,10 @@ ar_lookup(VALUE hash, st_data_t key, st_data_t *value) } else { st_hash_t hash_value = ar_do_hash(key); + if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) { + // `#hash` changes ar_table -> st_table + return st_lookup(RHASH_ST_TABLE(hash), key, value); + } unsigned bin = ar_find_entry(hash, hash_value, key); if (bin == RHASH_AR_TABLE_MAX_BOUND) { @@ -1098,6 +1112,10 @@ ar_delete(VALUE hash, st_data_t *key, st_data_t *value) unsigned bin; st_hash_t hash_value = ar_do_hash(*key); + if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) { + // `#hash` changes ar_table -> st_table + return st_delete(RHASH_ST_TABLE(hash), key, value); + } bin = ar_find_entry(hash, hash_value, *key); |