comfy.utils.load_safetensors() (used by VAELoader and other loaders when
the DynamicVRAM/aimdo backend is active) never validated that the
memory-mapped file actually contained as many bytes as the safetensors
header declared. When a .safetensors file was truncated -- e.g. still
downloading, or an interrupted/partial download -- Python's silent slice
clipping handed torch.frombuffer() a short buffer, producing the
inscrutable low-level error:
ValueError: buffer length (N bytes) after offset (0 bytes) must be
a multiple of element size (4)
instead of the clear "corrupt/incomplete file" message already produced
by the sibling (non-aimdo) safetensors.safe_open() code path for the same
underlying condition.
This adds an explicit bounds check against the header's declared data
size and raises the same friendly, actionable ValueError used elsewhere
in load_torch_file() for corrupt/incomplete files.
Fixes#14784 (reported by xnx20050723-lgtm).
Since comfy_aimdo's ModelMMAP requires a compiled, GPU-platform-specific
native backend unavailable in CI/dev environments without a real GPU, the
added test monkeypatches ModelMMAP with a fake backed by a real,
synthetic, truncated safetensors buffer, letting
comfy.utils.load_safetensors() itself be exercised deterministically.
Verified revert-proof: reverting the fix reproduces the exact reported
error string, `buffer length (37 bytes) after offset (0 bytes) must be a
multiple of element size (4)`.