In a Swift binary parser, why is returning raw.baseAddress from bytes.withUnsafeBytes and reading the header later a bug, and how should the header be read?
answer
- valid for the closure's duration
- no diagnostic, undefined behaviour
- do the reads inside, return values
- alignment: load vs loadUnaligned
basics
~20 sThe buffer pointer that Array.withUnsafeBytes passes in is valid only while the closure runs, so a pointer returned from it dangles. Do every read inside the closure and return plain values, using loadUnaligned for packed fields.
solid answer
~40 s`withUnsafeBytes` lends the closure an `UnsafeRawBufferPointer` whose documented validity is the duration of the closure's execution. After it returns, the storage may be released, moved by the next mutation, or have been a temporary copy, so reading through a returned `baseAddress` is undefined behaviour. The compiler does not diagnose returning it. The parser should check the length first, do all its loads inside the closure, and return a value type such as a `FileHeader` struct. For a packed format use `loadUnaligned(fromByteOffset:as:)`: `load(fromByteOffset:as:)` requires the address to be aligned for the type, and that requirement, like its bounds check, is only a debug precondition, so an `-O` build silently reads whatever is there. Convert byte order explicitly with `UInt32(bigEndian:)`.
code
swift · 8 linesfunc headerPointer(_ bytes: [UInt8]) -> UnsafeRawPointer {
bytes.withUnsafeBytes { raw in raw.baseAddress! } // escapes the closure
}
func readMagic(_ bytes: [UInt8]) -> UInt32 {
let p = headerPointer(bytes)
return UInt32(bigEndian: p.loadUnaligned(as: UInt32.self)) // reads freed or moved memory
}go deeper
Remember that the pointer handed to a withUnsafe closure is only good inside that closure, and that a parser should return values, not pointers.
Explain why the storage can move or disappear after the closure, and how load differs from loadUnaligned and from the platform's byte order.
Spot an escaping pointer in review even though the compiler is silent, and know that alignment and bounds checks on raw loads vanish in -O builds.
Contain unsafe pointer code to small, reviewed helpers that return values, so the rest of a parser works with safe types and the unsafe surface stays countable.
## The contract of `withUnsafeBytes` `Array.withUnsafeBytes(_:)` calls its closure with an **`UnsafeRawBufferPointer`** to the array's contiguous storage. Its documentation is precise about lifetime: *the argument is valid only for the duration of the closure's execution*. If the array has no contiguous storage yet, one is created for the call. So a parser that does this has a bug: ```swift func headerPointer(_ bytes: [UInt8]) -> UnsafeRawPointer { bytes.withUnsafeBytes { raw in raw.baseAddress! } } ``` Once the closure returns, nothing keeps that memory alive or in place: - the array may be released when the caller's last reference goes away; - a later mutation of the array can reallocate or copy its storage; - for an array without native contiguous storage, the buffer was a temporary. Reading through the returned pointer is **undefined behaviour**. It often works in a test, because the memory happens to still hold the bytes, which is what makes it dangerous. ## What the compiler catches and what it does not Swift 6.4 compiles the function above with **no diagnostic**. It does catch a related family: **temporary pointers** created by implicit conversions. `let p = UnsafePointer(&x)` for a local `x`, or `UnsafeRawPointer(bytes)` for an array, produces `warning: initialization of 'UnsafeRawPointer' results in a dangling pointer` because the pointer an `&x` or array argument converts to lives only for that one call. The `TemporaryPointers` diagnostic group notes that the compiler finds some, but not all, of these. ## Reading the header correctly 1. **Check the length first.** `guard bytes.count >= 10 else { return nil }`. The bounds checks inside `UnsafeRawBufferPointer.load` are debug preconditions only. 2. **Do every load inside the closure** and return a value: a `FileHeader` struct of integers carries no pointer, so nothing dangles. 3. **Respect alignment.** `load(fromByteOffset:as:)` requires the address to be aligned for the type. A `UInt32` at offset 6 is not; a debug build traps with `load from misaligned raw pointer`, while an `-O` build has no check and reads it anyway, which is undefined behaviour. **`loadUnaligned(fromByteOffset:as:)`** has no alignment requirement and is the right call for packed file formats. 4. **Fix the byte order.** A load reads the platform's native order. For a big-endian format wrap the value: `UInt32(bigEndian: raw.loadUnaligned(as: UInt32.self))`. ```swift return bytes.withUnsafeBytes { raw in FileHeader( magic: UInt32(bigEndian: raw.loadUnaligned(as: UInt32.self)), version: UInt16(bigEndian: raw.loadUnaligned(fromByteOffset: 4, as: UInt16.self)), recordCount: UInt32(bigEndian: raw.loadUnaligned(fromByteOffset: 6, as: UInt32.self)) ) } ``` ## What "Unsafe" gives up | Guarantee | Safe Swift | Unsafe pointers | |---|---|---| | Lifetime | ARC keeps storage alive | none; you track it | | Bounds | checked, traps | debug precondition at most | | Alignment for `load` | — | debug precondition only | | Exclusivity | enforced statically or at run time | not enforced | | Initialization | guaranteed | your responsibility | The `Unsafe` prefix on `UnsafePointer`, `UnsafeMutablePointer` and `UnsafeRawBufferPointer` marks exactly this: the compiler and runtime stop checking, and the code becomes responsible for each guarantee. ## Reviewing pointer code In review, check each `withUnsafe…` call for these patterns: - the closure returns `baseAddress`, the buffer itself, or anything built from them; - the pointer is stored in a property, a global or a captured variable; - a helper called inside the closure keeps the pointer after it returns; - `load(fromByteOffset:as:)` at an offset nobody proved aligned; - a length check that runs after the loads instead of before them. Each of these compiles cleanly, and each can pass tests: an escaped pointer usually still points at the old bytes until something reuses the memory, and an `-O` build performs a misaligned load without complaint. Debug builds help with alignment and bounds, but only a reviewer catches the escape. ## Safer options - Keep the pointer's whole use inside the closure, including any helper calls. - `withUnsafePointer(to:_:)` gives a scoped pointer to a single variable instead of `UnsafePointer(&x)`. - Since Swift 6.2, an array's `span` property offers a bounds-checked, non-escapable view that the compiler refuses to let escape — the subject of the Span material in this branch. Passing pointers to C functions is part of C interoperability; here the point is only how long a Swift-vended pointer stays valid.
- What happens in Swift when raw.load(fromByteOffset: 6, as: UInt32.self) reads a misaligned address?`load` requires the address to be aligned for `UInt32`. A debug build checks it and traps with `load from misaligned raw pointer`; an `-O` build has no check and performs the load anyway, which is undefined behaviour that may appear to work. `loadUnaligned(fromByteOffset:as:)` is the call with no alignment requirement.
- Why does Swift warn on UnsafePointer(&x) but not on returning baseAddress from withUnsafeBytes?`UnsafePointer(&x)` is an implicit inout-to-pointer conversion whose pointer lives only for that initializer call, a pattern the compiler recognises and flags as a dangling pointer. Returning `baseAddress` is an ordinary value flowing out of a closure; the compiler does not track pointer lifetimes, so it stays silent.
- The pointer stays valid as long as the array variable is in scope.
- load(fromByteOffset:as:) works at any offset because it reads raw bytes.
- Bounds checks on UnsafeRawBufferPointer.load also run in release builds.
- The compiler would warn if a pointer escaped withUnsafeBytes.
- load reads integers in big-endian order, so file headers need no conversion.