send-email: improve RFC2047 quote parsing

The RFC2047 unquoting, used to parse email addresses in From and Cc headers, is broken in several ways: * It erroneously substitutes ' ' for '_' in *the whole* header, even outside the quoted field. [Noticed by Christoph.] * It is too liberal in its matching, and happily matches the start of one quoted chunk against the end of another, or even just something that looks like such an end. [Noticed by Junio.] * It fundamentally cannot cope with encodings that are not a superset of ASCII, nor several (incompatible) encodings in the same header. This patch fixes the first two by doing a more careful decoding of the outer quoting (e.g. "=AB" to represent an octet whose value is 0xAB). Fixing the fundamental issues is left for a future, more intrusive, patch. Noticed-by: Christoph Miebach <christoph.miebach@web.de> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Thomas Rast <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Rast committed Jul 30, 2012 at 21:25 UTC b622d4d11d27fd290f7732c6a65f40c054796c1f
2 files changed +19 -4
git-send-email.perl
+6 -4
@@ -862,11 +862,13 @@ $time = time - scalar $#files;
862 sub unquote_rfc2047 {
863 local ($_) = @_;
864 my $encoding;
865 - if (s/=\?([^?]+)\?q\?(.*)\?=/$2/g) {
865 + s{=\?([^?]+)\?q\?(.*?)\?=}{
866 $encoding = $1;
867 - s/_/ /g;
868 - s/=([0-9A-F]{2})/chr(hex($1))/eg;
869 - }
867 + my $e = $2;
868 + $e =~ s/_/ /g;
869 + $e =~ s/=([0-9A-F]{2})/chr(hex($1))/eg;
870 + $e;
871 + }eg;
872 return wantarray ? ($_, $encoding) : $_;
873 }
874
t/t9001-send-email.sh
+13
@@ -841,6 +841,19 @@ test_expect_success $PREREQ '--compose adds MIME for utf8 subject' '
841 grep "^Subject: =?UTF-8?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
842 '
843
844 +test_expect_success $PREREQ 'utf8 author is correctly passed on' '
845 + clean_fake_sendmail &&
846 + test_commit weird_author &&
847 + test_when_finished "git reset --hard HEAD^" &&
848 + git commit --amend --author "Füñný Nâmé <odd_?=mail@example.com>" &&
849 + git format-patch --stdout -1 >funny_name.patch &&
850 + git send-email --from="Example <nobody@example.com>" \
851 + --to=nobody@example.com \
852 + --smtp-server="$(pwd)/fake.sendmail" \
853 + funny_name.patch &&
854 + grep "^From: Füñný Nâmé <odd_?=mail@example.com>" msgtxt1
855 +'
856 +
857 test_expect_success $PREREQ 'detects ambiguous reference/file conflict' '
858 echo master > master &&
859 git add master &&