{"record":{"id":"4d41707d1a0b454e","repo":"huggingface/candle","slug":"gguf-array-of-len-elements-needs-at-least-need","errorCode":null,"errorMessage":"gguf: array of {len} elements needs at least {needed} bytes, only {remaining} remaining","messagePattern":"gguf: array of (.+?) elements needs at least (.+?) bytes, only (.+?) remaining","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/quantized/gguf_file.rs","lineNumber":356,"sourceCode":"            ValueType::F32 => Self::F32(reader.read_f32::<LittleEndian>()?),\n            ValueType::F64 => Self::F64(reader.read_f64::<LittleEndian>()?),\n            ValueType::Bool => match reader.read_u8()? {\n                0 => Self::Bool(false),\n                1 => Self::Bool(true),\n                b => crate::bail!(\"unexpected bool value {b}\"),\n            },\n            ValueType::String => Self::String(read_string(reader, magic, file_size)?),\n            ValueType::Array => {\n                let value_type = reader.read_u32::<LittleEndian>()?;\n                let value_type = ValueType::from_u32(value_type)?;\n                let len = read_length(reader, magic)?;\n                if len > GGUF_MAX_ARRAY_ELEMENTS {\n                    crate::bail!(\"gguf: array length {len} exceeds max {GGUF_MAX_ARRAY_ELEMENTS}\")\n                }\n                let needed = len.saturating_mul(value_type.min_disk_size(magic));\n                let remaining = remaining_bytes(reader, file_size)?;\n                if needed > remaining {\n                    crate::bail!(\n                        \"gguf: array of {len} elements needs at least {needed} bytes, only {remaining} remaining\"\n                    )\n                }\n                let mut vs = Vec::new();\n                for _ in 0..len {\n                    vs.push(Value::read(\n                        reader,\n                        value_type,\n                        magic,\n                        depth + 1,\n                        file_size,\n                    )?)\n                }\n                Self::Array(vs)\n            }\n        };\n        Ok(v)\n    }","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/quantized/gguf_file.rs#L338-L374","documentation":"After reading an array's declared length, Value::read computes the minimum bytes the array must occupy on disk and compares that with the bytes remaining in the file; it bails when the array cannot possibly fit. This catches truncated files and bogus length fields early instead of failing mid-read or over-allocating. It is a size-consistency guard added as hardening.","triggerScenarios":"Reading a truncated/incomplete GGUF download whose header declares more array elements than the file has bytes for; a corrupted length field inflating len; a miscomputed min_disk_size mismatch when reading with older magic versions.","commonSituations":"Interrupted model downloads; partial uploads; storage corruption; reading a file while it is still being written/copied.","solutions":["Re-download the file and compare byte size/checksum with the publisher's.","Check the reported numbers in the message: len, needed bytes vs remaining bytes pinpoint how truncated the file is.","Make sure the file finished copying/downloading before parsing (e.g. verify final size).","If you produce GGUF files yourself, ensure lengths match the actual written elements."],"exampleFix":"// before\nlet content = gguf_file::Content::read(&mut reader)?; // fails on partial file\n// after\nlet expected_size: u64 = 4_618_022_272;\nif std::fs::metadata(path)?.len() < expected_size {\n    bail!(\"GGUF download incomplete\");\n}\nlet content = gguf_file::Content::read(&mut reader)?;","handlingStrategy":"validation","validationCode":"// check completeness before parsing\nlet expected: u64 = /* publisher's byte size */;\nlet actual = std::fs::metadata(path)?.len();\nif actual < expected { bail!(\"incomplete gguf: {} < {} bytes\", actual, expected); }","typeGuard":null,"tryCatchPattern":"match gguf_file::Content::read(&mut reader) {\n    Ok(c) => c,\n    Err(e) if e.to_string().contains(\"only\") && e.to_string().contains(\"remaining\") => {\n        eprintln!(\"GGUF truncated: {e}; re-download the file\");\n        return Err(Error::Msg(\"truncated gguf\".into()));\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Download to a temp file and rename only after completion","Compare final file size against the publisher's expected size","Never parse a GGUF file while a copy/download is still in progress","The error's len/needed/remaining numbers tell you how truncated the file is — log them"],"tags":["candle","gguf","truncated-file","corrupt-file"],"backgroundTag":"malformed-file-data","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}