]> granicus.if.org Git - neomutt/commitdiff
Fix wrong calculation of remaining length in nntp_log_binbuf
authorIan Zimmerman <itz@no-use.mooo.com>
Sat, 9 Feb 2019 05:46:48 +0000 (21:46 -0800)
committerRichard Russon <rich@flatcap.org>
Wed, 20 Feb 2019 13:09:39 +0000 (13:09 +0000)
An even better way to write nntp_log_binbuf

nntp/nntp.c

index e8168100160019f576d6bc1e3fb7e07565b18107..1b5668644213945af32eb930d303b0218cf05e09 100644 (file)
@@ -476,6 +476,25 @@ static int nntp_attempt_features(struct NntpAccountData *adata)
 }
 
 #ifdef USE_SASL
+/**
+ * nntp_memchr - look for a char in a binary buf, conveniently
+ * @param haystack [in/out] input: start here, output: store address of hit
+ * @param sentinel points just beyond (1 byte after) search area
+ * @needle the character to search for
+ * @retval true found and updated haystack
+ * @retval false not found
+ */
+static bool nntp_memchr(char **haystack, char *sentinel, int needle)
+{
+  char *start = *haystack;
+  size_t max_offset = sentinel - start;
+  void *vp = memchr(start, max_offset, needle);
+  if (!vp)
+    return false;
+  *haystack = vp;
+  return true;
+}
+
 /**
  * nntp_log_binbuf - log a buffer possibly containing NUL bytes
  * @param buf source buffer
@@ -485,24 +504,16 @@ static int nntp_attempt_features(struct NntpAccountData *adata)
  */
 static void nntp_log_binbuf(const char *buf, size_t len, const char *pfx, int dbg)
 {
-  size_t left = len;
   char tmp[LONG_STRING];
   char *p = tmp;
+  char *sentinel = tmp + len;
 
   if (DebugLevel < dbg)
     return;
-
   memcpy(tmp, buf, len);
   tmp[len] = '\0';
-  while (left > 0)
-  {
-    void *vp = memchr(p, '\0', left);
-    if (!vp)
-      break;
-    p = vp;
-    *p++ = '.';
-    left -= (p - tmp);
-  }
+  while (nntp_memchr(&p, sentinel, '\0'))
+    *p = '.';
   mutt_debug(dbg, "%s> %s\n", pfx, tmp);
 }
 #endif