@matheusbsbs / matheus-bagliano / commits / a9fd1383a7
Fork 0

mailinfo: fix MIME multi-part message boundary handling

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>

Junio C Hamano committed Aug 9, 2008 at 01:17 UTC a9fd1383a73878284d4157b20ac7c735e876102e
6 files changed +76 -7
builtin-mailinfo.c
+11 -6
@@ -203,7 +203,8 @@ static void handle_content_transfer_encoding(const struct strbuf *line)
203
204 static int is_multipart_boundary(const struct strbuf *line)
205 {
206 - return !strbuf_cmp(line, *content_top);
206 + return (((*content_top)->len <= line->len) &&
207 + !memcmp(line->buf, (*content_top)->buf, (*content_top)->len));
208 }
209
210 static void cleanup_subject(struct strbuf *subject)
@@ -649,8 +650,11 @@ again:
650 check_header(&line, p_hdr_data, 0);
651
652 strbuf_release(&newline);
652 - /* eat the blank line after section info */
653 - return (strbuf_getline(&line, fin, '\n') == 0);
653 + /* replenish line */
654 + if (strbuf_getline(&line, fin, '\n'))
655 + return 0;
656 + strbuf_addch(&line, '\n');
657 + return 1;
658 }
659
660 static inline int patchbreak(const struct strbuf *line)
@@ -757,9 +761,10 @@ static void handle_body(void)
761 /* process any boundary lines */
762 if (*content_top && is_multipart_boundary(&line)) {
763 /* flush any leftover */
760 - if (line.len)
761 - handle_filter(&line);
762 -
764 + if (prev.len) {
765 + handle_filter(&prev);
766 + strbuf_reset(&prev);
767 + }
768 if (!handle_boundary())
769 goto handle_body_out;
770 }
t/t5100-mailinfo.sh
+1 -1
@@ -11,7 +11,7 @@ test_expect_success 'split sample box' \
11 'git mailsplit -o. ../t5100/sample.mbox >last &&
12 last=`cat last` &&
13 echo total is $last &&
14 - test `cat last` = 10'
14 + test `cat last` = 11'
15
16 for mail in `echo 00*`
17 do
t/t5100/info0011 new
+5
@@ -0,0 +1,5 @@
1 +Author: A U Thor
2 +Email: a.u.thor@example.com
3 +Subject: Xyzzy
4 +Date: Fri, 8 Aug 2008 13:08:37 +0200 (CEST)
5 +
t/t5100/msg0011 new
+2
@@ -0,0 +1,2 @@
1 +Here comes a commit log message, and
2 +its second line is here.
t/t5100/patch0011 new
+22
@@ -0,0 +1,22 @@
1 +---
2 + builtin-mailinfo.c | 4 ++--
3 +
4 +diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
5 +index 3e5fe51..aabfe5c 100644
6 +--- a/builtin-mailinfo.c
7 ++++ b/builtin-mailinfo.c
8 +@@ -758,8 +758,8 @@ static void handle_body(void)
9 + /* process any boundary lines */
10 + if (*content_top && is_multipart_boundary(&line)) {
11 + /* flush any leftover */
12 +- if (line.len)
13 +- handle_filter(&line);
14 ++ if (prev.len)
15 ++ handle_filter(&prev);
16 +
17 + if (!handle_boundary())
18 + goto handle_body_out;
19 +--
20 +1.6.0.rc2
21 +
22 +
t/t5100/sample.mbox
+35
@@ -465,3 +465,38 @@ index 962aa34..2d1520f 100644
465 --
466 1.5.6.2.455.g1efb2
467
468 +From nobody Fri Aug 8 22:24:03 2008
469 +Date: Fri, 8 Aug 2008 13:08:37 +0200 (CEST)
470 +From: A U Thor <a.u.thor@example.com>
471 +Subject: [PATCH 3/3 v2] Xyzzy
472 +MIME-Version: 1.0
473 +Content-Type: multipart/mixed; boundary="=-=-="
474 +
475 +--=-=-=
476 +Content-Type: text/plain; charset=iso-8859-15
477 +Content-Transfer-Encoding: quoted-printable
478 +
479 +Here comes a commit log message, and
480 +its second line is here.
481 +---
482 + builtin-mailinfo.c | 4 ++--
483 +
484 +diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
485 +index 3e5fe51..aabfe5c 100644
486 +--- a/builtin-mailinfo.c
487 ++++ b/builtin-mailinfo.c
488 +@@ -758,8 +758,8 @@ static void handle_body(void)
489 + /* process any boundary lines */
490 + if (*content_top && is_multipart_boundary(&line)) {
491 + /* flush any leftover */
492 +- if (line.len)
493 +- handle_filter(&line);
494 ++ if (prev.len)
495 ++ handle_filter(&prev);
496 +=20
497 + if (!handle_boundary())
498 + goto handle_body_out;
499 +--=20
500 +1.6.0.rc2
501 +
502 +--=-=-=--