mailinfo: fix MIME multi-part message boundary handling
authorJunio C Hamano <gitster@pobox.com>
Sat, 9 Aug 2008 08:17:24 +0000 (01:17 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sat, 9 Aug 2008 08:26:35 +0000 (01:26 -0700)
After finding a MIME multi-part message boundary line, the handle_body()
function is supposed to first flush any accumulated contents from the
previous part to the output stream.  However, the code mistakenly output
the boundary line it found.

The old code that used one global, fixed-length buffer line[] used an
alternate static buffer newline[] for keeping track of this accumulated
contents and flushed newline[] upon seeing the boundary; when 3b6121f
(git-mailinfo: use strbuf's instead of fixed buffers, 2008-07-13)
converted a fixed-length buffer in this program to use strbuf,these two
buffers were converted to "line" and "prev" (the latter of which now has a
much more sensible name) strbufs, but the code mistakenly flushed "line"
(which contains the boundary we have just found), instead of "prev".

This resulted in the first boundary to be output in front of the first
line of the message.

The rewritten implementation of handle_boundary() lost the terminating
newline; this would then result in the second line of the message to be
stuck with the first line.

The is_multipart_boundary() was designed to catch both the internal
boundary and the terminating one (the one with trailing "--"); this also
was broken with the rewrite, and the code in the handle_boundary() to
handle the terminating boundary was never triggered.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-mailinfo.c
t/t5100-mailinfo.sh
t/t5100/info0011 [new file with mode: 0644]
t/t5100/msg0011 [new file with mode: 0644]
t/t5100/patch0011 [new file with mode: 0644]
t/t5100/sample.mbox

index f974b9df968c74c5d62d58b2a09493e6abb4322e..3577382d7039784e8a0c5ef9ce3d765409abb2d8 100644 (file)
@@ -203,7 +203,8 @@ static void handle_content_transfer_encoding(const struct strbuf *line)
 
 static int is_multipart_boundary(const struct strbuf *line)
 {
-       return !strbuf_cmp(line, *content_top);
+       return (((*content_top)->len <= line->len) &&
+               !memcmp(line->buf, (*content_top)->buf, (*content_top)->len));
 }
 
 static void cleanup_subject(struct strbuf *subject)
@@ -649,8 +650,11 @@ again:
                check_header(&line, p_hdr_data, 0);
 
        strbuf_release(&newline);
-       /* eat the blank line after section info */
-       return (strbuf_getline(&line, fin, '\n') == 0);
+       /* replenish line */
+       if (strbuf_getline(&line, fin, '\n'))
+               return 0;
+       strbuf_addch(&line, '\n');
+       return 1;
 }
 
 static inline int patchbreak(const struct strbuf *line)
@@ -757,9 +761,10 @@ static void handle_body(void)
                /* process any boundary lines */
                if (*content_top && is_multipart_boundary(&line)) {
                        /* flush any leftover */
-                       if (line.len)
-                               handle_filter(&line);
-
+                       if (prev.len) {
+                               handle_filter(&prev);
+                               strbuf_reset(&prev);
+                       }
                        if (!handle_boundary())
                                goto handle_body_out;
                }
index e9f3e72c7ee5584d956d46126956c641a7d53905..8dfaddda9129b476016afa88c518e51511da878c 100755 (executable)
@@ -11,7 +11,7 @@ test_expect_success 'split sample box' \
        'git mailsplit -o. ../t5100/sample.mbox >last &&
        last=`cat last` &&
        echo total is $last &&
-       test `cat last` = 10'
+       test `cat last` = 11'
 
 for mail in `echo 00*`
 do
diff --git a/t/t5100/info0011 b/t/t5100/info0011
new file mode 100644 (file)
index 0000000..da5a605
--- /dev/null
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: Xyzzy
+Date: Fri, 8 Aug 2008 13:08:37 +0200 (CEST)
+
diff --git a/t/t5100/msg0011 b/t/t5100/msg0011
new file mode 100644 (file)
index 0000000..4667f21
--- /dev/null
@@ -0,0 +1,2 @@
+Here comes a commit log message, and
+its second line is here.
diff --git a/t/t5100/patch0011 b/t/t5100/patch0011
new file mode 100644 (file)
index 0000000..8841d3c
--- /dev/null
@@ -0,0 +1,22 @@
+---
+ builtin-mailinfo.c  |    4 ++--
+
+diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
+index 3e5fe51..aabfe5c 100644
+--- a/builtin-mailinfo.c
++++ b/builtin-mailinfo.c
+@@ -758,8 +758,8 @@ static void handle_body(void)
+               /* process any boundary lines */
+               if (*content_top && is_multipart_boundary(&line)) {
+                       /* flush any leftover */
+-                      if (line.len)
+-                              handle_filter(&line);
++                      if (prev.len)
++                              handle_filter(&prev);
+                       if (!handle_boundary())
+                               goto handle_body_out;
+-- 
+1.6.0.rc2
+
+
index aba57f922b33b6ab708afbbf82c7e56f6e37bb8d..d7ca79b1fc1c5842cb0ebd95cc7055459f0391a0 100644 (file)
@@ -465,3 +465,38 @@ index 962aa34..2d1520f 100644
 -- 
 1.5.6.2.455.g1efb2
 
+From nobody Fri Aug  8 22:24:03 2008
+Date: Fri, 8 Aug 2008 13:08:37 +0200 (CEST)
+From: A U Thor <a.u.thor@example.com>
+Subject: [PATCH 3/3 v2] Xyzzy
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="=-=-="
+
+--=-=-=
+Content-Type: text/plain; charset=iso-8859-15
+Content-Transfer-Encoding: quoted-printable
+
+Here comes a commit log message, and
+its second line is here.
+---
+ builtin-mailinfo.c  |    4 ++--
+
+diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
+index 3e5fe51..aabfe5c 100644
+--- a/builtin-mailinfo.c
++++ b/builtin-mailinfo.c
+@@ -758,8 +758,8 @@ static void handle_body(void)
+               /* process any boundary lines */
+               if (*content_top && is_multipart_boundary(&line)) {
+                       /* flush any leftover */
+-                      if (line.len)
+-                              handle_filter(&line);
++                      if (prev.len)
++                              handle_filter(&prev);
+=20
+                       if (!handle_boundary())
+                               goto handle_body_out;
+--=20
+1.6.0.rc2
+
+--=-=-=--