Fix UInt32 underflow when sizing the address allocator for small subnets#1864
Open
radheradhe01 wants to merge 1 commit into
Open
Fix UInt32 underflow when sizing the address allocator for small subnets#1864radheradhe01 wants to merge 1 commit into
radheradhe01 wants to merge 1 commit into
Conversation
### Problem `DefaultNetworkService.init` computes the allocatable address count as: ```swift let size = Int(subnet.upper.value - subnet.lower.value - 3) ``` `upper.value` and `lower.value` are `UInt32`, so the subtraction happens in `UInt32` **before** the widening to `Int`. For a subnet where `upper - lower < 3` (e.g. a `/31`), this underflows and traps at runtime instead of producing a negative/zero count. ### Fix Widen to `Int` before subtracting, and guard that the resulting size is positive — throwing `invalidState` for a subnet too small to allocate from, rather than trapping. ### Notes No change for normally-sized subnets; this only affects degenerate small subnets that previously crashed.
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.
Problem
DefaultNetworkService.initcomputes the allocatable address count as:upper.valueandlower.valueareUInt32, so the subtraction happens inUInt32before the widening toInt. For a subnet whereupper - lower < 3(e.g. a/31), this underflows and traps at runtime instead of producing a negative/zero count.Fix
Widen to
Intbefore subtracting, and guard that the resulting size is positive — throwinginvalidStatefor a subnet too small to allocate from, rather than trapping.Notes
No change for normally-sized subnets; this only affects degenerate small subnets that previously crashed.