mailinfo: handle charset conversion errors in the caller

Instead of dying in convert_to_utf8(), just report an error and let the callers handle it. Between the two callers: - decode_header() silently punts when it cannot parse a broken RFC2047 encoded text (e.g. when it sees anything other than B or Q after it sees "=?<charset>") by jumping to release_return, returning the string it successfully parsed out so far, to the caller. A piece of string that convert_to_utf8() cannot handle can be treated the same way. - handle_commit_msg() doesn't cope with a malformed line well, so die there for now. We'll lift this even higher in later changes in this series. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 14, 2015 at 17:45 UTC 669b963af2778c489b0742d2f4d56aeda7dcfad8
1 file changed +11 -8
mailinfo.c
+11 -8
@@ -344,21 +344,22 @@ static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
344 return out;
345 }
346
347 -static void convert_to_utf8(struct mailinfo *mi,
348 - struct strbuf *line, const char *charset)
347 +static int convert_to_utf8(struct mailinfo *mi,
348 + struct strbuf *line, const char *charset)
349 {
350 char *out;
351
352 if (!mi->metainfo_charset || !charset || !*charset)
353 - return;
353 + return 0;
354
355 if (same_encoding(mi->metainfo_charset, charset))
356 - return;
356 + return 0;
357 out = reencode_string(line->buf, mi->metainfo_charset, charset);
358 if (!out)
359 - die("cannot convert from %s to %s",
360 - charset, mi->metainfo_charset);
359 + return error("cannot convert from %s to %s",
360 + charset, mi->metainfo_charset);
361 strbuf_attach(line, out, strlen(out), strlen(out));
362 + return 0;
363 }
364
365 static void decode_header(struct mailinfo *mi, struct strbuf *it)
@@ -424,7 +425,8 @@ static void decode_header(struct mailinfo *mi, struct strbuf *it)
425 dec = decode_q_segment(&piecebuf, 1);
426 break;
427 }
427 - convert_to_utf8(mi, dec, charset_q.buf);
428 + if (convert_to_utf8(mi, dec, charset_q.buf))
429 + goto release_return;
430
431 strbuf_addbuf(&outbuf, dec);
432 strbuf_release(dec);
@@ -637,7 +639,8 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
639 mi->header_stage = 0;
640
641 /* normalize the log message to UTF-8. */
640 - convert_to_utf8(mi, line, mi->charset.buf);
642 + if (convert_to_utf8(mi, line, mi->charset.buf))
643 + exit(128);
644
645 if (mi->use_scissors && is_scissors_line(line)) {
646 int i;