Skip to content

fix(core): refuse malformed safetensors entries - #460

Merged
ashhart merged 1 commit into
ashhart:zig-flashnextfrom
chaog992:safetensors-header-checks
Oct 7, 2026
Merged

ashhart merged 1 commit into
ashhart:zig-flashnextfrom
chaog992:safetensors-header-checks

Conversation

@chaog992

@chaog992 chaog992 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

parseHeader in zig/src/core/safetensors.zig now validates tensor fields and checks byte-count multiplication, returning BadSafetensors for malformed entries that previously panicked in Debug and were undefined behavior in the default ReleaseFast build. For example, in ReleaseFast a U8 tensor with shape [4611686018427387904, 4] and data_offsets [0, 0] was accepted as empty because its byte count wrapped to zero.

The parser rejects non-object roots or entries, missing or incorrectly typed fields, dimensions or offsets that are not non-negative JSON integers representable as usize, offset arrays that do not contain exactly two values, and byte-count overflow. Offset arrays with more than two values, which the old parser read by their first two, are now refused. Integer parsing follows the same rule as zig/src/cluster/checkpoint.zig.

UnsupportedDType and RankTooHigh are unchanged, and __metadata__ is still skipped. This PR changes only the core parser; the separate parser in zig/src/cluster/checkpoint.zig still has an unchecked dimension product.

Receipt

  • Environment: base db281878, fix daa8e29a, Zig 0.17.0, Linux x86_64 under WSL2.
  • Tests: zig build test from a clean cache: 19/19 steps, 73 passed, 0 failed, 0 skipped. The new test checks twelve malformed headers. With the new test applied before the fix, the Debug core tests fail with integer overflow, and the ReleaseFast flashnext_host tests fail because the parser accepts the overflowing entry. After the fix, both pass.
  • Valid-header compatibility: Five headers from TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit at d9d758fb (763 entries) parse identically before and after the change in Debug and ReleaseFast.
  • Format and cross-compilation: zig fmt --check passes. The core tests compile for aarch64-macos with --test-no-exec; this is a compile check, not a macOS test run.
  • Not run: full model loads, Metal/CUDA inference, token parity and decode/prefill benchmarks. The change does not touch inference arithmetic.

@ashhart
ashhart merged commit 7ae6df7 into ashhart:zig-flashnext Oct 7, 2026
@ashhart

ashhart commented Oct 7, 2026

Copy link
Copy Markdown
Owner

Thank you, @chaog992. This is landed on the Zig line under your name (merge 7ae6df7, now on zig-flashnext and zig-preview). The wrapped byte count was a real hole: a U8 tensor whose shape multiplies past the width of a usize, with data offsets of 0 and 0, is now refused, along with a non-object header, a missing field, a negative dimension, and an offset list that is not two non-negative integers.

Your host test runs in the suite on the merged tip (98 of 98 host tests pass), next to the namespace selection that arrived from #435 in the same file. It changes no inference arithmetic.

The cluster checkpoint parser is a separate reader with its own unchecked product. That one stays open as follow-up work.

@chaog992
chaog992 deleted the safetensors-header-checks branch October 7, 2026 15:38
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
# Conflicts:
#	zig/src/core/safetensors.zig
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.

2 participants