Skip to content

Fix IPv6 symmetric difference and add IPv6 multi-input tests#55

Merged
ktsaou merged 2 commits into
masterfrom
ipv6-correctness-hardening
Jun 20, 2026
Merged

Fix IPv6 symmetric difference and add IPv6 multi-input tests#55
ktsaou merged 2 commits into
masterfrom
ipv6-correctness-hardening

Conversation

@ktsaou

@ktsaou ktsaou commented Jun 20, 2026

Copy link
Copy Markdown
Member

Summary

Fixes a correctness bug in IPv6 --diff (symmetric difference) and adds tests covering the IPv6 multi-input set operations, which were largely or entirely unexercised.

The bug

--diff is documented as symmetric difference — IPs in either set but not both. In IPv6 mode it wrongly included addresses common to both sets:

A = 2001:db8::1, 2001:db8::2, 2001:db8::5
B = 2001:db8::1, 2001:db8::3
iprange -6 A --diff B
  before:  2001:db8::1  2001:db8::2  2001:db8::3  2001:db8::5   # ::1 is common — wrong
  after:                2001:db8::2  2001:db8::3  2001:db8::5   # correct

The IPv4 path (ipset_diff) handles the same case correctly.

Root cause

In ipset6_diff(), when one range extended past the other, the code emitted the partial-overlap prefix, set the surviving range's lo to hi_other + 1, advanced the consumed index — and then unconditionally reloaded lo/hi for the range that did not advance, clobbering the adjusted lo. The common head of that range was then re-emitted. Both the hi1>hi2 and hi2>hi1 branches had the mirror defect.

The fix mirrors the IPv4 algorithm: keep the survivor's adjusted lo, advance/reload only the consumed range. (hi1>hi2 implies hi2<IPV6_ADDR_MAX, so the increment can't overflow and the previous max-guard is unnecessary.)

Verification

Beyond the new fixed cases, I ran differential/property fuzzing on the IPv6 engine (~25k checks, all passing after the fix):

  • algebraic set identities on random native IPv6 sets — e.g. A△B = (A\\B)∪(B\\A), A∪B = (A∩B)∪(A△B), (A\\B)∩B = ∅;
  • an independent oracle comparing the IPv6 engine against the well-tested IPv4 engine on embedded inputs;
  • binary round-trip and count-unique invariants.

No other correctness issues were found.

Tests added (tests.d/95–100)

Multi-input IPv6 coverage for merge, common, exclude, symmetric difference, count-unique (merged) and compare-first. 98-ipv6-diff specifically guards against the regression (the common address must not appear in the result).

Coverage impact (line coverage)

file before after
ipset6_merge.c 0% 67%
ipset6_diff.c 34% 56%
ipset6_exclude.c 49% 64%
ipset6_common.c 56% 66%
iprange6_main.c 47% 53%

All 100 CLI tests pass.

ktsaou added 2 commits June 20, 2026 23:34
ipset6_diff() reloaded lo/hi for the range that did NOT advance after
emitting a partial overlap, clobbering the deliberately-adjusted lo of
the surviving range. The common head of that range was then re-emitted,
so --diff in IPv6 mode wrongly included addresses present in both sets.

Mirror the proven IPv4 algorithm: when one range extends past the other,
keep the survivor's adjusted lo and only advance/reload the consumed
range. hi1>hi2 implies hi2<IPV6_ADDR_MAX, so the increment cannot
overflow and the previous max-guard is unnecessary.
Cover the IPv6 multi-file paths that had little or no coverage: merge,
common, exclude, symmetric difference, count-unique (merged) and
compare-first. These exercise ipset6_merge (previously unreached),
ipset6_diff, ipset6_exclude and ipset6_common across two inputs.

98-ipv6-diff guards the symmetric-difference regression: the common
address must not appear in the result.
@codacy-production

codacy-production Bot commented Jun 20, 2026

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 duplication

Metric Results
Duplication 0

View in Codacy

🟢 Coverage 50.00% diff coverage · +2.21% coverage variation

Metric Results
Coverage variation +2.21% coverage variation (-1.00%)
Diff coverage 50.00% diff coverage

View coverage diff in Codacy

Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (5ed9649) 3310 2235 67.52%
Head commit (6b32487) 3304 (-6) 2304 (+69) 69.73% (+2.21%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#55) 4 2 50.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@ktsaou ktsaou merged commit 2bb660c into master Jun 20, 2026
17 checks passed
@ktsaou ktsaou deleted the ipv6-correctness-hardening branch June 20, 2026 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant