Since it FREE()'s the header_cache_t, change the signature to pass
"header_cache_t **", so that the caller's header cache variable is
NULL'ed out too.
---
 hcache.c    | 62 +++++++++++++++++++++++++++++++++++------------------
 hcache.h    |  2 +-
 imap/imap.c |  7 +++---
 imap/util.c |  5 +----
 mh.c        | 10 ++++-----
 pop.c       |  6 +++---
 6 files changed, 54 insertions(+), 38 deletions(-)

diff --git a/hcache.c b/hcache.c
index dc52aba0..52af7a84 100644
--- a/hcache.c
+++ b/hcache.c
@@ -1025,14 +1025,17 @@ hcache_open_qdbm(struct header_cache *h, const char 
*path)
 }
 
 void
-mutt_hcache_close(header_cache_t *h)
+mutt_hcache_close(header_cache_t **ph)
 {
-  if (!h)
+  header_cache_t *h;
+
+  if (!ph || !*ph)
     return;
 
+  h = *ph;
   vlclose(h->db);
   FREE(&h->folder);
-  FREE(&h);
+  FREE(ph);
 }
 
 int
@@ -1080,11 +1083,14 @@ hcache_open_tc(struct header_cache *h, const char *path)
 }
 
 void
-mutt_hcache_close(header_cache_t *h)
+mutt_hcache_close(header_cache_t **ph)
 {
-  if (!h)
+  header_cache_t *h;
+
+  if (!ph || !*ph)
     return;
 
+  h = *ph;
   if (!tcbdbclose(h->db))
   {
 #ifdef DEBUG
@@ -1094,7 +1100,7 @@ mutt_hcache_close(header_cache_t *h)
   }
   tcbdbdel(h->db);
   FREE(&h->folder);
-  FREE(&h);
+  FREE(ph);
 }
 
 int
@@ -1157,17 +1163,20 @@ cleanup:
 }
 
 void
-mutt_hcache_close(header_cache_t *h)
+mutt_hcache_close(header_cache_t **ph)
 {
-  if (!h)
+  header_cache_t *h;
+
+  if (!ph || !*ph)
     return;
 
+  h = *ph;
   if (!kcdbclose(h->db))
     muttdbg(2, "kcdbclose failed for %s: %s (ecode %d)", h->folder,
             kcdbemsg(h->db), kcdbecode(h->db));
   kcdbdel(h->db);
   FREE(&h->folder);
-  FREE(&h);
+  FREE(ph);
 }
 
 int
@@ -1221,16 +1230,19 @@ cleanup:
 }
 
 void
-mutt_hcache_close(header_cache_t *h)
+mutt_hcache_close(header_cache_t **ph)
 {
-  if (!h)
+  header_cache_t *h;
+
+  if (!ph || !*ph)
     return;
 
+  h = *ph;
   if (!tkrzw_dbm_close(h->db))
     muttdbg(2, "tkrzw_dbm_close failed for %s: %s (ecode %d)", h->folder,
             tkrzw_get_last_status_message(), tkrzw_get_last_status_code());
   FREE(&h->folder);
-  FREE(&h);
+  FREE(ph);
 }
 
 int
@@ -1278,14 +1290,17 @@ hcache_open_gdbm(struct header_cache *h, const char 
*path)
 }
 
 void
-mutt_hcache_close(header_cache_t *h)
+mutt_hcache_close(header_cache_t **ph)
 {
-  if (!h)
+  header_cache_t *h;
+
+  if (!ph || !*ph)
     return;
 
+  h = *ph;
   gdbm_close(h->db);
   FREE(&h->folder);
-  FREE(&h);
+  FREE(ph);
 }
 
 int
@@ -1393,11 +1408,14 @@ fail_close:
 }
 
 void
-mutt_hcache_close(header_cache_t *h)
+mutt_hcache_close(header_cache_t **ph)
 {
-  if (!h)
+  header_cache_t *h;
+
+  if (!ph || !*ph)
     return;
 
+  h = *ph;
   h->db->close(h->db, 0);
   h->env->close(h->env, 0);
   mx_unlock_file(mutt_b2s(h->lockfile), h->fd, 0);
@@ -1405,7 +1423,7 @@ mutt_hcache_close(header_cache_t *h)
   unlink(mutt_b2s(h->lockfile));
   mutt_buffer_free(&h->lockfile);
   FREE(&h->folder);
-  FREE(&h);
+  FREE(ph);
 }
 
 int
@@ -1473,13 +1491,15 @@ fail_env:
 }
 
 void
-mutt_hcache_close(header_cache_t *h)
+mutt_hcache_close(header_cache_t **ph)
 {
+  header_cache_t *h;
   int rc;
 
-  if (!h)
+  if (!ph || !*ph)
     return;
 
+  h = *ph;
   if (h->txn)
   {
     if (h->txn_mode == txn_write)
@@ -1498,7 +1518,7 @@ mutt_hcache_close(header_cache_t *h)
 
   mdb_env_close(h->env);
   FREE(&h->folder);
-  FREE(&h);
+  FREE(ph);
 }
 
 int
diff --git a/hcache.h b/hcache.h
index 97ede996..96f67b52 100644
--- a/hcache.h
+++ b/hcache.h
@@ -28,7 +28,7 @@ typedef void(*hcache_namer_t)(const char *path, BUFFER *dest);
 
 header_cache_t *mutt_hcache_open(const char *path, const char *folder,
                                  hcache_namer_t namer);
-void mutt_hcache_close(header_cache_t *h);
+void mutt_hcache_close(header_cache_t **ph);
 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,
diff --git a/imap/imap.c b/imap/imap.c
index 730fa16e..cd3edcd9 100644
--- a/imap/imap.c
+++ b/imap/imap.c
@@ -1739,8 +1739,7 @@ int imap_close_mailbox(CONTEXT *ctx)
     }
 
     mutt_bcache_close(&idata->bcache);
-    mutt_hcache_close(idata->hcache);
-    idata->hcache = NULL;
+    mutt_hcache_close(&idata->hcache);
     idata->hcache_open_count = 0;
   }
 
@@ -2075,7 +2074,7 @@ IMAP_STATUS *imap_mboxcache_get(IMAP_DATA *idata, const 
char *mbox, int create)
         mutt_hcache_free((void **)&puidvalidity);
         mutt_hcache_free((void **)&puidnext);
         mutt_hcache_free((void **)&pmodseq);
-        mutt_hcache_close(hc);
+        mutt_hcache_close(&hc);
         return imap_mboxcache_get(idata, mbox, 1);
       }
       memcpy(&status->uidvalidity, puidvalidity, sizeof(unsigned int));
@@ -2095,7 +2094,7 @@ IMAP_STATUS *imap_mboxcache_get(IMAP_DATA *idata, const 
char *mbox, int create)
     mutt_hcache_free((void **)&puidvalidity);
     mutt_hcache_free((void **)&puidnext);
     mutt_hcache_free((void **)&pmodseq);
-    mutt_hcache_close(hc);
+    mutt_hcache_close(&hc);
   }
 #endif
 
diff --git a/imap/util.c b/imap/util.c
index d3a7c388..6b9940b7 100644
--- a/imap/util.c
+++ b/imap/util.c
@@ -215,10 +215,7 @@ void imap_idata_hcache_close(IMAP_DATA *idata)
 
   idata->hcache_open_count--;
   if (!idata->hcache_open_count)
-  {
-    mutt_hcache_close(idata->hcache);
-    idata->hcache = NULL;
-  }
+    mutt_hcache_close(&idata->hcache);
 }
 
 HEADER *imap_hcache_get(IMAP_DATA *idata, unsigned int uid)
diff --git a/mh.c b/mh.c
index ec9d07c6..eda484ae 100644
--- a/mh.c
+++ b/mh.c
@@ -1206,7 +1206,7 @@ static void maildir_delayed_parsing(CONTEXT *ctx, struct 
maildir **md,
 cleanup:
 #endif
 #if USE_HCACHE
-  mutt_hcache_close(hc);
+  mutt_hcache_close(&hc);
 #endif
   mutt_buffer_pool_release(&fn);
   mh_sort_natural(ctx, md);
@@ -2043,7 +2043,7 @@ int mh_sync_mailbox(CONTEXT * ctx, int *index_hint)
 
 #if USE_HCACHE
   if (ctx->magic == MUTT_MAILDIR || ctx->magic == MUTT_MH)
-    mutt_hcache_close(hc);
+    mutt_hcache_close(&hc);
 #endif /* USE_HCACHE */
 
   if (ctx->magic == MUTT_MH)
@@ -2072,7 +2072,7 @@ err:
   mutt_buffer_pool_release(&tmp);
 #if USE_HCACHE
   if (ctx->magic == MUTT_MAILDIR || ctx->magic == MUTT_MH)
-    mutt_hcache_close(hc);
+    mutt_hcache_close(&hc);
 #endif /* USE_HCACHE */
   return -1;
 }
@@ -2504,7 +2504,7 @@ static int maildir_save_to_header_cache(CONTEXT *ctx, 
HEADER *h)
   hc = mutt_hcache_open(HeaderCache, ctx->path, NULL);
   rc = mutt_hcache_store(hc, h->path + 3, h, 0, &maildir_hcache_keylen,
                          MUTT_GENERATE_UIDVALIDITY);
-  mutt_hcache_close(hc);
+  mutt_hcache_close(&hc);
 #endif
   return rc;
 }
@@ -2518,7 +2518,7 @@ static int mh_save_to_header_cache(CONTEXT *ctx, HEADER 
*h)
 
   hc = mutt_hcache_open(HeaderCache, ctx->path, NULL);
   rc = mutt_hcache_store(hc, h->path, h, 0, strlen, MUTT_GENERATE_UIDVALIDITY);
-  mutt_hcache_close(hc);
+  mutt_hcache_close(&hc);
 #endif
   return rc;
 }
diff --git a/pop.c b/pop.c
index 4a628430..f3182c46 100644
--- a/pop.c
+++ b/pop.c
@@ -404,7 +404,7 @@ static int pop_fetch_headers(CONTEXT *ctx)
   }
 
 #if USE_HCACHE
-  mutt_hcache_close(hc);
+  mutt_hcache_close(&hc);
 #endif
 
   if (ret < 0)
@@ -794,7 +794,7 @@ static int pop_sync_mailbox(CONTEXT *ctx, int *index_hint)
     }
 
 #if USE_HCACHE
-    mutt_hcache_close(hc);
+    mutt_hcache_close(&hc);
 #endif
 
     if (ret == 0)
@@ -862,7 +862,7 @@ static int pop_save_to_header_cache(CONTEXT *ctx, HEADER *h)
   pop_data = (POP_DATA *)ctx->data;
   hc = pop_hcache_open(pop_data, ctx->path);
   rc = mutt_hcache_store(hc, h->data, h, 0, strlen, MUTT_GENERATE_UIDVALIDITY);
-  mutt_hcache_close(hc);
+  mutt_hcache_close(&hc);
 #endif
 
   return rc;
-- 
2.55.0

Reply via email to