mailinfo: remove calls to exit() and die() deep in the callchain

The top-level mailinfo() would instead punt when the code in the deeper part of the callchain detects an unrecoverable error in the input. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 14, 2015 at 17:45 UTC 6ac617a321383b2e3a0f0537e3224ec6229e6500
2 files changed +23 -8
mailinfo.c
+22 -8
@@ -163,8 +163,10 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)
163 if (slurp_attr(line->buf, "boundary=", boundary)) {
164 strbuf_insert(boundary, 0, "--", 2);
165 if (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) {
166 - fprintf(stderr, "Too many boundaries to handle\n");
167 - exit(1);
166 + error("Too many boundaries to handle");
167 + mi->input_error = -1;
168 + mi->content_top = &mi->content[MAX_BOUNDARIES] - 1;
169 + return;
170 }
171 *(mi->content_top) = boundary;
172 boundary = NULL;
@@ -355,9 +357,11 @@ static int convert_to_utf8(struct mailinfo *mi,
357 if (same_encoding(mi->metainfo_charset, charset))
358 return 0;
359 out = reencode_string(line->buf, mi->metainfo_charset, charset);
358 - if (!out)
360 + if (!out) {
361 + mi->input_error = -1;
362 return error("cannot convert from %s to %s",
363 charset, mi->metainfo_charset);
364 + }
365 strbuf_attach(line, out, strlen(out), strlen(out));
366 return 0;
367 }
@@ -367,6 +371,7 @@ static void decode_header(struct mailinfo *mi, struct strbuf *it)
371 char *in, *ep, *cp;
372 struct strbuf outbuf = STRBUF_INIT, *dec;
373 struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT;
374 + int found_error = 1; /* pessimism */
375
376 in = it->buf;
377 while (in - it->buf <= it->len && (ep = strstr(in, "=?")) != NULL) {
@@ -436,10 +441,14 @@ static void decode_header(struct mailinfo *mi, struct strbuf *it)
441 strbuf_addstr(&outbuf, in);
442 strbuf_reset(it);
443 strbuf_addbuf(it, &outbuf);
444 + found_error = 0;
445 release_return:
446 strbuf_release(&outbuf);
447 strbuf_release(&charset_q);
448 strbuf_release(&piecebuf);
449 +
450 + if (found_error)
451 + mi->input_error = -1;
452 }
453
454 static int check_header(struct mailinfo *mi,
@@ -640,7 +649,7 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
649
650 /* normalize the log message to UTF-8. */
651 if (convert_to_utf8(mi, line, mi->charset.buf))
643 - exit(128);
652 + return 0; /* mi->input_error already set */
653
654 if (mi->use_scissors && is_scissors_line(line)) {
655 int i;
@@ -785,12 +794,15 @@ again:
794 will fail first. But just in case..
795 */
796 if (--mi->content_top < mi->content) {
788 - fprintf(stderr, "Detected mismatched boundaries, "
789 - "can't recover\n");
790 - exit(1);
797 + error("Detected mismatched boundaries, can't recover");
798 + mi->input_error = -1;
799 + mi->content_top = mi->content;
800 + return 0;
801 }
802 handle_filter(mi, &newline);
803 strbuf_release(&newline);
804 + if (mi->input_error)
805 + return 0;
806
807 /* skip to the next boundary */
808 if (!find_boundary(mi, line))
@@ -875,6 +887,8 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
887 handle_filter(mi, line);
888 }
889
890 + if (mi->input_error)
891 + break;
892 } while (!strbuf_getwholeline(line, mi->input, '\n'));
893
894 handle_body_out:
@@ -968,7 +982,7 @@ int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
982
983 handle_info(mi);
984 strbuf_release(&line);
971 - return 0;
985 + return mi->input_error;
986 }
987
988 static int git_mailinfo_config(const char *var, const char *value, void *mi_)
mailinfo.h
+1
@@ -31,6 +31,7 @@ struct mailinfo {
31 struct strbuf **s_hdr_data;
32
33 struct strbuf log_message;
34 + int input_error;
35 };
36
37 extern void setup_mailinfo(struct mailinfo *);