Skip to content

fix(Net): IPv6AddressImpl::isLoopback IPv4-mapped check (#5050)#5349

Merged
matejk merged 2 commits into
mainfrom
5050-codeql-ipv6-loopback
May 11, 2026
Merged

fix(Net): IPv6AddressImpl::isLoopback IPv4-mapped check (#5050)#5349
matejk merged 2 commits into
mainfrom
5050-codeql-ipv6-loopback

Conversation

@matejk
Copy link
Copy Markdown
Contributor

@matejk matejk commented May 11, 2026

Summary

  • Fix IPv6AddressImpl::isLoopback IPv4-mapped path: it read a single byte (s6_addr[6], part of the 0xFFFF prefix word) and AND-ed it with 0xFF000000, making the check always false. CodeQL flagged it as "comparison is always the same". Now reads s6_addr32[3] (the 32-bit IPv4 portion of ::ffff:a.b.c.d) and tests the high byte against 0x7F for the IPv4 loopback range 127/8.
  • Fix pre-existing copy/paste bugs in testClassification6: the IPv4-mapped loopback test cases declared ip8/ip9 but asserted on ip3 (the ::1 address from earlier in the function), so the buggy code path was never actually exercised.
  • Add a non-loopback IPv4-mapped regression guard (::ffff:192.168.1.120).

Closes #5050.

Test plan

  • Net-testrunner IPAddressTest -- 16/16 tests pass on macOS arm64
  • CI green on Linux/Windows

The IPv4-mapped path read a single byte (s6_addr[6], which is part of
the 0xFFFF prefix word) and AND-ed it with 0xFF000000, so the check
was always false. CodeQL flagged this as "comparison is always the same".

For an IPv4-mapped IPv6 address (::ffff:a.b.c.d), the IPv4 octets live
in s6_addr32[3]. Read that 32-bit word in network byte order and check
the high byte against 0x7F to detect the IPv4 loopback range 127/8.

Also fixed pre-existing copy/paste bugs in the IPv4-mapped loopback
test cases (testClassification6) which asserted on the wrong variable
(ip3 instead of ip8/ip9), so the buggy code was never exercised. Added
a non-loopback IPv4-mapped case as a regression guard.
@matejk matejk added this to the Release 1.15.3 milestone May 11, 2026
The previous fix used `_addr.s6_addr32[3]` to read the IPv4 portion of
an IPv4-mapped IPv6 address. That field is a Linux/BSD/macOS extension
and does not exist in Windows' `in6_addr`, breaking the
windows-2025-clang-cmake CI build:

  IPAddressImpl.cpp(573): error: no member named 's6_addr32' in 'in6_addr'

Use the same `reinterpret_cast<const UInt16*>(&_addr)` pattern that
the rest of this file already relies on, then check the high byte of
words[6] (which holds the (a, b) octets in network byte order). The
loopback condition becomes:

  (ByteOrder::fromNetwork(words[6]) & 0xFF00) == 0x7F00

Same semantics, portable to all supported platforms.
@matejk matejk merged commit 11450e0 into main May 11, 2026
52 checks passed
@matejk matejk deleted the 5050-codeql-ipv6-loopback branch May 11, 2026 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CodeQL: Net: Comparison result is always the same

1 participant