5 Commits
Author SHA1 Message Date
Nikhil GuptaandRaphael Hiesgen eabcc14f9d Don't trap when QUIC reservedCapacity isn't a valid integer length (#3747)
## Motivation

`QUICBinaryEncodingStrategy.writeInteger(_:reservedCapacity:to:)`
switches on `max(reservedCapacity, bytesNeededForInteger(integer))` and
handles only 1, 2, 4 and 8, falling through to
`fatalError("Unreachable")` otherwise.

`bytesNeededForInteger` only ever returns those four values, but
`reservedCapacity` is guarded solely against being greater than 8. A
reserved capacity of 3, 5, 6 or 7 therefore reaches the unreachable case
and traps:

```swift
var buffer = ByteBuffer()
let strategy = ByteBuffer.QUICBinaryEncodingStrategy.quic
strategy.writeInteger(1, reservedCapacity: 3, to: &buffer)
// NIOCore/ByteBuffer-quicBinaryEncodingStrategy.swift:126: Fatal error: Unreachable
```

This is reachable without calling that method directly.
`requiredBytesHint` is a public `var Int` with no validation — the
initialiser takes the 1/2/4/8 `IntegerLength` enum, but the property can
be assigned any value afterwards, and
`writeLengthPrefixed(strategy:writeData:)` passes it straight through as
the reserved capacity:

```swift
var strategy = ByteBuffer.QUICBinaryEncodingStrategy(requiredBytesHint: .one)
strategy.requiredBytesHint = 3          // public var, accepted silently
var buffer = ByteBuffer()
buffer.writeLengthPrefixed(strategy: strategy) { $0.writeString("hi") }
// Fatal error: Unreachable
```

## Modifications

Round the length up to the next valid QUIC integer length rather than
trapping, which is what the surrounding comment already intends by "use
more space than necessary in order to fill the reserved space":

```diff
-            case 4:
+            case 3, 4:
-            case 8:
+            case 5, 6, 7, 8:
```

RFC 9000 § 16 permits encoding a value in more bytes than it strictly
requires, and `writeLengthPrefixed` already shifts the payload when the
integer turns out longer than was reserved (`case reservedCapacity...`),
so writing 4 bytes for a reservation of 3 is handled by existing code.

## Result

No trap for any `reservedCapacity` in `0...8`, and the encoded value
still round-trips.

I checked every combination of `reservedCapacity` in `0...8` against the
boundary values `0, 1, 63, 64, 16383, 16384, 1073741823, 1073741824,
4611686018427387903`: all 81 encode, decode back to the original value,
and leave no bytes behind. On `main` the same check traps.

## Testing

The existing `testRoundtripWithReservedCapacity` iterates over `[0, 1,
2, 4, 8]` — skipping exactly the values that trap, which is presumably
why this went unnoticed. I've added a companion
`testRoundtripWithNonPowerOfTwoReservedCapacity` covering 3, 5, 6 and 7,
asserting both the encoded length and that the value round-trips. It
fails on `main` and passes with this change.

Full test suite: **2455 tests, 0 failures.**

## A note on the alternative

Rounding *down* to the largest valid length that still fits the
reservation would also avoid the trap and waste fewer bytes — for
`reservedCapacity: 3` that would write 2 bytes rather than 4. I went
with rounding up because the existing comment says to use more space
rather than less, and because it keeps the guarantee that at least the
reserved number of bytes is written. Happy to switch if you'd prefer the
other behaviour.

If you would rather fix this at the source by validating
`requiredBytesHint` or changing its type to `IntegerLength`, that would
be source-breaking, so I left it alone — but the public setter is what
makes this reachable in ordinary use.

---------

Co-authored-by: Raphael Hiesgen <r_hiesgen@apple.com>
2026-10-16 16:54:15 +01:00
Fabian Fett 28b1e07dd8 [Performance] Mark QUICBinaryEncodingStrategy.bytesNeededForInteger as @inlinable (#3718) 2026-09-03 10:40:24 +00:00
jmestwa-coderandCory Benfield 0b38af6217 fix off-by-one boundaries in QUIC bytesNeededForInteger (#3628)
Off-by-one in the QUIC variable-length integer length boundaries.

### Motivation:

`bytesNeededForInteger` classifies an integer into the 1/2/4/8 byte QUIC
encoding using half-open ranges (`0..<63`, `0..<16383`,
`0..<1_073_741_823`, `0..<4_611_686_018_427_387_903`). Those upper
bounds are the *largest* value each size can hold (2^6-1, 2^14-1, 2^30-1
and 2^62-1 per RFC 9000 § 16), so each maximum is wrongly excluded:

- 63, 16383 and 2^30-1 report one byte too many, so the encoder writes
them with a larger encoding than needed.
- 2^62-1, the largest value the encoding can represent, falls through to
the `fatalError` and crashes the encoder.

The existing one-byte write test only iterates `0..<63`, so the 63 case
was never exercised.

### Modifications:

- Switch the four ranges in `bytesNeededForInteger` to inclusive bounds.
- Extend the one-byte write test to include 63, and add boundary checks
plus a 2^62-1 round-trip test.

### Result:

Boundary values encode in the minimal number of bytes and 2^62-1
round-trips instead of crashing.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-06-22 12:45:20 +00:00
Rafael Cepeda 02be63c7f4 Fixes all warnings when -require-explicit-sendable flag is enabled (#3320)
Fixes all warnings when `-require-explicit-sendable` flag is enabled and
enables the flag on macOS CI.

### Motivation:

We want to ensure our public API is either explicitly marked as
`Sendable` or not.

### Modifications:

Marked appropriate public types as `Sendable`, or explicitly defined
their conformance to the `Sendable` protocol as unavailable.

### Result:

We can now enable `-require-explicit-sendable` compiler flag in our
codebase.
2025-07-30 11:50:28 +01:00
hamzahrmalik fb87d5e49e Add functions for reading and writing length-prefixed data with customizable encodings for the length (#2867)
Add functions for reading and writing length-prefixed data with
customizable encodings for the length, particularly for quic
variable-length integers (RFC 9000)

### Motivation:

Many protocols require us to write data and then prefix that data with
its length. But each protocol has a different way of encoding the
length. This PR introduces general purpose functions which can be
extended for different encoding strategies

### Modifications:

Create a new protocol which defines how to encode an integer
Implement this protocol for QUIC

Provide functions on bytebuffer for writing length-prefixed buffers,
strings or bytes
2024-10-02 09:54:42 +00:00