Skip to content

Commit 6ac617a

Browse files
committed
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 <[email protected]>
1 parent 669b963 commit 6ac617a

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

mailinfo.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)
163163
if (slurp_attr(line->buf, "boundary=", boundary)) {
164164
strbuf_insert(boundary, 0, "--", 2);
165165
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;
168170
}
169171
*(mi->content_top) = boundary;
170172
boundary = NULL;
@@ -355,9 +357,11 @@ static int convert_to_utf8(struct mailinfo *mi,
355357
if (same_encoding(mi->metainfo_charset, charset))
356358
return 0;
357359
out = reencode_string(line->buf, mi->metainfo_charset, charset);
358-
if (!out)
360+
if (!out) {
361+
mi->input_error = -1;
359362
return error("cannot convert from %s to %s",
360363
charset, mi->metainfo_charset);
364+
}
361365
strbuf_attach(line, out, strlen(out), strlen(out));
362366
return 0;
363367
}
@@ -367,6 +371,7 @@ static void decode_header(struct mailinfo *mi, struct strbuf *it)
367371
char *in, *ep, *cp;
368372
struct strbuf outbuf = STRBUF_INIT, *dec;
369373
struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT;
374+
int found_error = 1; /* pessimism */
370375

371376
in = it->buf;
372377
while (in - it->buf <= it->len && (ep = strstr(in, "=?")) != NULL) {
@@ -436,10 +441,14 @@ static void decode_header(struct mailinfo *mi, struct strbuf *it)
436441
strbuf_addstr(&outbuf, in);
437442
strbuf_reset(it);
438443
strbuf_addbuf(it, &outbuf);
444+
found_error = 0;
439445
release_return:
440446
strbuf_release(&outbuf);
441447
strbuf_release(&charset_q);
442448
strbuf_release(&piecebuf);
449+
450+
if (found_error)
451+
mi->input_error = -1;
443452
}
444453

445454
static int check_header(struct mailinfo *mi,
@@ -640,7 +649,7 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
640649

641650
/* normalize the log message to UTF-8. */
642651
if (convert_to_utf8(mi, line, mi->charset.buf))
643-
exit(128);
652+
return 0; /* mi->input_error already set */
644653

645654
if (mi->use_scissors && is_scissors_line(line)) {
646655
int i;
@@ -785,12 +794,15 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
785794
will fail first. But just in case..
786795
*/
787796
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;
791801
}
792802
handle_filter(mi, &newline);
793803
strbuf_release(&newline);
804+
if (mi->input_error)
805+
return 0;
794806

795807
/* skip to the next boundary */
796808
if (!find_boundary(mi, line))
@@ -875,6 +887,8 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
875887
handle_filter(mi, line);
876888
}
877889

890+
if (mi->input_error)
891+
break;
878892
} while (!strbuf_getwholeline(line, mi->input, '\n'));
879893

880894
handle_body_out:
@@ -968,7 +982,7 @@ int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
968982

969983
handle_info(mi);
970984
strbuf_release(&line);
971-
return 0;
985+
return mi->input_error;
972986
}
973987

974988
static int git_mailinfo_config(const char *var, const char *value, void *mi_)

mailinfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct mailinfo {
3131
struct strbuf **s_hdr_data;
3232

3333
struct strbuf log_message;
34+
int input_error;
3435
};
3536

3637
extern void setup_mailinfo(struct mailinfo *);

0 commit comments

Comments
 (0)