Handle case where src dict is larger in dictMerge

Former-commit-id: c750fdb381f5dd3e92d251a82bd86533eb2cadf5
This commit is contained in:
John Sully 2020-02-02 23:27:25 -05:00
parent 78da9b3af1
commit 424981309d

View File

@ -179,6 +179,7 @@ int dictExpand(dict *d, unsigned long size)
int dictMerge(dict *dst, dict *src) int dictMerge(dict *dst, dict *src)
{ {
assert(dst != src);
if (dictSize(src) == 0) if (dictSize(src) == 0)
return DICT_OK; return DICT_OK;
@ -188,12 +189,15 @@ int dictMerge(dict *dst, dict *src)
return DICT_OK; return DICT_OK;
} }
if (dictSize(src) > dictSize(dst))
std::swap(*dst, *src);
if (!dictIsRehashing(dst) && !dictIsRehashing(src)) if (!dictIsRehashing(dst) && !dictIsRehashing(src))
{ {
dst->ht[1] = dst->ht[0]; dst->ht[1] = dst->ht[0];
dst->ht[0] = src->ht[0]; dst->ht[0] = src->ht[0];
dst->rehashidx = 0;
_dictReset(&src->ht[0]); _dictReset(&src->ht[0]);
dst->rehashidx = 0;
return DICT_OK; return DICT_OK;
} }
@ -210,10 +214,12 @@ int dictMerge(dict *dst, dict *src)
while (de != nullptr) while (de != nullptr)
{ {
dictEntry *deNext = de->next; dictEntry *deNext = de->next;
uint64_t h = dictHashKey(dst, de->key) & htDst.sizemask; uint64_t h = dictHashKey(dst, de->key) & htDst.sizemask;
de->next = htDst.table[h]; de->next = htDst.table[h];
htDst.table[h] = de; htDst.table[h] = de;
htDst.used++; htDst.used++;
de = deNext; de = deNext;
src->ht[iht].used--; src->ht[iht].used--;
} }