Repository navigation
fix(core): refuse malformed safetensors entries - #460
Merged
Merged
Conversation
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. |
8 tasks done
BlivionIaG
pushed a commit
to BlivionIaG/TensorFold
that referenced
this pull request
Oct 20, 2026
# Conflicts: # zig/src/core/safetensors.zig
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.
What this changes
parseHeaderinzig/src/core/safetensors.zignow validates tensor fields and checks byte-count multiplication, returningBadSafetensorsfor malformed entries that previously panicked in Debug and were undefined behavior in the default ReleaseFast build. For example, in ReleaseFast aU8tensor with shape[4611686018427387904, 4]anddata_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 aszig/src/cluster/checkpoint.zig.UnsupportedDTypeandRankTooHighare unchanged, and__metadata__is still skipped. This PR changes only the core parser; the separate parser inzig/src/cluster/checkpoint.zigstill has an unchecked dimension product.Receipt
db281878, fixdaa8e29a, Zig 0.17.0, Linux x86_64 under WSL2.zig build testfrom 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 ReleaseFastflashnext_hosttests fail because the parser accepts the overflowing entry. After the fix, both pass.TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bitatd9d758fb(763 entries) parse identically before and after the change in Debug and ReleaseFast.zig fmt --checkpasses. The core tests compile foraarch64-macoswith--test-no-exec; this is a compile check, not a macOS test run.