From: Kevin McCarthy Date: Wed, 8 Feb 2017 03:36:43 +0000 (-0800) Subject: Create function to free header cache data. X-Git-Tag: neomutt-20170225~32^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2961eca581ae860960a4a44a14e52bc6f5ce55b5;p=neomutt Create function to free header cache data. Kyoto Cabinet documents that data from it should be freed via kcfree(). LMDB claims ownership of the data returned, so convert its free operation to be a noop and remove the malloc from its fetch function. --- diff --git a/hcache.c b/hcache.c index 6e1397326..6c73a6add 100644 --- a/hcache.c +++ b/hcache.c @@ -793,7 +793,7 @@ mutt_hcache_fetch(header_cache_t *h, const char *filename, if (!data || !crc_matches(data, h->crc)) { - FREE(&data); + mutt_hcache_free (&data); return NULL; } @@ -874,10 +874,9 @@ mutt_hcache_fetch_raw (header_cache_t *h, const char *filename, (mdb_get (h->txn, h->db, &key, &data) != MDB_SUCCESS)) return NULL; - /* Unlike other dbs, LMDB claims ownership of the returned data */ - char *d = safe_malloc (data.mv_size); - memcpy (d, data.mv_data, data.mv_size); - return d; + /* LMDB claims ownership of the returned data, so this will not be + * freed in mutt_hcache_free(). */ + return data.mv_data; #endif } @@ -1531,6 +1530,21 @@ mutt_hcache_open(const char *path, const char *folder, hcache_namer_t namer) } } +void mutt_hcache_free (void **data) +{ + if (!data || !*data) + return; + +#if HAVE_KC + kcfree (*data); + *data = NULL; +#elif HAVE_LMDB + /* LMDB owns the data returned. It should not be freed */ +#else + FREE (data); /* __FREE_CHECKED__ */ +#endif +} + #if HAVE_DB4 const char *mutt_hcache_backend (void) { diff --git a/hcache.h b/hcache.h index ca0b425e2..1b7d19941 100644 --- a/hcache.h +++ b/hcache.h @@ -33,6 +33,7 @@ HEADER *mutt_hcache_restore(const unsigned char *d, HEADER **oh); void *mutt_hcache_fetch(header_cache_t *h, const char *filename, size_t (*keylen)(const char *fn)); void *mutt_hcache_fetch_raw (header_cache_t *h, const char *filename, size_t (*keylen)(const char *fn)); +void mutt_hcache_free (void **data); typedef enum { MUTT_GENERATE_UIDVALIDITY = 1 /* use gettimeofday() as value */ diff --git a/imap/imap.c b/imap/imap.c index e4412bb16..275fbb3b2 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -1685,13 +1685,13 @@ IMAP_STATUS* imap_mboxcache_get (IMAP_DATA* idata, const char* mbox, int create) { uidvalidity = mutt_hcache_fetch_raw (hc, "/UIDVALIDITY", imap_hcache_keylen); uidnext = mutt_hcache_fetch_raw (hc, "/UIDNEXT", imap_hcache_keylen); - mutt_hcache_close (hc); if (uidvalidity) { if (!status) { - FREE (&uidvalidity); - FREE (&uidnext); + mutt_hcache_free ((void **)&uidvalidity); + mutt_hcache_free ((void **)&uidnext); + mutt_hcache_close (hc); return imap_mboxcache_get (idata, mbox, 1); } status->uidvalidity = *uidvalidity; @@ -1699,8 +1699,9 @@ IMAP_STATUS* imap_mboxcache_get (IMAP_DATA* idata, const char* mbox, int create) dprint (3, (debugfile, "mboxcache: hcache uidvalidity %d, uidnext %d\n", status->uidvalidity, status->uidnext)); } - FREE (&uidvalidity); - FREE (&uidnext); + mutt_hcache_free ((void **)&uidvalidity); + mutt_hcache_free ((void **)&uidnext); + mutt_hcache_close (hc); } #endif diff --git a/imap/message.c b/imap/message.c index 61672efa4..625b994de 100644 --- a/imap/message.c +++ b/imap/message.c @@ -146,11 +146,11 @@ int imap_read_headers (IMAP_DATA* idata, int msgbegin, int msgend) if (puidnext) { uidnext = *puidnext; - FREE (&puidnext); + mutt_hcache_free ((void **)&puidnext); } if (uid_validity && uidnext && *uid_validity == idata->uid_validity) evalhc = 1; - FREE (&uid_validity); + mutt_hcache_free ((void **)&uid_validity); } if (evalhc) { diff --git a/imap/util.c b/imap/util.c index 924d57364..2baa4f2c5 100644 --- a/imap/util.c +++ b/imap/util.c @@ -130,7 +130,7 @@ HEADER* imap_hcache_get (IMAP_DATA* idata, unsigned int uid) h = mutt_hcache_restore ((unsigned char*)uv, NULL); else dprint (3, (debugfile, "hcache uidvalidity mismatch: %u", *uv)); - FREE (&uv); + mutt_hcache_free ((void **)&uv); } return h; diff --git a/mh.c b/mh.c index 0833b025c..fe4f7ebdf 100644 --- a/mh.c +++ b/mh.c @@ -1195,7 +1195,7 @@ static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir **md, mutt_free_header (&p->h); #if USE_HCACHE } - FREE (&data); + mutt_hcache_free (&data); #endif last = p; } diff --git a/pop.c b/pop.c index a2210d6d2..c81c414f8 100644 --- a/pop.c +++ b/pop.c @@ -336,7 +336,7 @@ static int pop_fetch_headers (CONTEXT *ctx) mutt_hcache_store (hc, ctx->hdrs[i]->data, ctx->hdrs[i], 0, strlen, MUTT_GENERATE_UIDVALIDITY); } - FREE(&data); + mutt_hcache_free (&data); #endif /*