Skip to content

Commit e431225

Browse files
Krzysztof Mazurgitster
authored andcommitted
git-send-email: remove invalid addresses earlier
Some addresses are passed twice to unique_email_list() and invalid addresses may be reported twice per send_message. Now we warn about them earlier and we also remove invalid addresses. This also removes using of undefined values for string comparison for invalid addresses in cc list processing. Signed-off-by: Krzysztof Mazur <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 95c0d4b commit e431225

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

git-send-email.perl

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -786,9 +786,11 @@ sub expand_one_alias {
786786
}
787787

788788
@initial_to = expand_aliases(@initial_to);
789-
@initial_to = (map { sanitize_address($_) } @initial_to);
789+
@initial_to = validate_address_list(sanitize_address_list(@initial_to));
790790
@initial_cc = expand_aliases(@initial_cc);
791+
@initial_cc = validate_address_list(sanitize_address_list(@initial_cc));
791792
@bcclist = expand_aliases(@bcclist);
793+
@bcclist = validate_address_list(sanitize_address_list(@bcclist));
792794

793795
if ($thread && !defined $initial_reply_to && $prompting) {
794796
$initial_reply_to = ask(
@@ -839,6 +841,28 @@ sub extract_valid_address {
839841
return undef;
840842
}
841843

844+
sub extract_valid_address_or_die {
845+
my $address = shift;
846+
$address = extract_valid_address($address);
847+
die "error: unable to extract a valid address from: $address\n"
848+
if !$address;
849+
return $address;
850+
}
851+
852+
sub validate_address {
853+
my $address = shift;
854+
if (!extract_valid_address($address)) {
855+
print STDERR "W: unable to extract a valid address from: $address\n";
856+
return undef;
857+
}
858+
return $address;
859+
}
860+
861+
sub validate_address_list {
862+
return (grep { defined $_ }
863+
map { validate_address($_) } @_);
864+
}
865+
842866
# Usually don't need to change anything below here.
843867

844868
# we make a "fake" message id by taking the current number
@@ -955,6 +979,10 @@ sub sanitize_address {
955979

956980
}
957981

982+
sub sanitize_address_list {
983+
return (map { sanitize_address($_) } @_);
984+
}
985+
958986
# Returns the local Fully Qualified Domain Name (FQDN) if available.
959987
#
960988
# Tightly configured MTAa require that a caller sends a real DNS
@@ -1017,14 +1045,13 @@ sub maildomain {
10171045

10181046
sub send_message {
10191047
my @recipients = unique_email_list(@to);
1020-
@cc = (grep { my $cc = extract_valid_address($_);
1048+
@cc = (grep { my $cc = extract_valid_address_or_die($_);
10211049
not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
10221050
}
1023-
map { sanitize_address($_) }
10241051
@cc);
10251052
my $to = join (",\n\t", @recipients);
10261053
@recipients = unique_email_list(@recipients,@cc,@bcclist);
1027-
@recipients = (map { extract_valid_address($_) } @recipients);
1054+
@recipients = (map { extract_valid_address_or_die($_) } @recipients);
10281055
my $date = format_2822_time($time++);
10291056
my $gitversion = '@@GIT_VERSION@@';
10301057
if ($gitversion =~ m/..GIT_VERSION../) {
@@ -1267,7 +1294,7 @@ sub send_message {
12671294
foreach my $addr (parse_address_line($1)) {
12681295
printf("(mbox) Adding to: %s from line '%s'\n",
12691296
$addr, $_) unless $quiet;
1270-
push @to, sanitize_address($addr);
1297+
push @to, $addr;
12711298
}
12721299
}
12731300
elsif (/^Cc:\s+(.*)$/) {
@@ -1376,6 +1403,9 @@ sub send_message {
13761403
($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
13771404
$needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
13781405

1406+
@to = validate_address_list(sanitize_address_list(@to));
1407+
@cc = validate_address_list(sanitize_address_list(@cc));
1408+
13791409
@to = (@initial_to, @to);
13801410
@cc = (@initial_cc, @cc);
13811411

@@ -1431,14 +1461,10 @@ sub unique_email_list {
14311461
my @emails;
14321462

14331463
foreach my $entry (@_) {
1434-
if (my $clean = extract_valid_address($entry)) {
1435-
$seen{$clean} ||= 0;
1436-
next if $seen{$clean}++;
1437-
push @emails, $entry;
1438-
} else {
1439-
print STDERR "W: unable to extract a valid address",
1440-
" from: $entry\n";
1441-
}
1464+
my $clean = extract_valid_address_or_die($entry);
1465+
$seen{$clean} ||= 0;
1466+
next if $seen{$clean}++;
1467+
push @emails, $entry;
14421468
}
14431469
return @emails;
14441470
}

0 commit comments

Comments
 (0)