]> git.ipfire.org Git - thirdparty/git.git/commitdiff
send-email: capture errors in an eval {} block
authorZheng Yuting <05zyt30@gmail.com>
Wed, 26 Mar 2025 07:52:45 +0000 (15:52 +0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 7 Apr 2025 21:54:05 +0000 (14:54 -0700)
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 <05ZYT30@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-send-email.perl

index 798d59b84f1d6068261d65329ab4dde9b6ff8145..0f05f55e50d23ab91a8915e7d90ea03776f08175 100755 (executable)
@@ -1419,7 +1419,7 @@ sub smtp_auth_maybe {
                die "invalid smtp auth: '${smtp_auth}'";
        }
 
-       # TODO: Authentication may fail not because credentials were
+       # Authentication may fail not because credentials were
        # invalid but due to other reasons, in which we should not
        # reject credentials.
        $auth = Git::credential({
@@ -1431,21 +1431,32 @@ sub smtp_auth_maybe {
                'password' => $smtp_authpass
        }, sub {
                my $cred = shift;
-
-               if ($smtp_auth) {
-                       my $sasl = Authen::SASL->new(
-                               mechanism => $smtp_auth,
-                               callback => {
-                                       user => $cred->{'username'},
-                                       pass => $cred->{'password'},
-                                       authname => $cred->{'username'},
-                               }
-                       );
-
-                       return !!$smtp->auth($sasl);
-               }
-
-               return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
+               my $result;
+               my $error;
+
+               # catch all SMTP auth error in a unified eval block
+               eval {
+                       if ($smtp_auth) {
+                               my $sasl = Authen::SASL->new(
+                                       mechanism => $smtp_auth,
+                                       callback => {
+                                               user     => $cred->{'username'},
+                                               pass     => $cred->{'password'},
+                                               authname => $cred->{'username'},
+                                       }
+                               );
+                               $result = $smtp->auth($sasl);
+                       } else {
+                               $result = $smtp->auth($cred->{'username'}, $cred->{'password'});
+                       }
+                       1; # ensure true value is returned if no exception is thrown
+               } or do {
+                       $error = $@ || 'Unknown error';
+               };
+
+               # NOTE: SMTP status code handling will be added in a subsequent commit,
+               # return 1 when failed due to non-credential reasons
+               return $error ? 1 : ($result ? 1 : 0);
        });
 
        return $auth;