Skip to content

Commit

Permalink
Fix: fix the code about handling getaddrinfo when disable ipv6 in
Browse files Browse the repository at this point in the history
CPython and enable ipv6 in system.
Related to #5901; `getaddrinfo` will return an `(int, bytes)` tuple, if
CPython could not handle the address family. It will cause a index out
of range error in aiohttp.
  • Loading branch information
Hanaasagi committed Jul 21, 2021
1 parent 60f5727 commit 28c7e14
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES/5901.bugfix
@@ -0,0 +1,4 @@
Fix the error in handling the return value of `getaddrinfo`.
`getaddrinfo` will return an `(int, bytes)` tuple, if CPython could not handle the address family.
It will cause a index out of range error in aiohttp. For example, if user compile CPython with
`--disable-ipv6` option but his system enable the ipv6.
6 changes: 5 additions & 1 deletion aiohttp/resolver.py
Expand Up @@ -37,7 +37,11 @@ async def resolve(

hosts = []
for family, _, proto, _, address in infos:
if family == socket.AF_INET6 and address[3]: # type: ignore[misc]
if (
socket.has_ipv6
and family == socket.AF_INET6
and address[3] # type: ignore[misc]
):
# This is essential for link-local IPv6 addresses.
# LL IPv6 is a VERY rare case. Strictly speaking, we should use
# getnameinfo() unconditionally, but performance makes sense.
Expand Down

0 comments on commit 28c7e14

Please sign in to comment.