mailinfo: More carefully parse header lines in read_one_header_line()
We exited prematurely from header parsing loop when the header field did not have a space after the colon but we insisted on it, and we got the check wrong because we forgot that we strip the trailing whitespace before we do the check. The space after the colon is not even required by RFC2822, so stop requiring it. While we are at it, the header line is specified to be more strict than "anything with a colon in it" (there must be one or more characters before the colon, and they must not be controls, SP or non US-ASCII), so implement that check as well, lest we mistakenly think something like: Bogus not a header line: this is not. as a header line. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
May 26, 2006 at 00:46 UTC
ef29c11702594e616cf43bea260515d9f14f17b0
1 file changed
+24
-3
mailinfo.c
+24
-3
@@ -382,19 +382,40 @@ static void check_header_line(char *line)
382
check_header(line, header);
383
}
384
385
+static int is_rfc2822_header(char *line)
386
+{
387
+ /*
388
+ * The section that defines the loosest possible
389
+ * field name is "3.6.8 Optional fields".
390
+ *
391
+ * optional-field = field-name ":" unstructured CRLF
392
+ * field-name = 1*ftext
393
+ * ftext = %d33-57 / %59-126
394
+ */
395
+ int ch;
396
+ char *cp = line;
397
+ while ((ch = *cp++)) {
398
+ if (ch == ':')
399
+ return cp != line;
400
+ if ((33 <= ch && ch <= 57) ||
401
+ (59 <= ch && ch <= 126))
402
+ continue;
403
+ break;
404
+ }
405
+ return 0;
406
+}
407
+
408
static int read_one_header_line(char *line, int sz, FILE *in)
409
{
410
int ofs = 0;
411
while (ofs < sz) {
389
- const char *colon;
412
int peek, len;
413
if (fgets(line + ofs, sz - ofs, in) == NULL)
414
break;
415
len = eatspace(line + ofs);
416
if (len == 0)
417
break;
396
- colon = strchr(line, ':');
397
- if (!colon || !isspace(colon[1])) {
418
+ if (!is_rfc2822_header(line)) {
419
/* Re-add the newline */
420
line[ofs + len] = '\n';
421
line[ofs + len + 1] = '\0';