Patchwork [stable] py3: fix crash when server address is 0.0.0.0 (issue6362)

login
register
mail settings
Submitter Manuel Jacob
Date July 3, 2020, 9:26 p.m.
Message ID <e9af049b8e0b0845043f.1593811562@tmp.fritz.box>
Download mbox | patch
Permalink /patch/46617/
State Accepted
Headers show

Comments

Manuel Jacob - July 3, 2020, 9:26 p.m.
# HG changeset patch
# User Manuel Jacob <me@manueljacob.de>
# Date 1593811519 -7200
#      Fri Jul 03 23:25:19 2020 +0200
# Branch stable
# Node ID e9af049b8e0b0845043fc969672763889f8a517a
# Parent  383005aa9cdcc0202af05b97fcb1657d890e56ca
# EXP-Topic issue6362
py3: fix crash when server address is 0.0.0.0 (issue6362)

`socket.getfqdn()` assumes that the name is passed as `str` on Python 3 and
always returns `str` in this case. Mercurial passed `bytes` (but still expected
a `str` result), which worked by chance in many cases, except for e.g.
b'0.0.0.0', which was returned unchanged, breaking later code.

Instead of calling `socket.getfqdn()`, we can also use `self.server_name` from
the base `HTTPServer` class, which already stores the FQDN of the locally-bound
socket name (see `BaseHTTPServer.py` in the Python 2 stdlib and
`http/server.py` in the Python 3 stdlib).
Yuya Nishihara - July 4, 2020, 3:33 a.m.
On Fri, 03 Jul 2020 23:26:02 +0200, Manuel Jacob wrote:
> # HG changeset patch
> # User Manuel Jacob <me@manueljacob.de>
> # Date 1593811519 -7200
> #      Fri Jul 03 23:25:19 2020 +0200
> # Branch stable
> # Node ID e9af049b8e0b0845043fc969672763889f8a517a
> # Parent  383005aa9cdcc0202af05b97fcb1657d890e56ca
> # EXP-Topic issue6362
> py3: fix crash when server address is 0.0.0.0 (issue6362)

Queued for stable, thanks.

Patch

diff --git a/mercurial/hgweb/server.py b/mercurial/hgweb/server.py
--- a/mercurial/hgweb/server.py
+++ b/mercurial/hgweb/server.py
@@ -382,7 +382,7 @@ 
         self.errorlog = elog
 
         self.addr, self.port = self.socket.getsockname()[0:2]
-        self.fqaddr = socket.getfqdn(addr[0])
+        self.fqaddr = self.server_name
 
         self.serverheader = ui.config(b'web', b'server-header')
 
diff --git a/tests/test-serve.t b/tests/test-serve.t
--- a/tests/test-serve.t
+++ b/tests/test-serve.t
@@ -99,4 +99,11 @@ 
   $LOCALIP - - [$LOGDATE$] "GET /some/dir7?cmd=capabilities HTTP/1.1" 404 - (glob)
   $LOCALIP - - [$LOGDATE$] "GET /some?cmd=capabilities HTTP/1.1" 404 - (glob)
 
+  $ "$PYTHON" $RUNTESTDIR/killdaemons.py $DAEMON_PIDS
+
+issue6362: Previously, this crashed on Python 3
+
+  $ hg serve -a 0.0.0.0 -d
+  listening at http://*:$HGPORT1/ (bound to *:$HGPORT1) (glob)
+
   $ cd ..