Skip to content

Commit ce20dec

Browse files
Zheng Yutinggitster
authored andcommitted
send-email: capture errors in an eval {} block
Auth relied solely on return values without catching errors. This misjudges non-credential errors as auth failure without error info. Patch wraps the entire auth process in an eval {} block to catch all exceptions, including non-credential errors. It adds a new $error var, uses 'or do' to prevent flow break, and returns $result ? 1 : 0. And merges if/else branches, integrates SASL and basic auth, with comments for future status code handling. Signed-off-by: Zheng Yuting <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 683c54c commit ce20dec

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

git-send-email.perl

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ sub smtp_auth_maybe {
14191419
die "invalid smtp auth: '${smtp_auth}'";
14201420
}
14211421

1422-
# TODO: Authentication may fail not because credentials were
1422+
# Authentication may fail not because credentials were
14231423
# invalid but due to other reasons, in which we should not
14241424
# reject credentials.
14251425
$auth = Git::credential({
@@ -1431,21 +1431,32 @@ sub smtp_auth_maybe {
14311431
'password' => $smtp_authpass
14321432
}, sub {
14331433
my $cred = shift;
1434-
1435-
if ($smtp_auth) {
1436-
my $sasl = Authen::SASL->new(
1437-
mechanism => $smtp_auth,
1438-
callback => {
1439-
user => $cred->{'username'},
1440-
pass => $cred->{'password'},
1441-
authname => $cred->{'username'},
1442-
}
1443-
);
1444-
1445-
return !!$smtp->auth($sasl);
1446-
}
1447-
1448-
return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1434+
my $result;
1435+
my $error;
1436+
1437+
# catch all SMTP auth error in a unified eval block
1438+
eval {
1439+
if ($smtp_auth) {
1440+
my $sasl = Authen::SASL->new(
1441+
mechanism => $smtp_auth,
1442+
callback => {
1443+
user => $cred->{'username'},
1444+
pass => $cred->{'password'},
1445+
authname => $cred->{'username'},
1446+
}
1447+
);
1448+
$result = $smtp->auth($sasl);
1449+
} else {
1450+
$result = $smtp->auth($cred->{'username'}, $cred->{'password'});
1451+
}
1452+
1; # ensure true value is returned if no exception is thrown
1453+
} or do {
1454+
$error = $@ || 'Unknown error';
1455+
};
1456+
1457+
# NOTE: SMTP status code handling will be added in a subsequent commit,
1458+
# return 1 when failed due to non-credential reasons
1459+
return $error ? 1 : ($result ? 1 : 0);
14491460
});
14501461

14511462
return $auth;

0 commit comments

Comments
 (0)