Skip to content

Commit 669b963

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

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

mailinfo.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,21 +344,22 @@ static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
344344
return out;
345345
}
346346

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)
349349
{
350350
char *out;
351351

352352
if (!mi->metainfo_charset || !charset || !*charset)
353-
return;
353+
return 0;
354354

355355
if (same_encoding(mi->metainfo_charset, charset))
356-
return;
356+
return 0;
357357
out = reencode_string(line->buf, mi->metainfo_charset, charset);
358358
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);
361361
strbuf_attach(line, out, strlen(out), strlen(out));
362+
return 0;
362363
}
363364

364365
static void decode_header(struct mailinfo *mi, struct strbuf *it)
@@ -424,7 +425,8 @@ static void decode_header(struct mailinfo *mi, struct strbuf *it)
424425
dec = decode_q_segment(&piecebuf, 1);
425426
break;
426427
}
427-
convert_to_utf8(mi, dec, charset_q.buf);
428+
if (convert_to_utf8(mi, dec, charset_q.buf))
429+
goto release_return;
428430

429431
strbuf_addbuf(&outbuf, dec);
430432
strbuf_release(dec);
@@ -637,7 +639,8 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
637639
mi->header_stage = 0;
638640

639641
/* 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);
641644

642645
if (mi->use_scissors && is_scissors_line(line)) {
643646
int i;

0 commit comments

Comments
 (0)