From 2bda849bf9b39b423175d1ee1d8796b856cc9988 Mon Sep 17 00:00:00 2001 From: Andrew Gregory Date: Sun, 7 Jan 2018 19:30:42 -0500 Subject: detect pkghash allocation failure If rehash ever failed with a full hash it would return the old hash that is already full. get_hash_position would then loop forever because it would never find an empty bucket. Signed-off-by: Andrew Gregory Signed-off-by: Allan McRae --- lib/libalpm/pkghash.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'lib/libalpm/pkghash.c') diff --git a/lib/libalpm/pkghash.c b/lib/libalpm/pkghash.c index 45e63082..a0e81f28 100644 --- a/lib/libalpm/pkghash.c +++ b/lib/libalpm/pkghash.c @@ -132,8 +132,7 @@ static alpm_pkghash_t *rehash(alpm_pkghash_t *oldhash) newhash = _alpm_pkghash_create(newsize); if(newhash == NULL) { - /* creation of newhash failed, stick with old one... */ - return oldhash; + return NULL; } newhash->list = oldhash->list; @@ -156,23 +155,29 @@ static alpm_pkghash_t *rehash(alpm_pkghash_t *oldhash) return newhash; } -static alpm_pkghash_t *pkghash_add_pkg(alpm_pkghash_t *hash, alpm_pkg_t *pkg, +static alpm_pkghash_t *pkghash_add_pkg(alpm_pkghash_t **hashref, alpm_pkg_t *pkg, int sorted) { alpm_list_t *ptr; unsigned int position; + alpm_pkghash_t *hash; - if(pkg == NULL || hash == NULL) { - return hash; + if(pkg == NULL || hashref == NULL || *hashref == NULL) { + return NULL; } + hash = *hashref; if(hash->entries >= hash->limit) { - hash = rehash(hash); + if((hash = rehash(hash)) == NULL) { + /* resizing failed and there are no more open buckets */ + return NULL; + } + *hashref = hash; } position = get_hash_position(pkg->name_hash, hash); - MALLOC(ptr, sizeof(alpm_list_t), return hash); + MALLOC(ptr, sizeof(alpm_list_t), return NULL); ptr->data = pkg; ptr->prev = ptr; @@ -189,12 +194,12 @@ static alpm_pkghash_t *pkghash_add_pkg(alpm_pkghash_t *hash, alpm_pkg_t *pkg, return hash; } -alpm_pkghash_t *_alpm_pkghash_add(alpm_pkghash_t *hash, alpm_pkg_t *pkg) +alpm_pkghash_t *_alpm_pkghash_add(alpm_pkghash_t **hash, alpm_pkg_t *pkg) { return pkghash_add_pkg(hash, pkg, 0); } -alpm_pkghash_t *_alpm_pkghash_add_sorted(alpm_pkghash_t *hash, alpm_pkg_t *pkg) +alpm_pkghash_t *_alpm_pkghash_add_sorted(alpm_pkghash_t **hash, alpm_pkg_t *pkg) { return pkghash_add_pkg(hash, pkg, 1); } -- cgit v1.2.3-54-g00ecf