diff --git a/m-dict.h b/m-dict.h index 89d3369a..3c5a70d1 100644 --- a/m-dict.h +++ b/m-dict.h @@ -325,6 +325,9 @@ M_ARRAY_DEF(m_array_index, m_indexhash_t, M_POD_OPLIST) { \ map->upper_limit = (m_index_t) ((double) size * M_D1CT_OA_UPPER_BOUND) - 1; \ map->lower_limit = (size <= M_D1CT_INITIAL_SIZE) ? 0 : (m_index_t) ((double) size * M_D1CT_OA_LOWER_BOUND) ; \ + if (M_UNLIKELY(map->count <= map->lower_limit)) { \ + map->lower_limit = 0; \ + } \ } \ \ M_P(void, name, _init, dict_t map) \ @@ -1534,6 +1537,9 @@ enum m_d1ct_oa_element_e { /* FIXME: Overflow not handled. What to do in case of it? */ \ dict->upper_limit = (size_t) ((double) size * coeff_up) - 1; \ dict->lower_limit = (size <= M_D1CT_INITIAL_SIZE) ? 0 : (size_t) ((double) size * coeff_down) ; \ + if (M_UNLIKELY(dict->count <= dict->lower_limit)) { \ + dict->lower_limit = 0; \ + } \ } \ \ M_P(void, name, _init, dict_t dict) \ diff --git a/tests/test-mdict.c b/tests/test-mdict.c index 6d847007..2134cb28 100644 --- a/tests/test-mdict.c +++ b/tests/test-mdict.c @@ -893,6 +893,28 @@ static void test_coverage(void) } +/* Reported by florommel */ +static void test_reserve_bug(void) +{ + dict_int_t dict; + dict_int_init(dict); + + /* NOTE: Remove the following line to make it not fail: */ + dict_int_reserve(dict, 32); + + int k = 0; + for (int n = 0; n < 100; n++) { + for (int i = 0; i < 3; i++) { + dict_int_set_at(dict, ++k, 42); + } + for (int i = 0; i < 3; i++) { + dict_int_erase(dict, k--); + } + k += 100; /* ensure fresh keys in the next iteration */ + } + dict_int_clear(dict); +} + int main(void) { test1(); @@ -905,6 +927,7 @@ int main(void) test_it_oa(); test_oa_str1(); test_oa_str2(); + test_reserve_bug(); testobj_final_check(); test_coverage(); exit(0);