Subject: Re: bug#74626: 30.0.91; mini buffer password prompt mismatches shell buffer's

From: Michael Albinus <[email-masked]>
Date: Sun, 01 Dec 2024 16:44:37 +0100

Van Ly via "Bug reports for GNU Emacs, the Swiss army knife of text
editors" <[email-masked]> writes:

> Hello,

Hi,

> When using ssh to connect to a machine, the password prompt is expected
> to be identical in the shell buffer and mini buffer.
>
> The mini buffer's prompt seems to make use of only the last character.
>
> See
>
>   http://sdf.org/~van.ly/img/emacs-30-0-91-password-prompt.webp
>
> The shell buffer's password prompt shows
>
>   [email-masked]'s password:
>
> The mini buffer shows
>
>   4's password:

Thanks for the report.

I can reproduce the problem. In fact, there are two issues:

- comint-password-prompt-regexp uses "'s" in order to search for a password
  prompt. That's fine for password prompts like "[email-masked]'s
  password:", however, everything before this "'s" is ignored when the
  minibuffer prompt is assembled.

- The string the password prompt is looked for is "[email-masked]'s
  password:". In comint-watch-for-password-prompt, the "" is removed,
  but while searching for the password prompt. As a result,
  (match-string 0 string) returns a wrong substring (just one position
  left).

The problem does not happen with Emacs 29. The culprit seems to be
commit 1a55e957ae57, which has changed the implementation of
comint-watch-for-password-prompt. The appended patch fixes this for me
in Emacs 30. Could you, please, check?

Jim, do you have a comment?

Best regards, Michael.

diff --git a/lisp/comint.el b/lisp/comint.el
index 8860b3edb11..8b4ab2f9ea1 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -404,7 +404,7 @@ comint-password-prompt-regexp
    (regexp-opt
     '("Enter" "enter" "Enter same" "enter same" "Enter the" "enter the"
       "Current"
-      "Enter Auth" "enter auth" "Old" "old" "New" "new" "'s" "login"
+      "Enter Auth" "enter auth" "Old" "old" "New" "new" "login"
       "Kerberos" "CVS" "UNIX" " SMB" "LDAP" "PEM" "SUDO"
       "[sudo]" "doas" "Repeat" "Bad" "Retype" "Verify")
     t)
@@ -418,11 +418,13 @@ comint-password-prompt-regexp
    ;; The ccrypt encryption dialog doesn't end with a colon, so
    ;; treat it specially.
    "\\|^Enter encryption key: (repeat) *\\'"
+   ;; Default openssh format: "user@host's password:".
+   "\\|^[^@ \t\n]+@[^@ \t\n]+'s password: *\\'"
    ;; openssh-8.6p1 format: "(user@host) Password:".
    "\\|^([^)@ \t\n]+@[^)@ \t\n]+) Password: *\\'")
   "Regexp matching prompts for passwords in the inferior process.
 This is used by `comint-watch-for-password-prompt'."
-  :version "29.1"
+  :version "30.1"
   :type 'regexp
   :group 'comint)

@@ -2568,11 +2570,12 @@ comint-watch-for-password-prompt
 carriage returns (\\r) in STRING.

 This function could be in the list `comint-output-filter-functions'."
-  (let ((string (string-limit string comint-password-prompt-max-length t))
+  (let ((string (string-limit
+                 (string-replace "\r" "" string)
+                 comint-password-prompt-max-length t))
         prompt)
     (when (let ((case-fold-search t))
-            (string-match comint-password-prompt-regexp
-                          (string-replace "\r" "" string)))
+            (string-match comint-password-prompt-regexp string))
       (setq prompt (string-trim (match-string 0 string)
                                 "[ \n\r\t\v\f\b\a]+" "\n+"))
       ;; Use `run-at-time' in order not to pause execution of the