fix(Net): IPv6AddressImpl::isLoopback IPv4-mapped check (#5050)#5349
Merged
Conversation
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IPv6AddressImpl::isLoopbackIPv4-mapped path: it read a single byte (s6_addr[6], part of the 0xFFFF prefix word) and AND-ed it with0xFF000000, making the check always false. CodeQL flagged it as "comparison is always the same". Now readss6_addr32[3](the 32-bit IPv4 portion of::ffff:a.b.c.d) and tests the high byte against0x7Ffor the IPv4 loopback range 127/8.testClassification6: the IPv4-mapped loopback test cases declaredip8/ip9but asserted onip3(the::1address from earlier in the function), so the buggy code path was never actually exercised.::ffff:192.168.1.120).Closes #5050.
Test plan
Net-testrunner IPAddressTest-- 16/16 tests pass on macOS arm64