From 3e68debb134ce767d747c66d9c962ae39127598e Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 9 Apr 2024 16:30:23 +0100 Subject: [PATCH] xdgmime: Add assertion to silence static analysis false positive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a lot of loop unwinding, during which I think it might have lost its knowledge that `cache->buffer != NULL` (from a prior check on line 765), scan-build seems to think that there can be a `NULL` pointer dereference of `cache->buffer` within `cache_magic_compare_to_data()`. There can’t be. Add an assertion to try and help the analyser. Upstreamed as https://gitlab.freedesktop.org/xdg/xdgmime/-/merge_requests/38. Signed-off-by: Philip Withnall Helps: #1767 --- gio/xdgmime/xdgmimecache.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/gio/xdgmime/xdgmimecache.c b/gio/xdgmime/xdgmimecache.c index f7eddcbe6..7e9b29fad 100644 --- a/gio/xdgmime/xdgmimecache.c +++ b/gio/xdgmime/xdgmimecache.c @@ -245,10 +245,15 @@ cache_magic_compare_to_data (XdgMimeCache *cache, size_t len, int *prio) { - xdg_uint32_t priority = GET_UINT32 (cache->buffer, offset); - xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, offset + 4); - xdg_uint32_t n_matchlets = GET_UINT32 (cache->buffer, offset + 8); - xdg_uint32_t matchlet_offset = GET_UINT32 (cache->buffer, offset + 12); + xdg_uint32_t priority, mimetype_offset, n_matchlets, matchlet_offset; + + assert (cache->buffer != NULL); + + priority = GET_UINT32 (cache->buffer, offset); + mimetype_offset = GET_UINT32 (cache->buffer, offset + 4); + n_matchlets = GET_UINT32 (cache->buffer, offset + 8); + matchlet_offset = GET_UINT32 (cache->buffer, offset + 12); + if (OUT_OF_BOUNDS (matchlet_offset, n_matchlets, 32, cache->size)) return NULL; @@ -280,6 +285,8 @@ cache_magic_lookup_data (XdgMimeCache *cache, xdg_uint32_t j; + assert (cache->buffer != NULL); + *prio = 0; list_offset = GET_UINT32 (cache->buffer, 24);