Subject: bug#68376: 30.0.50; auth-source-search doesn't support numeric port on macOS

From: Michael Albinus <[email-masked]>
Date: Thu, 11 Jan 2024 11:12:17 +0100

"Kirill A. Korinsky" <[email-masked]> writes:

> Greeting,

Hi Kirill,

> I'd like to share a bug with simple reproducer:
>
> (auth-source-search :host "some.host" :port "123" :user "user")
> (auth-source-search :host "some.host" :port 123 :user "user")
>
> The first line works as expected, and the second one fails as:
>
> Debugger entered--Lisp error: (wrong-type-argument stringp 123)
>   call-process("/usr/bin/security" nil t nil "find-generic-password"
> "-g" "-c" "some.host" "-a" "user" "-s" 123)

call-process requires strings as arguments. This must be converted
in auth-source-macos-keychain-search-items if needed.

Could you, please, check whether the appended patch works? I cannot test
myself, I have no macOS machine.

Best regards, Michael.

diff --git a/lisp/auth-source.el b/lisp/auth-source.el
index 369cf4dca2e..cf93cb05fba 100644
--- a/lisp/auth-source.el
+++ b/lisp/auth-source.el
@@ -1946,18 +1946,20 @@ auth-source-macos-keychain-search
          (returned-keys (delete-dups (append
 				      '(:host :login :port :secret)
 				      search-keys)))
-         ;; Extract host and port from spec
+         ;; Extract host, port and user from spec
          (hosts (plist-get spec :host))
-         (hosts (if (and hosts (listp hosts)) hosts `(,hosts)))
+         (hosts (if (consp hosts) hosts `(,hosts)))
          (ports (plist-get spec :port))
-         (ports (if (and ports (listp ports)) ports `(,ports)))
+         (ports (if (consp ports) ports `(,ports)))
          (users (plist-get spec :user))
-         (users (if (and users (listp users)) users `(,users)))
+         (users (if (consp users) users `(,users)))
          ;; Loop through all combinations of host/port and pass each of these to
-         ;; auth-source-macos-keychain-search-items
+         ;; auth-source-macos-keychain-search-items.  Convert numeric port to
+         ;; string (bug#68376).
          (items (catch 'match
                   (dolist (host hosts)
                     (dolist (port ports)
+                      (when (numberp port) (setq port (number-to-string port)))
                       (dolist (user users)
                         (let ((items (apply
                                       #'auth-source-macos-keychain-search-items
@@ -2019,7 +2021,7 @@ auth-source-macos-keychain-search-items
     (when port
       (if keychain-generic
           (setq args (append args (list "-s" port)))
-        (setq args (append args (if (string-match "[0-9]+" port)
+        (setq args (append args (if (string-match-p "\\`[[:digit:]]+\\'" port)
                                     (list "-P" port)
                                   (list "-r" (substring
                                               (format "%-4s" port)