Skip to content

iterate fixed-size arrays by element, not byte step#61

Merged
gballet merged 2 commits into
blockblaz:masterfrom
GrapeBaBa:fix/array-deserialize-element-index
Apr 22, 2026
Merged

iterate fixed-size arrays by element, not byte step#61
gballet merged 2 commits into
blockblaz:masterfrom
GrapeBaBa:fix/array-deserialize-element-index

Conversation

@GrapeBaBa
Copy link
Copy Markdown
Member

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:

type pitch writes leaves undefined
`[3]u16` 2 `out[0]`, `out[2]` `out[1]`
`[4]u64` 8 `out[0]` `out[1..4]`
`[2][4]u32` 16 `out[0]` `out[1]`

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:

  • `[3]u16` — primitive fixed-size element > 1 byte
  • `[4]u64` — widest primitive int
  • `[2][4]u32` — nested fixed-size array

Before the fix all three fail (interior elements zero / undefined). After, all 139 tests pass.

Test plan

  • `zig build test --summary all` — 139/139 pass (was 136/139 with new regression tests on unfixed HEAD).

`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
@GrapeBaBa GrapeBaBa requested a review from gballet as a code owner April 21, 2026 06:58
Copilot AI review requested due to automatic review settings April 21, 2026 06:58
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

@gballet gballet changed the title fix(deserialize): iterate fixed-size arrays by element, not byte step iterate fixed-size arrays by element, not byte step Apr 22, 2026
Copy link
Copy Markdown
Collaborator

@gballet gballet left a comment

Choose a reason for hiding this comment

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

wow yeah that's a big issue, thanks for catching it.

Comment thread src/lib.zig Outdated
@gballet gballet merged commit 4047628 into blockblaz:master Apr 22, 2026
3 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.

4 participants