Repository navigation
Fix "load from misaligned raw pointer" trap in Debug builds - #17
Merged
Merged
Conversation
`load(as:)` traps in Debug when the pointer isn't aligned for the type, which `Data` doesn't guarantee. Use `loadUnaligned(as:)` in `DataSourceTransaction.readInt()` and `Data.scanValue(start:)`. Add a test reading a `UInt32` at an odd offset, and compare ZIP64 version fields against `Archive.Version.v45.rawValue` as upstream does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
|
@mickael-menu the CI run here is waiting for maintainer approval (first-time contributor). Could you approve it and take a look when you get a chance? Context: readium/swift-toolkit#927 |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the changes addressing alignment-related traps in archive-field reads.
No open issues 🎉
What changed in this PR
This PR fixes alignment-related traps when ZIPFoundation reads archive fields in Debug builds, preserving the existing byte interpretation.
Changes:
- Uses
loadUnaligned(as:)for integer reads in both data-reading helpers. - Adds and registers a regression test for reading a
UInt32from an odd-offset data slice. - Corrects ZIP64 assertions to compare serialized version fields against the enum’s raw value.
| File | Description |
|---|---|
Tests/ZIPFoundationTests/ZIPFoundationWritingTests+ZIP64.swift |
Compares ZIP64 version fields against .rawValue. |
Tests/ZIPFoundationTests/ZIPFoundationTests.swift |
Registers the misaligned-read regression test. |
Tests/ZIPFoundationTests/ZIPFoundationDataSerializationTests.swift |
Adds the regression test and a fixed-data transaction helper. |
Sources/ZIPFoundation/DataSource.swift |
Removes the alignment requirement from readInt(). |
Sources/ZIPFoundation/Data+Serialization.swift |
Removes the alignment requirement from scanValue(start:). |
Review effort: Balanced
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mickael-menu
approved these changes
Oct 30, 2026
mickael-menu
left a comment
Member
There was a problem hiding this comment.
Thank you @janmalu, I will publish a patch release right await.
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
Debug builds trap with
Fatal error: load from misaligned raw pointerwhen opening an archive (FileManager.unzipItem→Archive.init→scanForEndOfCentralDirectoryRecord). Seen in an app's Debug build on the iOS 27.0 simulator.Two places read ZIP fields with
load(as:), which requires the pointer to be aligned for the type:DataSource.swift:DataSourceTransaction.readInt()Data+Serialization.swift:Data.scanValue(start:)Datadoesn't guarantee that its bytes are aligned: a slice or small inline storage can start at any address. ZIP fields also sit at arbitrary offsets.Solution
Use
loadUnaligned(as:)in both places. It reads the same bytes and returns the same value, without the alignment precondition. Upstream made the same change toscanValueafter weichsel#106.readInt()exists only in this fork, so merging upstream (#16) would not fix it.Tests:
testReadIntFromMisalignedDatareads aUInt32from aDataslice at an odd offset. It traps ondevelop(macOS, Debug) and passes with this change.testCreateZIP64ArchiveWithLargeSizecomparedscanValuewithArchive.Version.v45, which reads the bytes as the enum's tag rather than its raw value. Ondevelopwith Xcode 27.1 the test process crashes (SIGBUS). The assertion now uses upstream's.rawValue.Testing
swift teston macOS (Xcode 27.1): 110 tests, 0 failures.🤖 Generated with Claude Code