From 8768c972136b853ceb2e811c56de5c8d996f592f Mon Sep 17 00:00:00 2001 From: Nelson Ferreira Date: Fri, 20 Jan 2012 12:31:22 -0500 Subject: [PATCH] Coverity: Forward NULL: CID 54, 53, 52 * src/ui/X11/xgccache.c (gc_cache_lookup): Make sure not to derefence possible NULL pointers. Signed-off-by: Nelson Ferreira --- src/ui/X11/xgccache.c | 70 +++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/src/ui/X11/xgccache.c b/src/ui/X11/xgccache.c index 9f350c2..068ce4b 100644 --- a/src/ui/X11/xgccache.c +++ b/src/ui/X11/xgccache.c @@ -151,9 +151,11 @@ GC gc_cache_lookup(struct gc_cache *cache, XGCValues * gcv, unsigned long mask) struct gc_cache_cell *cell = NULL, *next = NULL, *prev = NULL; struct gcv_and_mask gcvm; - if ((!!cache->head) != (!!cache->tail)) + if (cache == NULL) abort(); - else if (cache->head && (cache->head->prev || cache->tail->next)) + else if ((!!cache->head) != (!!cache->tail)) + abort(); + else if (cache->head && cache->tail && (cache->head->prev || cache->tail->next)) abort(); else { gcvm.mask = mask; @@ -176,11 +178,9 @@ GC gc_cache_lookup(struct gc_cache *cache, XGCValues * gcv, unsigned long mask) /* #### This whole file needs some serious overhauling. */ if (!(mask | GCTile) && cell->gc->values.tile) - cell = 0; + cell = NULL; else if (!(mask | GCStipple) && cell->gc->values.stipple) - cell = 0; - - if (cell) + cell = NULL; #endif /* !GCCACHE_HASH */ { @@ -189,38 +189,44 @@ GC gc_cache_lookup(struct gc_cache *cache, XGCValues * gcv, unsigned long mask) be collected than a cell that was accessed less recently. */ - if (cell && cell == cache->tail) - return cell->gc; - next = cell->next; - prev = cell->prev; - if (prev) - prev->next = next; - if (next) - next->prev = prev; - if (cache->head == cell) - cache->head = next; - cell->next = 0; - cell->prev = cache->tail; - cache->tail->next = cell; - cache->tail = cell; - if (cache->head == cell) - abort(); - else if (cell->next) + if (!cell) { abort(); - else if (cache->head->prev) - abort(); - else if (cache->tail->next) - abort(); - if (cell) - return cell->gc; - else return NULL; + } else { + if (cell == cache->tail) + return cell->gc; + next = cell->next; + prev = cell->prev; + if (prev) + prev->next = next; + if (next) + next->prev = prev; + if (cache->head == cell) + cache->head = next; + cell->next = NULL; + cell->prev = cache->tail; + if (cache->tail) + cache->tail->next = cell; + else + abort(); + cache->tail = cell; + if (cache->head == cell) + abort(); + else if (cell->next) + abort(); + else if (cache->head != NULL && cache->head->prev) + abort(); + else if (cache->tail != NULL && cache->tail->next) + abort(); + return cell->gc; + } } /* else, cache miss. */ - - if (cache->size == GC_CACHE_SIZE) + if (cache == NULL) + abort(); + else if (cache->size == GC_CACHE_SIZE) /* Reuse the first cell on the list (least-recently-used). Remove it from the list, and unhash it from the table. -- 2.25.1