fix: validate CIDR prefix lengths in match - #212
Merged
whitequark merged 2 commits intoSep 1, 2026
Merged
Conversation
whitequark
reviewed
Sep 1, 2026
whitequark
left a comment
Owner
There was a problem hiding this comment.
Looks OK. Please reformat the tests to make them more readable, like the rest of the file.
Contributor
Author
|
Reformatted the tests for readability and consistency with the rest of the test suite. All 70 tests are passing. |
whitequark
approved these changes
Sep 1, 2026
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
match()did not validate CIDR prefix lengths before passing them tomatchCIDR().Invalid values could therefore produce unexpected behavior:
- negative values and
NaNcould result in an unconditional match;- fractional prefix lengths were accepted with inconsistent matching semantics;
- values above the IPv4/IPv6 prefix range were not rejected;
-
Infinitycould cause non-termination for certain inputs.This also affected
subnetMatch(), since it delegates CIDR comparisons tomatch().## Fix
Validate that
cidrBitsis:- an integer;
- greater than or equal to
0;- less than or equal to the address bit length (
32for IPv4 and128for IPv6).Invalid prefix lengths now throw:
## Tests
Added regression tests covering invalid CIDR prefix lengths for:
- IPv4
match();- IPv6
match();-
subnetMatch().The full test suite passes: