Skip to content

fix: avoid UInt32 underflow sizing the address allocator for small subnets#1864

Open
radheradhe01 wants to merge 1 commit into
apple:mainfrom
radheradhe01:fix/subnet-uint32-underflow
Open

fix: avoid UInt32 underflow sizing the address allocator for small subnets#1864
radheradhe01 wants to merge 1 commit into
apple:mainfrom
radheradhe01:fix/subnet-uint32-underflow

Conversation

@radheradhe01

@radheradhe01 radheradhe01 commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Motivation and Context

DefaultNetworkService.init computes the allocatable address count as 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 non-positive count.

This change widens to Int before subtracting, and guards that the resulting size is positive — throwing invalidState for a subnet too small to allocate from, rather than trapping. No change for normally-sized subnets; this only affects degenerate small subnets that previously crashed.

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

Verified by static / code-level review; not built locally (no macOS 26 toolchain available here) — CI build will validate.

### 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.
@radheradhe01 radheradhe01 changed the title Fix UInt32 underflow when sizing the address allocator for small subnets fix: avoid UInt32 underflow sizing the address allocator for small subnets Jun 30, 2026
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