Error handling for maximum database size

Check that the requested size of a pkghash is not beyond the maximum
prime.  Also check for successful creation of a new hash before
rehashing.

Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Allan McRae 2011-01-29 11:39:25 +10:00
parent 021085624e
commit d843c86b7b

View file

@ -59,6 +59,7 @@ pmpkghash_t *_alpm_pkghash_create(size_t size)
hash->list = NULL;
hash->entries = 0;
hash->buckets = 0;
loopsize = sizeof(prime_list) / sizeof(*prime_list);
for(i = 0; i < loopsize; i++) {
@ -68,6 +69,12 @@ pmpkghash_t *_alpm_pkghash_create(size_t size)
}
}
if(hash->buckets < size) {
_alpm_log(PM_LOG_ERROR, _("database larger than maximum size"));
free(hash);
return(NULL);
}
CALLOC(hash->hash_table, hash->buckets, sizeof(alpm_list_t*), \
free(hash); RET_ERR(PM_ERR_MEMORY, NULL));
@ -98,6 +105,11 @@ static pmpkghash_t *rehash(pmpkghash_t *oldhash)
}
newhash = _alpm_pkghash_create(newsize);
if(newhash == NULL) {
/* creation of newhash failed, stick with old one... */
return(oldhash);
}
for(ptr = oldhash->list; ptr != NULL; ptr = ptr->next) {
newhash = _alpm_pkghash_add(newhash, ptr->data);
}