Skip to content

Fix "load from misaligned raw pointer" trap in Debug builds - #17

Merged
mickael-menu merged 2 commits into
readium:developfrom
janmalu:fix/unaligned-raw-loads
Oct 30, 2026
Merged

mickael-menu merged 2 commits into
readium:developfrom
janmalu:fix/unaligned-raw-loads

Conversation

@janmalu

@janmalu janmalu commented Oct 23, 2026

Copy link
Copy Markdown

Problem

Debug builds trap with Fatal error: load from misaligned raw pointer when 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:)

Data doesn'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 to scanValue after weichsel#106. readInt() exists only in this fork, so merging upstream (#16) would not fix it.

Tests:

  • New testReadIntFromMisalignedData reads a UInt32 from a Data slice at an odd offset. It traps on develop (macOS, Debug) and passes with this change.
  • testCreateZIP64ArchiveWithLargeSize compared scanValue with Archive.Version.v45, which reads the bytes as the enum's tag rather than its raw value. On develop with Xcode 27.1 the test process crashes (SIGBUS). The assertion now uses upstream's .rawValue.

Testing

  • swift test on macOS (Xcode 27.1): 110 tests, 0 failures.
  • In an app's Debug build on the iOS 27.0 simulator, unzipping an EPUB no longer traps.

🤖 Generated with Claude Code

`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>
@janmalu

janmalu commented Oct 30, 2026

Copy link
Copy Markdown
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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 UInt32 from 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 mickael-menu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @janmalu, I will publish a patch release right await.

@mickael-menu
mickael-menu merged commit 2aef2aa into readium:develop Oct 30, 2026
2 checks passed
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.

3 participants