Skip to content

Commit

Permalink
notmuch: check for long run during db free
Browse files Browse the repository at this point in the history
`nm_db_release(...)` should protect against freeing a long run database.
Prior to this commit, it was up to the programmer to ensure that a long
run database was not released.

The commit moves the check into `nm_db_release(...)` and removes the
checks elsewhere to reduce unnecessary, repeated checks.
  • Loading branch information
Austin-Ray authored and flatcap committed Dec 4, 2018
1 parent 198f1cb commit 1cdfc0d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
30 changes: 12 additions & 18 deletions notmuch/mutt_notmuch.c
Expand Up @@ -597,8 +597,7 @@ static notmuch_query_t *get_query(struct Mailbox *m, bool writable)
mutt_debug(2, "nm: query successfully initialized (%s)\n", str);
return q;
err:
if (!nm_db_is_longrun(m))
nm_db_release(m);
nm_db_release(m);
return NULL;
}

Expand Down Expand Up @@ -1637,8 +1636,8 @@ int nm_read_entire_thread(struct Context *ctx, struct Email *e)
done:
if (q)
notmuch_query_destroy(q);
if (!nm_db_is_longrun(m))
nm_db_release(m);

nm_db_release(m);

if (m->msg_count == mdata->oldmsgcount)
mutt_message(_("No more messages in the thread"));
Expand Down Expand Up @@ -1853,8 +1852,7 @@ int nm_update_filename(struct Mailbox *m, const char *old, const char *new, stru

int rc = rename_filename(m, old, new, e);

if (!nm_db_is_longrun(m))
nm_db_release(m);
nm_db_release(m);
m->mtime.tv_sec = time(NULL);
m->mtime.tv_nsec = 0;
return rc;
Expand Down Expand Up @@ -2028,8 +2026,8 @@ int nm_record_message(struct Mailbox *m, char *path, struct Email *e)
notmuch_message_destroy(msg);
if (trans == 1)
nm_db_trans_end(m);
if (!nm_db_is_longrun(m))
nm_db_release(m);

nm_db_release(m);
return rc;
}

Expand Down Expand Up @@ -2078,8 +2076,7 @@ int nm_get_all_tags(struct Mailbox *m, char **tag_list, int *tag_count)
if (tags)
notmuch_tags_destroy(tags);

if (!nm_db_is_longrun(m))
nm_db_release(m);
nm_db_release(m);

mutt_debug(1, "nm: get all tags done [rc=%d tag_count=%u]\n", rc, *tag_count);
return rc;
Expand Down Expand Up @@ -2165,8 +2162,7 @@ static int nm_mbox_open(struct Mailbox *m, struct Context *ctx)
notmuch_query_destroy(q);
}

if (!nm_db_is_longrun(m))
nm_db_release(m);
nm_db_release(m);

m->mtime.tv_sec = time(NULL);
m->mtime.tv_nsec = 0;
Expand Down Expand Up @@ -2295,8 +2291,7 @@ static int nm_mbox_check(struct Context *ctx, int *index_hint)
if (q)
notmuch_query_destroy(q);

if (!nm_db_is_longrun(m))
nm_db_release(m);
nm_db_release(m);

m->mtime.tv_sec = time(NULL);
m->mtime.tv_nsec = 0;
Expand Down Expand Up @@ -2389,8 +2384,8 @@ static int nm_mbox_sync(struct Context *ctx, int *index_hint)
mutt_str_strfcpy(m->path, uri, sizeof(m->path));
m->magic = MUTT_NOTMUCH;

if (!nm_db_is_longrun(m))
nm_db_release(m);
nm_db_release(m);

if (changed)
{
m->mtime.tv_sec = time(NULL);
Expand Down Expand Up @@ -2500,8 +2495,7 @@ static int nm_tags_commit(struct Mailbox *m, struct Email *e, char *buf)
rc = 0;
e->changed = true;
done:
if (!nm_db_is_longrun(m))
nm_db_release(m);
nm_db_release(m);
if (e->changed)
{
m->mtime.tv_sec = time(NULL);
Expand Down
2 changes: 1 addition & 1 deletion notmuch/nm_db.c
Expand Up @@ -162,7 +162,7 @@ notmuch_database_t *nm_db_get(struct Mailbox *m, bool writable)
int nm_db_release(struct Mailbox *m)
{
struct NmAccountData *adata = nm_adata_get(m);
if (!adata || !adata->db)
if (!adata || !adata->db || nm_db_is_longrun(m))
return -1;

mutt_debug(1, "nm: db close\n");
Expand Down

0 comments on commit 1cdfc0d

Please sign in to comment.