iterate fixed-size arrays by element, not byte step#61
Merged
gballet merged 2 commits intoApr 22, 2026
Conversation
`deserialize` for `.array` with fixed-size child previously used `i`
both as the element index (`out[i]`, `out.len`) and as the byte stride
(`i += pitch`). For `[N]u8` (pitch=1) the two uses coincide; for any
wider element type only every `pitch`-th output slot was written, and
the interior elements were left undefined:
[3]u16 — writes out[0], out[2]; out[1] never touched
[4]u64 — writes out[0]; out[1..4] never touched
Round-tripping then produced garbage for the unwritten slots.
Step by element instead (`for (0..out.len) |i|`) while keeping
`i * pitch` as the byte slice.
Add regression coverage for three shapes that the existing
`deserializes a Vector[N]` test missed because its element type
(`Pastry` with a `[]const u8` field) is variable-size and therefore
routes through the offset-based decode path:
* `[3]u16` — primitive fixed-size element > 1 byte
* `[4]u64` — widest int
* `[2][4]u32` — nested fixed-size array
There was a problem hiding this comment.
Pull request overview
Fixes a bug in deserialize for fixed-size arrays ([N]T) where the loop index was incorrectly used as both an element index and a byte stride, causing only every pitch-th element to be written for element sizes > 1 byte.
Changes:
- Correct array deserialization for fixed-size element arrays by iterating by element and slicing bytes via
i * pitch. - Add regression tests for fixed-size arrays of wider primitives and nested fixed-size arrays.
- Document the root cause in the deserialization implementation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/lib.zig |
Fixes fixed-size array deserialization iteration to write every element correctly. |
src/tests.zig |
Adds targeted regression tests covering fixed-size array element sizes > 1 and nested fixed-size arrays. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
g11tech
approved these changes
Apr 22, 2026
gballet
approved these changes
Apr 22, 2026
Collaborator
gballet
left a comment
There was a problem hiding this comment.
wow yeah that's a big issue, thanks for catching it.
gballet
reviewed
Apr 22, 2026
gballet
approved these changes
Apr 22, 2026
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.
Bug
`deserialize` for `.array` with fixed-size child uses `i` simultaneously as the element index and as the byte stride:
```zig
var i: usize = 0;
const pitch = try comptime serializedFixedSize(U);
while (i < out.len) : (i += pitch) {
try deserialize(U, serialized[i * pitch .. (i + 1) * pitch], &out[i], allocator);
}
```
`i < out.len` treats `i` as an element index; `i += pitch` treats it as a byte step. For `[N]u8` (`pitch = 1`) the two uses coincide, which is why every existing test passes. For any wider element the loop only writes every `pitch`-th slot — interior elements are left undefined and the roundtrip diverges.
Concrete cases hit while consuming this library from zeam against leanSpec's `test_basic_types` fixtures:
Re-serializing then produces garbage for the unwritten slots and the roundtrip test diverges.
Fix
Step by one element and keep `i * pitch` as the byte slice:
```zig
const pitch = try comptime serializedFixedSize(U);
for (0..out.len) |i| {
try deserialize(U, serialized[i * pitch .. (i + 1) * pitch], &out[i], allocator);
}
```
Why existing tests didn't catch this
The only array-deserialize test, `deserializes a Vector[N]`, uses `[2]Pastry` where `Pastry` contains a `[]const u8` — variable-size. That routes through the offset-based decode branch, not the fixed-size branch this fix lives in.
Added three regression tests covering the previously-untested shape:
Before the fix all three fail (interior elements zero / undefined). After, all 139 tests pass.
Test plan