From: Kevin McCarthy Date: Sun, 10 Mar 2019 01:58:07 +0000 (+0800) Subject: Minor buffer handling code cleanup X-Git-Tag: 2019-10-25~276^2~12 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5952fc25565111089e4c2b708e00f168ae396f47;p=neomutt Minor buffer handling code cleanup Use mutt_buffer_len() and mutt_buffer_clear() to make the code a bit clearer. There are still places in the code that manipulate the buffers directly (pattern.c, for example), but that doesn't mean we shouldn't abstract the buffer where we can. Add comments in a couple places where unusual buffer manipulation is occurring. Co-authored-by: Richard Russon --- diff --git a/email/parse.c b/email/parse.c index d2ad032d1..ad6b1936b 100644 --- a/email/parse.c +++ b/email/parse.c @@ -1073,12 +1073,9 @@ struct Envelope *mutt_rfc822_read_header(FILE *fp, struct Email *e, bool user_hd mutt_buffer_addstr(env->spam, C_SpamSeparator); mutt_buffer_addstr(env->spam, buf); } - - /* else overwrite */ - else + else /* overwrite */ { - env->spam->dptr = env->spam->data; - *env->spam->dptr = '\0'; + mutt_buffer_reset(env->spam); mutt_buffer_addstr(env->spam, buf); } } diff --git a/imap/command.c b/imap/command.c index fba3f985f..0d6e4ffcf 100644 --- a/imap/command.c +++ b/imap/command.c @@ -200,12 +200,12 @@ static int cmd_start(struct ImapAccountData *adata, const char *cmdstr, ImapCmdF if (flags & IMAP_CMD_QUEUE) return 0; - if (adata->cmdbuf->dptr == adata->cmdbuf->data) + if (mutt_buffer_len(adata->cmdbuf) == 0) return IMAP_CMD_BAD; rc = mutt_socket_send_d(adata->conn, adata->cmdbuf->data, (flags & IMAP_CMD_PASS) ? IMAP_LOG_PASS : IMAP_LOG_CMD); - adata->cmdbuf->dptr = adata->cmdbuf->data; + mutt_buffer_reset(adata->cmdbuf); /* unidle when command queue is flushed */ if (adata->state == IMAP_IDLE) diff --git a/imap/imap.c b/imap/imap.c index fdce9463e..dcf87b4b7 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -192,7 +192,7 @@ static int make_msg_set(struct Mailbox *m, struct Buffer *buf, int flag, struct Email **emails = m->emails; - for (n = *pos; (n < m->msg_count) && ((buf->dptr - buf->data) < IMAP_MAX_CMDLEN); n++) + for (n = *pos; (n < m->msg_count) && (mutt_buffer_len(buf) < IMAP_MAX_CMDLEN); n++) { bool match = false; /* whether current message matches flag condition */ /* don't include pending expunged messages. @@ -1042,7 +1042,7 @@ int imap_exec_msgset(struct Mailbox *m, const char *pre, const char *post, do { - cmd->dptr = cmd->data; + mutt_buffer_reset(cmd); mutt_buffer_add_printf(cmd, "%s ", pre); rc = make_msg_set(m, cmd, flag, changed, invert, &pos); if (rc > 0) @@ -1105,7 +1105,7 @@ int imap_sync_message_for_copy(struct Mailbox *m, struct Email *e, } snprintf(uid, sizeof(uid), "%u", imap_edata_get(e)->uid); - cmd->dptr = cmd->data; + mutt_buffer_reset(cmd); mutt_buffer_addstr(cmd, "UID STORE "); mutt_buffer_addstr(cmd, uid); diff --git a/imap/message.c b/imap/message.c index b393e84da..537e8bd75 100644 --- a/imap/message.c +++ b/imap/message.c @@ -639,7 +639,7 @@ static void imap_fetch_msn_seqset(struct Buffer *b, struct ImapAccountData *adat /* Too big. Just query the whole range then. */ if ((chunks == 150) || (mutt_str_strlen(b->data) > 500)) { - b->dptr = b->data; + mutt_buffer_reset(b); mutt_buffer_add_printf(b, "%u:%u", msn_begin, msn_end); } } diff --git a/init.c b/init.c index 899015830..93f4b6bc5 100644 --- a/init.c +++ b/init.c @@ -2665,8 +2665,7 @@ int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags char qc = 0; /* quote char */ char *pc = NULL; - /* reset the destination pointer to the beginning of the buffer */ - dest->dptr = dest->data; + mutt_buffer_reset(dest); SKIPWS(tok->dptr); while ((ch = *tok->dptr)) @@ -3002,9 +3001,7 @@ int mutt_init(bool skip_sys_rc, struct ListHead *commands) struct Buffer err; mutt_buffer_init(&err); - err.dsize = 256; - err.data = mutt_mem_malloc(err.dsize); - err.dptr = err.data; + mutt_buffer_increase_size(&err, 256); mutt_grouplist_init(); /* reverse alias keys need to be strdup'ed because of idna conversions */ diff --git a/mutt/base64.c b/mutt/base64.c index c1d01d481..c67613d63 100644 --- a/mutt/base64.c +++ b/mutt/base64.c @@ -201,6 +201,7 @@ int mutt_b64_buffer_decode(struct Buffer *buf, const char *in) { mutt_buffer_increase_size(buf, mutt_str_strlen(in)); int olen = mutt_b64_decode(in, buf->data, buf->dsize); + /* mutt_from_base64 returns raw bytes, so don't terminate the buffer either */ if (olen > 0) buf->dptr = buf->data + olen; else diff --git a/muttlib.c b/muttlib.c index 77f813b8f..8d6253e94 100644 --- a/muttlib.c +++ b/muttlib.c @@ -852,8 +852,10 @@ void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const c strncpy(srccopy, src, n); srccopy[n - 1] = '\0'; - /* prepare BUFFERs */ + /* prepare Buffers */ struct Buffer *srcbuf = mutt_buffer_from(srccopy); + /* note: we are resetting dptr and *reading* from the buffer, so we don't + * want to use mutt_buffer_reset(). */ srcbuf->dptr = srcbuf->data; struct Buffer *word = mutt_buffer_new(); struct Buffer *cmd = mutt_buffer_new();