From 4b7349a0d1413ef1598e0d9ef97bbefded0bb00c Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Tue, 16 Apr 2019 18:11:35 -0700 Subject: [PATCH] Convert other users of struct Mailbox->pathbuf to use Buffers A few functions in browser.c, buffy.c, and monitor.c were using struct Mailbox->pathbuf but were potentially truncating via fixed size buffers. Convert those to use Buffers too. buffy_get() was creating epath and expanding it, apparently to match against expanded struct Mailbox list entries, was wasn't using the epath. I believe this is a bug, and have switched the comparison to epath. Co-authored-by: Richard Russon --- browser.c | 13 +++++++----- mailbox.c | 25 ++++++++++++++-------- monitor.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 76 insertions(+), 24 deletions(-) diff --git a/browser.c b/browser.c index 5b82db3e5..403fa2aa3 100644 --- a/browser.c +++ b/browser.c @@ -813,6 +813,7 @@ static int examine_mailboxes(struct Menu *menu, struct BrowserState *state) { struct stat s; struct Buffer *md = NULL; + struct Buffer *mailbox = NULL; #ifdef USE_NNTP if (OptNews) @@ -837,6 +838,7 @@ static int examine_mailboxes(struct Menu *menu, struct BrowserState *state) if (STAILQ_EMPTY(&AllMailboxes)) return -1; + mailbox = mutt_buffer_pool_get(); md = mutt_buffer_pool_get(); mutt_mailbox_check(Context ? Context->mailbox : NULL, 0); @@ -849,16 +851,16 @@ static int examine_mailboxes(struct Menu *menu, struct BrowserState *state) np->mailbox->msg_unread = Context->mailbox->msg_unread; } - char buf[PATH_MAX]; - mutt_str_strfcpy(buf, mutt_b2s(np->mailbox->pathbuf), sizeof(buf)); + mutt_buffer_strcpy(mailbox, mutt_b2s(np->mailbox->pathbuf)); if (C_BrowserAbbreviateMailboxes) - mutt_pretty_mailbox(buf, sizeof(buf)); + mutt_buffer_pretty_mailbox(mailbox); switch (np->mailbox->magic) { case MUTT_IMAP: case MUTT_POP: - add_folder(menu, state, buf, np->mailbox->desc, NULL, np->mailbox, NULL); + add_folder(menu, state, mutt_b2s(mailbox), np->mailbox->desc, NULL, + np->mailbox, NULL); continue; case MUTT_NOTMUCH: case MUTT_NNTP: @@ -889,11 +891,12 @@ static int examine_mailboxes(struct Menu *menu, struct BrowserState *state) s.st_mtime = st2.st_mtime; } - add_folder(menu, state, buf, np->mailbox->desc, &s, np->mailbox, NULL); + add_folder(menu, state, mutt_b2s(mailbox), np->mailbox->desc, &s, np->mailbox, NULL); } } browser_sort(state); + mutt_buffer_pool_release(&mailbox); mutt_buffer_pool_release(&md); return 0; } diff --git a/mailbox.c b/mailbox.c index 647cc8787..8a1e95065 100644 --- a/mailbox.c +++ b/mailbox.c @@ -399,13 +399,14 @@ int mutt_mailbox_check(struct Mailbox *m_cur, int force) */ bool mutt_mailbox_list(void) { - char path[PATH_MAX]; char mailboxlist[512]; size_t pos = 0; int first = 1; int have_unnotified = MailboxNotify; + struct Buffer *path = mutt_buffer_pool_get(); + mailboxlist[0] = '\0'; pos += strlen(strncat(mailboxlist, _("New mail in "), sizeof(mailboxlist) - 1 - pos)); struct MailboxNode *np = NULL; @@ -415,11 +416,11 @@ bool mutt_mailbox_list(void) if (!np->mailbox->has_new || (have_unnotified && np->mailbox->notified)) continue; - mutt_str_strfcpy(path, mutt_b2s(np->mailbox->pathbuf), sizeof(path)); - mutt_pretty_mailbox(path, sizeof(path)); + mutt_buffer_strcpy(path, mutt_b2s(np->mailbox->pathbuf)); + mutt_buffer_pretty_mailbox(path); if (!first && (MuttMessageWindow->cols >= 7) && - (pos + strlen(path) >= (size_t) MuttMessageWindow->cols - 7)) + ((pos + mutt_buffer_len(path)) >= ((size_t) MuttMessageWindow->cols - 7))) { break; } @@ -434,7 +435,7 @@ bool mutt_mailbox_list(void) np->mailbox->notified = true; MailboxNotify--; } - pos += strlen(strncat(mailboxlist + pos, path, sizeof(mailboxlist) - 1 - pos)); + pos += strlen(strncat(mailboxlist + pos, mutt_b2s(path), sizeof(mailboxlist) - 1 - pos)); first = 0; } @@ -442,15 +443,21 @@ bool mutt_mailbox_list(void) { strncat(mailboxlist + pos, ", ...", sizeof(mailboxlist) - 1 - pos); } + + mutt_buffer_pool_release(&path); + if (!first) { mutt_message("%s", mailboxlist); return true; } - /* there were no mailboxes needing to be notified, so clean up since - * MailboxNotify has somehow gotten out of sync */ - MailboxNotify = 0; - return false; + else + { + /* there were no mailboxes needing to be notified, so clean up since + * MailboxNotify has somehow gotten out of sync */ + MailboxNotify = 0; + return false; + } } /** diff --git a/monitor.c b/monitor.c index f2e0c8f9b..fbb68335f 100644 --- a/monitor.c +++ b/monitor.c @@ -92,7 +92,7 @@ struct MonitorInfo dev_t st_dev; ino_t st_ino; struct Monitor *monitor; - char path_buf[PATH_MAX]; /* access via path only (maybe not initialized) */ + struct Buffer *path_buf; /* access via path only (maybe not initialized) */ }; /** @@ -198,6 +198,24 @@ static struct Monitor *monitor_new(struct MonitorInfo *info, int descriptor) return monitor; } +/** + * monitor_info_init - Set up a file monitor + * @param info Monitor to initialise + */ +static void monitor_info_init(struct MonitorInfo *info) +{ + memset(info, 0, sizeof(*info)); +} + +/** + * monitor_info_free - Shutdown a file monitor + * @param info Monitor to shut down + */ +static void monitor_info_free(struct MonitorInfo *info) +{ + mutt_buffer_free(&info->path_buf); +} + /** * monitor_delete - Free a file monitor * @param monitor Monitor to free @@ -327,8 +345,10 @@ static int monitor_resolve(struct MonitorInfo *info, struct Mailbox *m) if (fmt) { - snprintf(info->path_buf, sizeof(info->path_buf), fmt, info->path); - info->path = info->path_buf; + if (!info->path_buf) + info->path_buf = mutt_buffer_new(); + mutt_buffer_printf(info->path_buf, fmt, info->path); + info->path = mutt_b2s(info->path_buf); } if (stat(info->path, &sb) != 0) return RESOLVERES_FAIL_STAT; @@ -441,13 +461,16 @@ int mutt_monitor_poll(void) int mutt_monitor_add(struct Mailbox *m) { struct MonitorInfo info; + monitor_info_init(&info); + int rc = 0; int desc = monitor_resolve(&info, m); if (desc != RESOLVERES_OK_NOTEXISTING) { if (!m && (desc == RESOLVERES_OK_EXISTING)) MonitorContextDescriptor = info.monitor->desc; - return (desc == RESOLVERES_OK_EXISTING) ? 0 : -1; + rc = (desc == RESOLVERES_OK_EXISTING) ? 0 : -1; + goto cleanup; } uint32_t mask = info.isdir ? INOTIFY_MASK_DIR : INOTIFY_MASK_FILE; @@ -456,7 +479,8 @@ int mutt_monitor_add(struct Mailbox *m) { mutt_debug(LL_DEBUG2, "inotify_add_watch failed for '%s', errno=%d %s\n", info.path, errno, strerror(errno)); - return -1; + rc = -1; + goto cleanup; } mutt_debug(LL_DEBUG3, "inotify_add_watch descriptor=%d for '%s'\n", desc, info.path); @@ -464,7 +488,10 @@ int mutt_monitor_add(struct Mailbox *m) MonitorContextDescriptor = desc; monitor_new(&info, desc); - return 0; + +cleanup: + monitor_info_free(&info); + return rc; } /** @@ -479,6 +506,10 @@ int mutt_monitor_add(struct Mailbox *m) int mutt_monitor_remove(struct Mailbox *m) { struct MonitorInfo info, info2; + int rc = 0; + + monitor_info_init(&info); + monitor_info_init(&info2); if (!m) { @@ -487,7 +518,10 @@ int mutt_monitor_remove(struct Mailbox *m) } if (monitor_resolve(&info, m) != RESOLVERES_OK_EXISTING) - return 2; + { + rc = 2; + goto cleanup; + } if (Context) { @@ -496,13 +530,17 @@ int mutt_monitor_remove(struct Mailbox *m) if ((monitor_resolve(&info2, NULL) == RESOLVERES_OK_EXISTING) && (info.st_ino == info2.st_ino) && (info.st_dev == info2.st_dev)) { - return 1; + rc = 1; + goto cleanup; } } else { if (mutt_find_mailbox(Context->mailbox->realpath)) - return 1; + { + rc = 1; + goto cleanup; + } } } @@ -512,5 +550,9 @@ int mutt_monitor_remove(struct Mailbox *m) monitor_delete(info.monitor); monitor_check_free(); - return 0; + +cleanup: + monitor_info_free(&info); + monitor_info_free(&info2); + return rc; } -- 2.40.0