Subject: Re: bug#71972: 31.0.50; Tramp does not parse ipv6 address with port correctly

From: Michael Albinus <[email-masked]>
Date: Sun, 07 Jul 2024 12:28:45 +0200

Yikai Zhao <[email-masked]> writes:

> Hi,

Hi,

> Thanks for the patch. However, with this patch applied to master, the result is:
>
> (tramp-dissect-file-name "/ssh:[2001::abcd]#2202:/tmp/xxx")
> => (tramp-file-name "ssh" nil nil #("Oldtown" 0 7 (tramp-default t))
> "2202" "/tmp/xxx" nil) [2 times]
>
> ("oldtown" is my local hostname). Although the port is correctly
> parsed, the host is now incorrect.

Indeed, we have a race condition. tramp-host-with-port-regexp is defined
before tramp-set-syntax has run.

Could you, please, try the appended patch indeed?

> Also note that in master, the correct file path for tramp.el is
> lisp/net/tramp.el, so I had to manually edit your patch before
> applying it. What version is your patch based on? Maybe the result is
> different in different versions?

I develop in the Tramp git repo. The files are identical with the ones
in the Emacs git repo. However, there is a different directory structure.

Best regards, Michael.

diff --git a/lisp/tramp.el b/lisp/tramp.el
index f97ed66c..f960b1a3 100644
--- a/lisp/tramp.el
+++ b/lisp/tramp.el
@@ -78,6 +78,7 @@
 (defvar tramp-postfix-ipv6-regexp)
 (defvar tramp-postfix-host-format)
 (defvar tramp-postfix-host-regexp)
+(defvar tramp-host-with-port-regexp)
 (defvar tramp-remote-file-name-spec-regexp)
 (defvar tramp-file-name-structure)
 (defvar tramp-file-name-regexp)
@@ -878,6 +879,7 @@ to be set, depending on VALUE."
 	tramp-postfix-ipv6-regexp (tramp-build-postfix-ipv6-regexp)
 	tramp-postfix-host-format (tramp-build-postfix-host-format)
 	tramp-postfix-host-regexp (tramp-build-postfix-host-regexp)
+	tramp-host-with-port-regexp (tramp-build-host-with-port-regexp)
 	tramp-remote-file-name-spec-regexp
 	(tramp-build-remote-file-name-spec-regexp)
 	tramp-file-name-structure (tramp-build-file-name-structure)
@@ -1059,11 +1061,18 @@ Derived from `tramp-prefix-port-format'.")
 (defconst tramp-port-regexp (rx (+ digit))
   "Regexp matching port numbers.")

-(defconst tramp-host-with-port-regexp
+(defun tramp-build-host-with-port-regexp ()
+  "Return `tramp-host-with-port-regexp'."
   (rx
-   (group (regexp tramp-host-regexp))
+   (group (| (regexp tramp-host-regexp)
+	     (: (regexp (tramp-build-prefix-ipv6-regexp))
+		(? (regexp tramp-ipv6-regexp))
+		(regexp (tramp-build-postfix-ipv6-regexp)))))
    (regexp tramp-prefix-port-regexp)
-   (group (regexp tramp-port-regexp)))
+   (group (regexp tramp-port-regexp))))
+
+(defvar tramp-host-with-port-regexp
+  nil ; Initialized when defining `tramp-syntax'!
   "Regexp matching host names with port numbers.")

 (defconst tramp-postfix-hop-format "|"