]> granicus.if.org Git - mutt/commitdiff
Minor buffer handling code cleanup.
authorKevin McCarthy <kevin@8t8.us>
Sun, 10 Mar 2019 01:58:07 +0000 (09:58 +0800)
committerKevin McCarthy <kevin@8t8.us>
Sun, 10 Mar 2019 01:58:07 +0000 (09:58 +0800)
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.

base64.c
imap/command.c
imap/imap.c
imap/message.c
init.c
muttlib.c
parse.c

index d942e546f42f8b5b75f6af931b5a7f8915093c0a..67e13c30492f242e78ae56a753b19fb97d8a3c92 100644 (file)
--- a/base64.c
+++ b/base64.c
@@ -93,6 +93,7 @@ int mutt_buffer_from_base64 (BUFFER *out, const char *in)
 
   mutt_buffer_increase_size (out, mutt_strlen (in));
   olen = mutt_from_base64 (out->data, in, out->dsize);
+  /* mutt_from_base64 returns raw bytes, so don't terminate the buffer either */
   if (olen > 0)
     out->dptr = out->data + olen;
   else
index 175ae5c8262945166984c2d53764638ae45e75fd..1494088c3a8df8ba815158504f207a7b37ec305f 100644 (file)
@@ -448,12 +448,12 @@ static int cmd_start (IMAP_DATA* idata, const char* cmdstr, int flags)
   if (flags & IMAP_CMD_QUEUE)
     return 0;
 
-  if (idata->cmdbuf->dptr == idata->cmdbuf->data)
+  if (mutt_buffer_len (idata->cmdbuf) == 0)
     return IMAP_CMD_BAD;
 
   rc = mutt_socket_write_d (idata->conn, idata->cmdbuf->data, -1,
                             flags & IMAP_CMD_PASS ? IMAP_LOG_PASS : IMAP_LOG_CMD);
-  idata->cmdbuf->dptr = idata->cmdbuf->data;
+  mutt_buffer_clear (idata->cmdbuf);
 
   /* unidle when command queue is flushed */
   if (idata->state == IMAP_IDLE)
index f9ce495711abb8ddd7cb63f201a063014ffdd4c8..4764698a13de733d885e1adf2f65948e49617b6e 100644 (file)
@@ -973,7 +973,7 @@ static int imap_make_msg_set (IMAP_DATA* idata, BUFFER* buf, int flag,
   hdrs = idata->ctx->hdrs;
 
   for (n = *pos;
-       n < idata->ctx->msgcount && buf->dptr - buf->data < IMAP_MAX_CMDLEN;
+       (n < idata->ctx->msgcount) && (mutt_buffer_len (buf) < IMAP_MAX_CMDLEN);
        n++)
   {
     match = 0;
@@ -1086,7 +1086,7 @@ int imap_exec_msgset (IMAP_DATA* idata, const char* pre, const char* post,
 
   do
   {
-    cmd->dptr = cmd->data;
+    mutt_buffer_clear (cmd);
     mutt_buffer_add_printf (cmd, "%s ", pre);
     rc = imap_make_msg_set (idata, cmd, flag, changed, invert, &pos);
     if (rc > 0)
@@ -1153,7 +1153,7 @@ int imap_sync_message_for_copy (IMAP_DATA *idata, HEADER *hdr, BUFFER *cmd,
   }
 
   snprintf (uid, sizeof (uid), "%u", HEADER_DATA(hdr)->uid);
-  cmd->dptr = cmd->data;
+  mutt_buffer_clear (cmd);
   mutt_buffer_addstr (cmd, "UID STORE ");
   mutt_buffer_addstr (cmd, uid);
 
index 95b8366a52f133bc00c2f7201ad01b81415e4ecc..47904f58a3c8d28a1b1f3874061b0dc132110e5a 100644 (file)
@@ -185,7 +185,7 @@ static void imap_fetch_msn_seqset (BUFFER *b, IMAP_DATA *idata, unsigned int msn
   /* Too big.  Just query the whole range then. */
   if (chunks == 150 || mutt_strlen (b->data) > 500)
   {
-    b->dptr = b->data;
+    mutt_buffer_clear (b);
     mutt_buffer_add_printf (b, "%u:%u", msn_begin, msn_end);
   }
 }
diff --git a/init.c b/init.c
index fe75c2db2d1e2c09c4e38cea0de47599b56659b5..387fde15f58c64444765d1bd59d641774add8842 100644 (file)
--- a/init.c
+++ b/init.c
@@ -140,8 +140,7 @@ int mutt_extract_token (BUFFER *dest, BUFFER *tok, int flags)
   char         qc = 0; /* quote char */
   char         *pc;
 
-  /* reset the destination pointer to the beginning of the buffer */
-  dest->dptr = dest->data;
+  mutt_buffer_clear (dest);
 
   SKIPWS (tok->dptr);
   while ((ch = *tok->dptr))
@@ -3406,9 +3405,7 @@ void mutt_init (int skip_sys_rc, LIST *commands)
   BUFFER err;
 
   mutt_buffer_init (&err);
-  err.dsize = STRING;
-  err.data = safe_malloc(err.dsize);
-  err.dptr = err.data;
+  mutt_buffer_increase_size (&err, STRING);
 
   Groups = hash_create (1031, 0);
   /* reverse alias keys need to be strdup'ed because of idna conversions */
index 191c1df794eb82682f48d5d1fb67e64b2406a900..f458e7acfa48988bb1bf7533d64d47aa56b41512 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
@@ -1275,6 +1275,8 @@ void mutt_FormatString (char *dest,               /* output buffer */
 
       /* prepare BUFFERs */
       srcbuf = mutt_buffer_from (srccopy);
+      /* note: we are resetting dptr and *reading* from the buffer, so we don't
+       * want to use mutt_buffer_clear(). */
       srcbuf->dptr = srcbuf->data;
       word = mutt_buffer_new ();
       command = mutt_buffer_new ();
diff --git a/parse.c b/parse.c
index c243062c5391bdc0f6dda2844bb6ef2172fc1bfc..47e0f08434d3f80707cb380baa1a199033cea6da 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -1447,8 +1447,7 @@ ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs,
          /* else overwrite */
          else
          {
-           e->spam->dptr = e->spam->data;
-           *e->spam->dptr = '\0';
+            mutt_buffer_clear (e->spam);
            mutt_buffer_addstr(e->spam, buf);
          }
        }