Fix broadcast() corrupting upper 64 bits for IPv4 addresses#30
Open
rustyconover wants to merge 3 commits into
Open
Fix broadcast() corrupting upper 64 bits for IPv4 addresses#30rustyconover wants to merge 3 commits into
rustyconover wants to merge 3 commits into
Conversation
…764349a126f53de0728c9f Apply patches from duckdb/duckdb
The bitwise NOT `~netmask` in Broadcast() flips all 128 bits of the uhugeint_t. For IPv4, the netmask has upper=0 and lower=netmask_value, so `~` sets upper to 0xFFFFFFFFFFFFFFFF. This corrupts the broadcast result, breaking equality comparisons, ordering, and arithmetic. Fix: compute the host mask as `full_mask ^ netmask` where full_mask is the address-space mask (0xFFFFFFFF for IPv4, all-ones for IPv6). This keeps the inversion within the correct address space. For IPv6 the behavior is unchanged since all_ones ^ x == ~x. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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
Broadcast()method uses~Netmask().address(bitwise NOT on 128-bituhugeint_t) to compute the host mask. For IPv4, the netmask hasupper=0, so~0sets the upper 64 bits to0xFFFFFFFFFFFFFFFF. This garbage propagates into the broadcast result.broadcast('192.168.1.0/24') = '192.168.1.255/24'→ false (should be true)broadcast('192.168.1.0/24') > '0.0.0.1'→ false (should be true)broadcast('10.0.0.0/24') + 1→ throws spurious overflow errorToStringIPv4only reads the lower 32 bits, masking the bug from casual observation.full_mask ^ netmaskinstead of~netmask, wherefull_maskis the address-space mask (0xFFFFFFFFfor IPv4, all-ones for IPv6). For IPv6 this is equivalent sinceall_ones ^ x == ~x.Reproduction
Test plan
broadcast(... /24) = '192.168.1.255/24'broadcast(... /24) > '0.0.0.1'/0test🤖 Generated with Claude Code