rfc822: Fix bug parsing headers when body begins with linear whitespace
authorDavid Gibson <david@gibson.dropbear.id.au>
Sat, 30 Jun 2012 05:34:23 +0000 (15:34 +1000)
committerDavid Gibson <david@gibson.dropbear.id.au>
Sat, 30 Jun 2012 05:34:23 +0000 (15:34 +1000)
next_header_parse() had a bug which would mis-parse an extra header if the
body of the message began with a linear whitespace character (space or
tab).  This patch fixes it and adds a suitable testcase.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
ccan/rfc822/rfc822.c
ccan/rfc822/test/testdata.h

index 3bd3dd747ae28f532b4eef592297cc0e9b689d4f..4a5e4ccdcc709d130fb6c7222ff215d9858e91ac 100644 (file)
@@ -150,29 +150,33 @@ static struct rfc822_header *next_header_parse(struct rfc822_msg *msg)
        if (msg->body && (msg->remainder >= msg->body))
                return NULL;
 
-       eh = h = msg->remainder;
-       do {
-               eh = next_line(eh, msg->end);
-       } while ((eh < msg->end) && rfc822_iswsp(*eh));
-
-       if (eh >= msg->end)
-               msg->remainder = NULL;
-       else
-               msg->remainder = eh;
+       h = msg->remainder;
+       eh = next_line(h, msg->end);
 
        ev = eh;
        if ((ev > h) && (ev[-1] == '\n'))
                ev--;
        if ((ev > h) && (ev[-1] == '\r'))
                ev--;
-
        if (ev == h) {
                /* Found the end of the headers */
+
+               assert(!msg->body || (msg->body == eh));
+
                if (eh < msg->end)
                        msg->body = eh;
                return NULL;
        }
 
+       while ((eh < msg->end) && rfc822_iswsp(*eh))
+               eh = next_line(eh, msg->end);
+
+       if (eh >= msg->end)
+               msg->remainder = NULL;
+       else
+               msg->remainder = eh;
+
+
        hi = talloc_zero(msg, struct rfc822_header);
        ALLOC_CHECK(hi, NULL);
 
index ceb3bcd892078e429509d1911988ad5ebbf45ac0..4ef07095474cef88fa940a466f2798e4f7ec79cd 100644 (file)
@@ -53,11 +53,15 @@ AEXAMPLE(test_msg_nlnl_crlf);
 const char test_msg_nlnl_mixed_body[] = "Message containing both \n\n and \r\n\r\n inside body\n\r\n";
 AEXAMPLE(test_msg_nlnl_mixed);
 
+#define test_msg_space_body_hdrs test_msg_1_hdrs
+const char test_msg_space_body_body[] = " Message with LWS at start of body\n";
+AEXAMPLE(test_msg_space_body);
 
 #define for_each_aexample(_e)                               \
        foreach_ptr((_e), &test_msg_1, &test_msg_empty_body, \
                    &test_msg_nlnl_lf, &test_msg_nlnl_crlf, \
-                   &test_msg_nlnl_mixed)
+                   &test_msg_nlnl_mixed, \
+                   &test_msg_space_body)
 
 #define for_each_aexample_buf(_e, _buf, _len)  \
        for_each_aexample((_e))                 \