Skip to content

fix: unsafe error handling and allocation crashes#122

Open
hamchapman wants to merge 1 commit intomasterfrom
hc/fix-unsafe-error-handling-and-allocation-crashes
Open

fix: unsafe error handling and allocation crashes#122
hamchapman wants to merge 1 commit intomasterfrom
hc/fix-unsafe-error-handling-and-allocation-crashes

Conversation

@hamchapman
Copy link
Collaborator

@hamchapman hamchapman commented Feb 2, 2026

These changes address multiple critical safety issues:

Issue #118 - Large allocation crash:

  • Replace .map() with progressive array building in readN()
  • Prevents memory allocation crashes from malformed CBOR with huge declared lengths
  • Add capacity reservation capped at reasonable size (1024)

Issue #69 - Array bounds checking:

  • Add bounds validation before accessing data ranges in UnkeyedDecodingContainer
  • Prevents crashes from corrupted data with out-of-bounds ranges
  • Throw proper DecodingError instead of crashing

Unsafe try! replacements:

  • AnyCodingKey: Replace try! with proper error handling for invalid key types
  • KeyedDecodingContainer: Replace try! with proper error propagation
  • KeyedDecodingContainer: Replace fatalError() with throwing DecodingError
  • UnkeyedDecodingContainer: Remove fatalError(), allow errors to propagate
  • AnyCodingKey.encode(): Replace fatalError() with throwing EncodingError
  • AnyCodingKey.key(): Replace fatalError() with preconditionFailure (better messages)

Encoder changes:

  • Make encodeMap() and encodeMapChunk() throw instead of using try!
  • Update call sites with proper error handling
  • Add preconditionFailure for programming errors (forbidNonStringMapKeys violations)

Fixes: #118
Fixes: #69

These changes address multiple critical safety issues:

**Issue #118 - Large allocation crash:**
- Replace `.map()` with progressive array building in `readN()`
- Prevents memory allocation crashes from malformed CBOR with huge declared lengths
- Add capacity reservation capped at reasonable size (1024)

**Issue #69 - Array bounds checking:**
- Add bounds validation before accessing data ranges in `UnkeyedDecodingContainer`
- Prevents crashes from corrupted data with out-of-bounds ranges
- Throw proper `DecodingError` instead of crashing

**Unsafe `try!` replacements:**
- AnyCodingKey: Replace `try!` with proper error handling for invalid key types
- KeyedDecodingContainer: Replace `try!` with proper error propagation
- KeyedDecodingContainer: Replace `fatalError()` with throwing DecodingError
- UnkeyedDecodingContainer: Remove `fatalError()`, allow errors to propagate
- AnyCodingKey.encode(): Replace `fatalError()` with throwing EncodingError
- AnyCodingKey.key(): Replace `fatalError()` with preconditionFailure (better messages)

**Encoder changes:**
- Make `encodeMap()` and `encodeMapChunk()` throw instead of using `try!`
- Update call sites with proper error handling
- Add `preconditionFailure` for programming errors (`forbidNonStringMapKeys` violations)
@hamchapman hamchapman self-assigned this Feb 2, 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

1 participant