@matheusbsbs / matheus-bagliano / commits / 34fc5cefa7
Fork 0

mailinfo: do not get confused with logical lines that are too long.

It basically considers all the continuation lines to be lines of their own, and if the total line is bigger than what we can fit in it, we just truncate the result rather than stop in the middle and then get confused when we try to parse the "next" line (which is just the remainder of the first line). [jc: added test, and tightened boundary a bit per list discussion.] Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Feb 26, 2007 at 11:10 UTC 34fc5cefa7068492d5103b40dca1b55f69986eb8
6 files changed +147 -19
builtin-mailinfo.c
+54 -18
@@ -406,6 +406,11 @@ static int is_rfc2822_header(char *line)
406 */
407 int ch;
408 char *cp = line;
409 +
410 + /* Count mbox From headers as headers */
411 + if (!memcmp(line, "From ", 5) || !memcmp(line, ">From ", 6))
412 + return 1;
413 +
414 while ((ch = *cp++)) {
415 if (ch == ':')
416 return cp != line;
@@ -417,30 +422,61 @@ static int is_rfc2822_header(char *line)
422 return 0;
423 }
424
425 +/*
426 + * sz is size of 'line' buffer in bytes. Must be reasonably
427 + * long enough to hold one physical real-world e-mail line.
428 + */
429 static int read_one_header_line(char *line, int sz, FILE *in)
430 {
422 - int ofs = 0;
423 - while (ofs < sz) {
424 - int peek, len;
425 - if (fgets(line + ofs, sz - ofs, in) == NULL)
426 - break;
427 - len = eatspace(line + ofs);
428 - if ((len == 0) || !is_rfc2822_header(line)) {
429 - /* Re-add the newline */
430 - line[ofs + len] = '\n';
431 - line[ofs + len + 1] = '\0';
432 - break;
433 - }
434 - ofs += len;
435 - /* Yuck, 2822 header "folding" */
431 + int len;
432 +
433 + /*
434 + * We will read at most (sz-1) bytes and then potentially
435 + * re-add NUL after it. Accessing line[sz] after this is safe
436 + * and we can allow len to grow up to and including sz.
437 + */
438 + sz--;
439 +
440 + /* Get the first part of the line. */
441 + if (!fgets(line, sz, in))
442 + return 0;
443 +
444 + /*
445 + * Is it an empty line or not a valid rfc2822 header?
446 + * If so, stop here, and return false ("not a header")
447 + */
448 + len = eatspace(line);
449 + if (!len || !is_rfc2822_header(line)) {
450 + /* Re-add the newline */
451 + line[len] = '\n';
452 + line[len + 1] = '\0';
453 + return 0;
454 + }
455 +
456 + /*
457 + * Now we need to eat all the continuation lines..
458 + * Yuck, 2822 header "folding"
459 + */
460 + for (;;) {
461 + int peek, addlen;
462 + static char continuation[1000];
463 +
464 peek = fgetc(in); ungetc(peek, in);
465 if (peek != ' ' && peek != '\t')
466 break;
467 + if (!fgets(continuation, sizeof(continuation), in))
468 + break;
469 + addlen = eatspace(continuation);
470 + if (len < sz - 1) {
471 + if (addlen >= sz - len)
472 + addlen = sz - len - 1;
473 + memcpy(line + len, continuation, addlen);
474 + len += addlen;
475 + }
476 }
440 - /* Count mbox From headers as headers */
441 - if (!ofs && (!memcmp(line, "From ", 5) || !memcmp(line, ">From ", 6)))
442 - ofs = 1;
443 - return ofs;
477 + line[len] = 0;
478 +
479 + return 1;
480 }
481
482 static int decode_q_segment(char *in, char *ot, char *ep, int rfc2047)
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` = 5'
14 + test `cat last` = 6'
15
16 for mail in `echo 00*`
17 do
t/t5100/info0006 new
+5
@@ -0,0 +1,5 @@
1 +Author: A U Thor
2 +Email: a.u.thor@example.com
3 +Subject: a commit.
4 +Date: Fri, 9 Jun 2006 00:44:16 -0700
5 +
t/t5100/msg0006 new
+2
@@ -0,0 +1,2 @@
1 +Here is a patch from A U Thor.
2 +
t/t5100/patch0006 new
+14
@@ -0,0 +1,14 @@
1 +---
2 + foo | 2 +-
3 + 1 files changed, 1 insertions(+), 1 deletions(-)
4 +
5 +diff --git a/foo b/foo
6 +index 9123cdc..918dcf8 100644
7 +--- a/foo
8 ++++ b/foo
9 +@@ -1 +1 @@
10 +-Fri Jun 9 00:44:04 PDT 2006
11 ++Fri Jun 9 00:44:13 PDT 2006
12 +--
13 +1.4.0.g6f2b
14 +
t/t5100/sample.mbox
+71
@@ -315,3 +315,74 @@ To unsubscribe from this list: send the line "unsubscribe git" in
315 the body of a message to majordomo@vger.kernel.org
316 More majordomo info at http://vger.kernel.org/majordomo-info.html
317
318 +From nobody Mon Sep 17 00:00:00 2001
319 +From: A U Thor <a.u.thor@example.com>
320 +References: <Pine.LNX.4.640.0001@woody.linux-foundation.org>
321 + <Pine.LNX.4.640.0002@woody.linux-foundation.org>
322 + <Pine.LNX.4.640.0003@woody.linux-foundation.org>
323 + <Pine.LNX.4.640.0004@woody.linux-foundation.org>
324 + <Pine.LNX.4.640.0005@woody.linux-foundation.org>
325 + <Pine.LNX.4.640.0006@woody.linux-foundation.org>
326 + <Pine.LNX.4.640.0007@woody.linux-foundation.org>
327 + <Pine.LNX.4.640.0008@woody.linux-foundation.org>
328 + <Pine.LNX.4.640.0009@woody.linux-foundation.org>
329 + <Pine.LNX.4.640.0010@woody.linux-foundation.org>
330 + <Pine.LNX.4.640.0011@woody.linux-foundation.org>
331 + <Pine.LNX.4.640.0012@woody.linux-foundation.org>
332 + <Pine.LNX.4.640.0013@woody.linux-foundation.org>
333 + <Pine.LNX.4.640.0014@woody.linux-foundation.org>
334 + <Pine.LNX.4.640.0015@woody.linux-foundation.org>
335 + <Pine.LNX.4.640.0016@woody.linux-foundation.org>
336 + <Pine.LNX.4.640.0017@woody.linux-foundation.org>
337 + <Pine.LNX.4.640.0018@woody.linux-foundation.org>
338 + <Pine.LNX.4.640.0019@woody.linux-foundation.org>
339 + <Pine.LNX.4.640.0020@woody.linux-foundation.org>
340 + <Pine.LNX.4.640.0021@woody.linux-foundation.org>
341 + <Pine.LNX.4.640.0022@woody.linux-foundation.org>
342 + <Pine.LNX.4.640.0023@woody.linux-foundation.org>
343 + <Pine.LNX.4.640.0024@woody.linux-foundation.org>
344 + <Pine.LNX.4.640.0025@woody.linux-foundation.org>
345 + <Pine.LNX.4.640.0026@woody.linux-foundation.org>
346 + <Pine.LNX.4.640.0027@woody.linux-foundation.org>
347 + <Pine.LNX.4.640.0028@woody.linux-foundation.org>
348 + <Pine.LNX.4.640.0029@woody.linux-foundation.org>
349 + <Pine.LNX.4.640.0030@woody.linux-foundation.org>
350 + <Pine.LNX.4.640.0031@woody.linux-foundation.org>
351 + <Pine.LNX.4.640.0032@woody.linux-foundation.org>
352 + <Pine.LNX.4.640.0033@woody.linux-foundation.org>
353 + <Pine.LNX.4.640.0034@woody.linux-foundation.org>
354 + <Pine.LNX.4.640.0035@woody.linux-foundation.org>
355 + <Pine.LNX.4.640.0036@woody.linux-foundation.org>
356 + <Pine.LNX.4.640.0037@woody.linux-foundation.org>
357 + <Pine.LNX.4.640.0038@woody.linux-foundation.org>
358 + <Pine.LNX.4.640.0039@woody.linux-foundation.org>
359 + <Pine.LNX.4.640.0040@woody.linux-foundation.org>
360 + <Pine.LNX.4.640.0041@woody.linux-foundation.org>
361 + <Pine.LNX.4.640.0042@woody.linux-foundation.org>
362 + <Pine.LNX.4.640.0043@woody.linux-foundation.org>
363 + <Pine.LNX.4.640.0044@woody.linux-foundation.org>
364 + <Pine.LNX.4.640.0045@woody.linux-foundation.org>
365 + <Pine.LNX.4.640.0046@woody.linux-foundation.org>
366 + <Pine.LNX.4.640.0047@woody.linux-foundation.org>
367 + <Pine.LNX.4.640.0048@woody.linux-foundation.org>
368 + <Pine.LNX.4.640.0049@woody.linux-foundation.org>
369 + <Pine.LNX.4.640.0050@woody.linux-foundation.org>
370 +Date: Fri, 9 Jun 2006 00:44:16 -0700
371 +Subject: [PATCH] a commit.
372 +
373 +Here is a patch from A U Thor.
374 +
375 +---
376 + foo | 2 +-
377 + 1 files changed, 1 insertions(+), 1 deletions(-)
378 +
379 +diff --git a/foo b/foo
380 +index 9123cdc..918dcf8 100644
381 +--- a/foo
382 ++++ b/foo
383 +@@ -1 +1 @@
384 +-Fri Jun 9 00:44:04 PDT 2006
385 ++Fri Jun 9 00:44:13 PDT 2006
386 +--
387 +1.4.0.g6f2b
388 +