Skip to content

Commit

Permalink
Fix bug with dict/set error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdrake committed Jun 12, 2015
1 parent e16b69c commit b0a61f2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
22 changes: 16 additions & 6 deletions extension/boolexpr/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,31 +136,41 @@ static bool
_enlarge(struct BX_Dict *dict)
{
struct BX_DictItem *item;

size_t pridx = dict->_pridx;
size_t length = dict->length;
struct BX_DictItem **items = dict->items;

size_t old_width = _primes[pridx];
size_t new_width = _primes[pridx + 1];

dict->_pridx += 1;
dict->length = 0;
dict->items = malloc(_primes[dict->_pridx] * sizeof(struct BX_DictItem *));
dict->items = malloc(new_width * sizeof(struct BX_DictItem *));
if (dict->items == NULL)
return false; // LCOV_EXCL_LINE

for (size_t i = 0; i < _primes[dict->_pridx]; ++i)
for (size_t i = 0; i < new_width; ++i)
dict->items[i] = (struct BX_DictItem *) NULL;

for (size_t i = 0; i < _primes[pridx]; ++i) {
for (size_t i = 0; i < old_width; ++i) {
for (item = items[i]; item; item = item->tail) {
if (!_dict_insert(dict, item->key, item->val)) {
/* LCOV_EXCL_START */
for (size_t j = 0; j < i; ++j)
_list_del(items[j]);
free(items);
_list_del(dict->items[j]);
free(dict->items);
dict->_pridx = pridx;
dict->length = length;
dict->items = items;
return false;
/* LCOV_EXCL_STOP */
}
}
_list_del(items[i]);
}

for (size_t i = 0; i < old_width; ++i)
_list_del(items[i]);
free(items);

return true;
Expand Down
22 changes: 16 additions & 6 deletions extension/boolexpr/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,31 +172,41 @@ static bool
_enlarge(struct BX_Set *set)
{
struct BX_SetItem *item;

size_t pridx = set->_pridx;
size_t length = set->length;
struct BX_SetItem **items = set->items;

size_t old_width = _primes[pridx];
size_t new_width = _primes[pridx + 1];

set->_pridx += 1;
set->length = 0;
set->items = malloc(_primes[set->_pridx] * sizeof(struct BX_SetItem *));
set->items = malloc(new_width * sizeof(struct BX_SetItem *));
if (set->items == NULL)
return false; // LCOV_EXCL_LINE

for (size_t i = 0; i < _primes[set->_pridx]; ++i)
for (size_t i = 0; i < new_width; ++i)
set->items[i] = (struct BX_SetItem *) NULL;

for (size_t i = 0; i < _primes[pridx]; ++i) {
for (size_t i = 0; i < old_width; ++i) {
for (item = items[i]; item; item = item->tail) {
if (!_set_insert(set, item->key)) {
/* LCOV_EXCL_START */
for (size_t j = 0; j < i; ++j)
_list_del(items[j]);
free(items);
_list_del(set->items[j]);
free(set->items);
set->_pridx = pridx;
set->length = length;
set->items = items;
return false;
/* LCOV_EXCL_STOP */
}
}
_list_del(items[i]);
}

for (size_t i = 0; i < old_width; ++i)
_list_del(items[i]);
free(items);

return true;
Expand Down

0 comments on commit b0a61f2

Please sign in to comment.