{"record":{"id":"934681244c71f823","repo":"janhq/jan","slug":"string-length-is-unreasonably-large","errorCode":null,"errorMessage":"String length {} is unreasonably large","messagePattern":"String length (.+?) is unreasonably large","errorType":"validation","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"src-tauri/plugins/tauri-plugin-llamacpp/src/gguf/helpers.rs","lineNumber":66,"sourceCode":") -> io::Result<(String, String)> {\n    let key = read_gguf_string(reader).map_err(|e| {\n        io::Error::new(\n            io::ErrorKind::InvalidData,\n            format!(\"Failed to read key for metadata entry {}: {}\", index, e),\n        )\n    })?;\n\n    let value_type_u32 = reader.read_u32::<LittleEndian>()?;\n    let value_type = GgufValueType::try_from(value_type_u32)?;\n    let value = read_gguf_value(reader, value_type)?;\n\n    Ok((key, value))\n}\n\nfn read_gguf_string<R: Read + ReadBytesExt>(reader: &mut R) -> io::Result<String> {\n    let len = reader.read_u64::<LittleEndian>()?;\n    if len > (1024 * 1024) {\n        return Err(io::Error::new(\n            io::ErrorKind::InvalidData,\n            format!(\"String length {} is unreasonably large\", len),\n        ));\n    }\n    let mut buf = vec![0u8; len as usize];\n    reader.read_exact(&mut buf)?;\n    String::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))\n}\n\nfn read_gguf_value<R: Read + Seek + ReadBytesExt>(\n    reader: &mut R,\n    value_type: GgufValueType,\n) -> io::Result<String> {\n    match value_type {\n        GgufValueType::Uint8 => Ok(reader.read_u8()?.to_string()),\n        GgufValueType::Int8 => Ok(reader.read_i8()?.to_string()),\n        GgufValueType::Uint16 => Ok(reader.read_u16::<LittleEndian>()?.to_string()),\n        GgufValueType::Int16 => Ok(reader.read_i16::<LittleEndian>()?.to_string()),","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/janhq/jan/blob/fad3f12a147d138388a66f0d92a02b2675f65294/src-tauri/plugins/tauri-plugin-llamacpp/src/gguf/helpers.rs#L48-L84","documentation":"Returned by `read_gguf_string` when the u64 little-endian length of a GGUF string exceeds 1 MiB (1024 * 1024 bytes). The parser refuses to allocate a buffer of that size as a guard against corrupt length fields that would cause OOM. Real GGUF metadata keys and most string values are well under this bound.","triggerScenarios":"Reading a metadata key or string value whose length field decodes to a value greater than 1,048,576. This typically means the byte stream is misaligned (a previous field was read with the wrong width) or the length bytes themselves are corrupted. It can also fire on a maliciously crafted file designed to OOM the reader, though the cap defeats that.","commonSituations":"Stream misalignment after an array element was skipped with the wrong stride; an endianness mismatch on the length field; a quantized file from a buggy converter that wrote the wrong length. Rarely, a legitimately huge string value (e.g. a tokenizer chat template) could approach 1 MiB, in which case the cap is too tight.","solutions":["Check stream alignment: confirm the immediately preceding value was read with the correct width per the GGUF spec.","If the field is legitimately a large string (chat template, tokenizer data), raise the cap to e.g. 16 MiB and re-test.","Hex-dump the region around the failure to confirm whether the length bytes look like a plausible ASCII-size value or random garbage.","If the file is corrupted, re-download from a trusted source."],"exampleFix":"// before\nif len > (1024 * 1024) {\n    return Err(io::Error::new(InvalidData, format!(\"String length {} is unreasonably large\", len)));\n}\n\n// after - larger cap plus alignment hint in the message\nconst MAX_STR_LEN: u64 = 16 * 1024 * 1024;\nif len > MAX_STR_LEN {\n    let pos = reader.stream_position().unwrap_or(0);\n    return Err(io::Error::new(InvalidData,\n        format!(\"string len {} at byte {} exceeds cap {} (likely misalignment)\", len, pos, MAX_STR_LEN)));\n}","handlingStrategy":"validation","validationCode":"fn plausibly_aligned(reader: &mut impl BufRead) -> bool {\n    // heuristic: peek the next u64 length; if huge, the stream is likely misaligned\n    if let Ok(len) = reader.read_u64::<LittleEndian>() { len <= 1024 * 1024 } else { false }\n}","typeGuard":"null","tryCatchPattern":"match read_gguf_string(reader) {\n    Ok(s) => Ok(s),\n    Err(e) if e.to_string().contains(\"unreasonably large\") => {\n        Err(CorruptFile(\"oversized string length\".into()))\n    }\n    Err(e) => Err(e.into()),\n}","preventionTips":["Validate stream alignment by re-deriving offsets from the spec before reading each field.","Cross-check the file's metadata_count against expected ranges for the model family.","Verify checksums before parsing."],"tags":["gguf","memory","validation","string","llamacpp","rust"],"backgroundTag":null,"analyzedSha":"fad3f12a147d138388a66f0d92a02b2675f65294","analyzedAt":"2026-08-12T20:33:47.516Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}