mx_open_mailbox can initialize a context in two different ways: either it gets a pointer to an existing context, or it will allocate one if the pointer is NULL. Currently, when it is passed a pointer to an existing context, it is always a pointer to a uninitialized context that was just allocated on the stack, but that may not hold true forever. Plus it's not obvious to determine contexts' lifespan with this construct.
This commit changes calls to mx_open_mailbox to always let this function allocates contexts on the heap. The point is to make mx_{open,close}_mailbox responsible for allocating and freeing contexts, and make the lifespan of these objects more tied to the mx API. Ultimately, 'CONTEXT' could become opaque pointers that are passed to mx_* functions. --- attach.c | 21 ++++++++++++--------- buffy.c | 20 +++++++++++--------- commands.c | 20 ++++++++++++-------- editmsg.c | 30 +++++++++++++++++------------- mx.c | 26 ++++++++++++++++---------- pop.c | 17 ++++++++++------- postpone.c | 11 +++++++---- sendlib.c | 31 ++++++++++++++++++------------- 8 files changed, 103 insertions(+), 73 deletions(-) diff --git a/attach.c b/attach.c index 84cdf6d..bf935f5 100644 --- a/attach.c +++ b/attach.c @@ -717,7 +717,7 @@ int mutt_save_attachment (FILE *fp, BODY *m, char *path, int flags, HEADER *hdr) char buf[HUGE_STRING]; HEADER *hn; - CONTEXT ctx; + CONTEXT *ctx; MESSAGE *msg; int chflags = 0; int r = -1; @@ -729,24 +729,27 @@ int mutt_save_attachment (FILE *fp, BODY *m, char *path, int flags, HEADER *hdr) fseeko (fp, m->offset, 0); if (fgets (buf, sizeof (buf), fp) == NULL) return -1; - if (mx_open_mailbox(path, MUTT_APPEND | MUTT_QUIET, &ctx) == NULL) + ctx = mx_open_mailbox (path, MUTT_APPEND | MUTT_QUIET, NULL); + if (ctx == NULL) return -1; - if ((msg = mx_open_new_message (&ctx, hn, is_from (buf, NULL, 0, NULL) ? 0 : MUTT_ADD_FROM)) == NULL) + if ((msg = mx_open_new_message (ctx, hn, is_from (buf, NULL, 0, NULL) ? 0 : MUTT_ADD_FROM)) == NULL) { - mx_close_mailbox(&ctx, NULL); + mx_close_mailbox(ctx, NULL); + FREE (&ctx); return -1; } - if (ctx.magic == MUTT_MBOX || ctx.magic == MUTT_MMDF) + if (ctx->magic == MUTT_MBOX || ctx->magic == MUTT_MMDF) chflags = CH_FROM | CH_UPDATE_LEN; - chflags |= (ctx.magic == MUTT_MAILDIR ? CH_NOSTATUS : CH_UPDATE); + chflags |= (ctx->magic == MUTT_MAILDIR ? CH_NOSTATUS : CH_UPDATE); if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0 - && mx_commit_message (msg, &ctx) == 0) + && mx_commit_message (msg, ctx) == 0) r = 0; else r = -1; - mx_close_message (&ctx, &msg); - mx_close_mailbox (&ctx, NULL); + mx_close_message (ctx, &msg); + mx_close_mailbox (ctx, NULL); + FREE (&ctx); return r; } else diff --git a/buffy.c b/buffy.c index 1306297..61e491f 100644 --- a/buffy.c +++ b/buffy.c @@ -418,7 +418,6 @@ static int buffy_mbox_check (BUFFY* mailbox, struct stat *sb, int check_stats) { int rc = 0; int new_or_changed; - CONTEXT ctx; if (option (OPTCHECKMBOXSIZE)) new_or_changed = sb->st_size > mailbox->size; @@ -447,15 +446,18 @@ static int buffy_mbox_check (BUFFY* mailbox, struct stat *sb, int check_stats) if (check_stats && (mailbox->stats_last_checked < sb->st_mtime)) { - if (mx_open_mailbox (mailbox->path, - MUTT_READONLY | MUTT_QUIET | MUTT_NOSORT | MUTT_PEEK, - &ctx) != NULL) + CONTEXT *ctx = mx_open_mailbox (mailbox->path, + MUTT_READONLY | MUTT_QUIET | + MUTT_NOSORT | MUTT_PEEK, + NULL); + if (ctx) { - mailbox->msg_count = ctx.msgcount; - mailbox->msg_unread = ctx.unread; - mailbox->msg_flagged = ctx.flagged; - mailbox->stats_last_checked = ctx.mtime; - mx_close_mailbox (&ctx, 0); + mailbox->msg_count = ctx->msgcount; + mailbox->msg_unread = ctx->unread; + mailbox->msg_flagged = ctx->flagged; + mailbox->stats_last_checked = ctx->mtime; + mx_close_mailbox (ctx, 0); + FREE (&ctx); } } diff --git a/commands.c b/commands.c index 2202a67..d6474f9 100644 --- a/commands.c +++ b/commands.c @@ -732,7 +732,7 @@ int mutt_save_message (HEADER *h, int delete, int i, need_buffy_cleanup; int need_passphrase = 0, app=0; char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX]; - CONTEXT ctx; + CONTEXT *ctx; struct stat st; *redraw = 0; @@ -835,13 +835,15 @@ int mutt_save_message (HEADER *h, int delete, } #endif - if (mx_open_mailbox (buf, MUTT_APPEND, &ctx) != NULL) + ctx = mx_open_mailbox (buf, MUTT_APPEND, NULL); + if (ctx) { if (h) { - if (_mutt_save_message(h, &ctx, delete, decode, decrypt) != 0) + if (_mutt_save_message(h, ctx, delete, decode, decrypt) != 0) { - mx_close_mailbox (&ctx, NULL); + mx_close_mailbox (ctx, NULL); + FREE (&ctx); return -1; } } @@ -853,18 +855,20 @@ int mutt_save_message (HEADER *h, int delete, { mutt_message_hook (Context, Context->hdrs[Context->v2r[i]], MUTT_MESSAGEHOOK); if (_mutt_save_message(Context->hdrs[Context->v2r[i]], - &ctx, delete, decode, decrypt) != 0) + ctx, delete, decode, decrypt) != 0) { - mx_close_mailbox (&ctx, NULL); + mx_close_mailbox (ctx, NULL); + FREE (&ctx); return -1; } } } } - need_buffy_cleanup = (ctx.magic == MUTT_MBOX || ctx.magic == MUTT_MMDF); + need_buffy_cleanup = (ctx->magic == MUTT_MBOX || ctx->magic == MUTT_MMDF); - mx_close_mailbox (&ctx, NULL); + mx_close_mailbox (ctx, NULL); + FREE (&ctx); if (need_buffy_cleanup) mutt_buffy_cleanup (buf, &st); diff --git a/editmsg.c b/editmsg.c index 356810a..93384e9 100644 --- a/editmsg.c +++ b/editmsg.c @@ -56,7 +56,7 @@ static int edit_one_message (CONTEXT *ctx, HEADER *cur) int of, cf; - CONTEXT tmpctx; + CONTEXT *tmpctx; MESSAGE *msg; FILE *fp = NULL; @@ -69,21 +69,22 @@ static int edit_one_message (CONTEXT *ctx, HEADER *cur) omagic = DefaultMagic; DefaultMagic = MUTT_MBOX; - rc = (mx_open_mailbox (tmp, MUTT_NEWFOLDER, &tmpctx) == NULL) ? -1 : 0; + tmpctx = mx_open_mailbox (tmp, MUTT_NEWFOLDER, NULL); DefaultMagic = omagic; - if (rc == -1) + if (!tmpctx) { mutt_error (_("could not create temporary folder: %s"), strerror (errno)); return -1; } - rc = mutt_append_message (&tmpctx, ctx, cur, 0, CH_NOLEN | + rc = mutt_append_message (tmpctx, ctx, cur, 0, CH_NOLEN | ((ctx->magic == MUTT_MBOX || ctx->magic == MUTT_MMDF) ? 0 : CH_NOSTATUS)); oerrno = errno; - mx_close_mailbox (&tmpctx, NULL); + mx_close_mailbox (tmpctx, NULL); + FREE (&tmpctx); if (rc == -1) { @@ -143,7 +144,8 @@ static int edit_one_message (CONTEXT *ctx, HEADER *cur) goto bail; } - if (mx_open_mailbox (ctx->path, MUTT_APPEND, &tmpctx) == NULL) + tmpctx = mx_open_mailbox (ctx->path, MUTT_APPEND, NULL); + if (!tmpctx) { rc = -1; /* L10N: %s is from strerror(errno) */ @@ -152,11 +154,11 @@ static int edit_one_message (CONTEXT *ctx, HEADER *cur) } of = 0; - cf = ((tmpctx.magic == MUTT_MBOX || tmpctx.magic == MUTT_MMDF) ? 0 : CH_NOSTATUS); + cf = ((tmpctx->magic == MUTT_MBOX || tmpctx->magic == MUTT_MMDF) ? 0 : CH_NOSTATUS); if (fgets (buff, sizeof (buff), fp) && is_from (buff, NULL, 0, NULL)) { - if (tmpctx.magic == MUTT_MBOX || tmpctx.magic == MUTT_MMDF) + if (tmpctx->magic == MUTT_MBOX || tmpctx->magic == MUTT_MMDF) cf = CH_FROM | CH_FORCE_FROM; } else @@ -170,13 +172,14 @@ static int edit_one_message (CONTEXT *ctx, HEADER *cur) o_read = cur->read; o_old = cur->old; cur->read = cur->old = 0; - msg = mx_open_new_message (&tmpctx, cur, of); + msg = mx_open_new_message (tmpctx, cur, of); cur->read = o_read; cur->old = o_old; if (msg == NULL) { mutt_error (_("Can't append to folder: %s"), strerror (errno)); - mx_close_mailbox (&tmpctx, NULL); + mx_close_mailbox (tmpctx, NULL); + FREE (&tmpctx); goto bail; } @@ -186,10 +189,11 @@ static int edit_one_message (CONTEXT *ctx, HEADER *cur) rc = mutt_copy_stream (fp, msg->fp); } - rc = mx_commit_message (msg, &tmpctx); - mx_close_message (&tmpctx, &msg); + rc = mx_commit_message (msg, tmpctx); + mx_close_message (tmpctx, &msg); - mx_close_mailbox (&tmpctx, NULL); + mx_close_mailbox (tmpctx, NULL); + FREE (&tmpctx); bail: if (fp) safe_fclose (&fp); diff --git a/mx.c b/mx.c index fbe82e4..f0833a5 100644 --- a/mx.c +++ b/mx.c @@ -715,7 +715,7 @@ static int sync_mailbox (CONTEXT *ctx, int *index_hint) /* move deleted mails to the trash folder */ static int trash_append (CONTEXT *ctx) { - CONTEXT ctx_trash; + CONTEXT *ctx_trash; int i; struct stat st, stc; int opt_confappend, rc; @@ -755,20 +755,23 @@ static int trash_append (CONTEXT *ctx) } #endif - if (mx_open_mailbox (TrashPath, MUTT_APPEND, &ctx_trash) != NULL) + ctx_trash = mx_open_mailbox (TrashPath, MUTT_APPEND, NULL); + if (ctx_trash) { /* continue from initial scan above */ for (; i < ctx->msgcount ; i++) if (ctx->hdrs[i]->deleted && (!ctx->hdrs[i]->purge)) { - if (mutt_append_message (&ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) + if (mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) { - mx_close_mailbox (&ctx_trash, NULL); + mx_close_mailbox (ctx_trash, NULL); + FREE (&ctx_trash); return -1; } } - mx_close_mailbox (&ctx_trash, NULL); + mx_close_mailbox (ctx_trash, NULL); + FREE (&ctx_trash); } else { @@ -785,7 +788,6 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint) int i, move_messages = 0, purge = 1, read_msgs = 0; int check; int isSpool = 0; - CONTEXT f; char mbox[_POSIX_PATH_MAX]; char buf[SHORT_STRING]; @@ -907,7 +909,9 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint) else /* use regular append-copy mode */ #endif { - if (mx_open_mailbox (mbox, MUTT_APPEND, &f) == NULL) + CONTEXT *f = mx_open_mailbox (mbox, MUTT_APPEND, NULL); + + if (!f) { ctx->closing = 0; return -1; @@ -918,21 +922,23 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint) if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) { - if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0) + if (mutt_append_message (f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0) { mutt_set_flag (ctx, ctx->hdrs[i], MUTT_DELETE, 1); mutt_set_flag (ctx, ctx->hdrs[i], MUTT_PURGE, 1); } else { - mx_close_mailbox (&f, NULL); + mx_close_mailbox (f, NULL); + FREE (&f); ctx->closing = 0; return -1; } } } - mx_close_mailbox (&f, NULL); + mx_close_mailbox (f, NULL); + FREE (&f); } } diff --git a/pop.c b/pop.c index 294d71d..6ad97d4 100644 --- a/pop.c +++ b/pop.c @@ -781,7 +781,7 @@ void pop_fetch_mail (void) char *url, *p; int i, delanswer, last = 0, msgs, bytes, rset = 0, ret; CONNECTION *conn; - CONTEXT ctx; + CONTEXT *ctx; MESSAGE *msg = NULL; ACCOUNT acct; POP_DATA *pop_data; @@ -856,7 +856,8 @@ void pop_fetch_mail (void) goto finish; } - if (mx_open_mailbox (NONULL (Spoolfile), MUTT_APPEND, &ctx) == NULL) + ctx = mx_open_mailbox (NONULL (Spoolfile), MUTT_APPEND, NULL); + if (!ctx) goto finish; delanswer = query_quadoption (OPT_POPDELETE, _("Delete messages from server?")); @@ -866,7 +867,7 @@ void pop_fetch_mail (void) for (i = last + 1 ; i <= msgs ; i++) { - if ((msg = mx_open_new_message (&ctx, NULL, MUTT_ADD_FROM)) == NULL) + if ((msg = mx_open_new_message (ctx, NULL, MUTT_ADD_FROM)) == NULL) ret = -3; else { @@ -875,13 +876,13 @@ void pop_fetch_mail (void) if (ret == -3) rset = 1; - if (ret == 0 && mx_commit_message (msg, &ctx) != 0) + if (ret == 0 && mx_commit_message (msg, ctx) != 0) { rset = 1; ret = -3; } - mx_close_message (&ctx, &msg); + mx_close_message (ctx, &msg); } if (ret == 0 && delanswer == MUTT_YES) @@ -893,7 +894,8 @@ void pop_fetch_mail (void) if (ret == -1) { - mx_close_mailbox (&ctx, NULL); + mx_close_mailbox (ctx, NULL); + FREE (&ctx); goto fail; } if (ret == -2) @@ -910,7 +912,8 @@ void pop_fetch_mail (void) mutt_message (_("%s [%d of %d messages read]"), msgbuf, i - last, msgs - last); } - mx_close_mailbox (&ctx, NULL); + mx_close_mailbox (ctx, NULL); + FREE (&ctx); if (rset) { diff --git a/postpone.c b/postpone.c index 4fe1543..9f03792 100644 --- a/postpone.c +++ b/postpone.c @@ -58,7 +58,6 @@ static short UpdateNumPostponed = 0; int mutt_num_postponed (int force) { struct stat st; - CONTEXT ctx; static time_t LastModify = 0; static char *OldPostponed = NULL; @@ -125,15 +124,19 @@ int mutt_num_postponed (int force) if (LastModify < st.st_mtime) { + CONTEXT *ctx; LastModify = st.st_mtime; if (access (Postponed, R_OK | F_OK) != 0) return (PostCount = 0); - if (mx_open_mailbox (Postponed, MUTT_NOSORT | MUTT_QUIET, &ctx) == NULL) + + ctx = mx_open_mailbox (Postponed, MUTT_NOSORT | MUTT_QUIET, NULL); + if (!ctx) PostCount = 0; else - PostCount = ctx.msgcount; - mx_fastclose_mailbox (&ctx); + PostCount = ctx->msgcount; + mx_fastclose_mailbox (ctx); + FREE (&ctx); } return (PostCount); diff --git a/sendlib.c b/sendlib.c index 771eae2..2d48f87 100644 --- a/sendlib.c +++ b/sendlib.c @@ -2692,7 +2692,7 @@ static void set_noconv_flags (BODY *b, short flag) int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int post, char *fcc) { - CONTEXT f; + CONTEXT *f; MESSAGE *msg; char tempfile[_POSIX_PATH_MAX]; FILE *tempfp = NULL; @@ -2704,7 +2704,8 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int post, if (post) set_noconv_flags (hdr->content, 1); - if (mx_open_mailbox (path, MUTT_APPEND | MUTT_QUIET, &f) == NULL) + f = mx_open_mailbox (path, MUTT_APPEND | MUTT_QUIET, NULL); + if (!f) { dprint (1, (debugfile, "mutt_write_fcc(): unable to open mailbox %s in append-mode, aborting.\n", path)); @@ -2714,13 +2715,14 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int post, /* We need to add a Content-Length field to avoid problems where a line in * the message body begins with "From " */ - if (f.magic == MUTT_MMDF || f.magic == MUTT_MBOX) + if (f->magic == MUTT_MMDF || f->magic == MUTT_MBOX) { mutt_mktemp (tempfile, sizeof (tempfile)); if ((tempfp = safe_fopen (tempfile, "w+")) == NULL) { mutt_perror (tempfile); - mx_close_mailbox (&f, NULL); + mx_close_mailbox (f, NULL); + FREE (&f); return (-1); } /* remember new mail status before appending message */ @@ -2732,9 +2734,10 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int post, onm_flags = MUTT_ADD_FROM; if (post) onm_flags |= MUTT_SET_DRAFT; - if ((msg = mx_open_new_message (&f, hdr, onm_flags)) == NULL) + if ((msg = mx_open_new_message (f, hdr, onm_flags)) == NULL) { - mx_close_mailbox (&f, NULL); + mx_close_mailbox (f, NULL); + FREE (&f); return (-1); } @@ -2758,7 +2761,7 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int post, if (post && fcc) fprintf (msg->fp, "X-Mutt-Fcc: %s\n", fcc); - if (f.magic == MUTT_MMDF || f.magic == MUTT_MBOX) + if (f->magic == MUTT_MMDF || f->magic == MUTT_MBOX) fprintf (msg->fp, "Status: RO\n"); /* mutt_write_rfc822_header() only writes out a Date: header with @@ -2849,9 +2852,10 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int post, dprint (1, (debugfile, "mutt_write_fcc(): %s: write failed.\n", tempfile)); safe_fclose (&tempfp); unlink (tempfile); - mx_commit_message (msg, &f); /* XXX - really? */ - mx_close_message (&f, &msg); - mx_close_mailbox (&f, NULL); + mx_commit_message (msg, f); /* XXX - really? */ + mx_close_message (f, &msg); + mx_close_mailbox (f, NULL); + FREE (&f); return -1; } @@ -2877,10 +2881,11 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int post, r = mutt_write_mime_body (hdr->content, msg->fp); } - if (mx_commit_message (msg, &f) != 0) + if (mx_commit_message (msg, f) != 0) r = -1; - mx_close_message (&f, &msg); - mx_close_mailbox (&f, NULL); + mx_close_message (f, &msg); + mx_close_mailbox (f, NULL); + FREE (&f); if (!post && need_buffy_cleanup) mutt_buffy_cleanup (path, &st); -- 2.9.3