Merge branch 'jk/ref-filter-colors-fix' into maint
[gitweb.git] / mailinfo.c
index a489d9d0fbcc37a1198247a8daae4b3104ebe284..70187e3eb30b102c7038a1d1c4e0033d856c99ac 100644 (file)
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "utf8.h"
 #include "strbuf.h"
 #include "mailinfo.h"
@@ -57,17 +58,17 @@ static void parse_bogus_from(struct mailinfo *mi, const struct strbuf *line)
 static const char *unquote_comment(struct strbuf *outbuf, const char *in)
 {
        int c;
-       int take_next_litterally = 0;
+       int take_next_literally = 0;
 
        strbuf_addch(outbuf, '(');
 
        while ((c = *in++) != 0) {
-               if (take_next_litterally == 1) {
-                       take_next_litterally = 0;
+               if (take_next_literally == 1) {
+                       take_next_literally = 0;
                } else {
                        switch (c) {
                        case '\\':
-                               take_next_litterally = 1;
+                               take_next_literally = 1;
                                continue;
                        case '(':
                                in = unquote_comment(outbuf, in);
@@ -87,15 +88,15 @@ static const char *unquote_comment(struct strbuf *outbuf, const char *in)
 static const char *unquote_quoted_string(struct strbuf *outbuf, const char *in)
 {
        int c;
-       int take_next_litterally = 0;
+       int take_next_literally = 0;
 
        while ((c = *in++) != 0) {
-               if (take_next_litterally == 1) {
-                       take_next_litterally = 0;
+               if (take_next_literally == 1) {
+                       take_next_literally = 0;
                } else {
                        switch (c) {
                        case '\\':
-                               take_next_litterally = 1;
+                               take_next_literally = 1;
                                continue;
                        case '"':
                                return in;
@@ -367,11 +368,16 @@ static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
 
        while ((c = *in++) != 0) {
                if (c == '=') {
-                       int d = *in++;
+                       int ch, d = *in;
                        if (d == '\n' || !d)
                                break; /* drop trailing newline */
-                       strbuf_addch(out, (hexval(d) << 4) | hexval(*in++));
-                       continue;
+                       ch = hex2chr(in);
+                       if (ch >= 0) {
+                               strbuf_addch(out, ch);
+                               in += 2;
+                               continue;
+                       }
+                       /* garbage -- fall through */
                }
                if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
                        c = 0x20;
@@ -757,8 +763,13 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
        assert(!mi->filter_stage);
 
        if (mi->header_stage) {
-               if (!line->len || (line->len == 1 && line->buf[0] == '\n'))
+               if (!line->len || (line->len == 1 && line->buf[0] == '\n')) {
+                       if (mi->inbody_header_accum.len) {
+                               flush_inbody_header_accum(mi);
+                               mi->header_stage = 0;
+                       }
                        return 0;
+               }
        }
 
        if (mi->use_inbody_headers && mi->header_stage) {
@@ -877,7 +888,10 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
        for (;;) {
                int peek;
 
-               peek = fgetc(in); ungetc(peek, in);
+               peek = fgetc(in);
+               if (peek == EOF)
+                       break;
+               ungetc(peek, in);
                if (peek != ' ' && peek != '\t')
                        break;
                if (strbuf_getline_lf(&continuation, in))
@@ -911,8 +925,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
                /* we hit an end boundary */
                /* pop the current boundary off the stack */
                strbuf_release(*(mi->content_top));
-               free(*(mi->content_top));
-               *(mi->content_top) = NULL;
+               FREE_AND_NULL(*(mi->content_top));
 
                /* technically won't happen as is_multipart_boundary()
                   will fail first.  But just in case..
@@ -1094,6 +1107,10 @@ int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
 
        do {
                peek = fgetc(mi->input);
+               if (peek == EOF) {
+                       fclose(cmitmsg);
+                       return error("empty patch: '%s'", patch);
+               }
        } while (isspace(peek));
        ungetc(peek, mi->input);