{"record":{"id":"19a8ac82d04a8f2a","repo":"GraphiteEditor/Graphite","slug":"invaliddata","errorCode":"InvalidData","errorMessage":"Invalid Tiff format","messagePattern":"Invalid Tiff format","errorType":"validation","errorClass":"std::io::Error","httpStatus":null,"severity":"error","filePath":"libraries/rawkit/src/tiff/file.rs","lineNumber":16,"sourceCode":"use std::io::{Error, ErrorKind, Read, Result, Seek, SeekFrom};\n\n#[derive(Clone, Copy, PartialEq, Eq, Debug)]\npub enum Endian {\n\tLittle,\n\tBig,\n}\n\npub struct TiffRead<R: Read + Seek> {\n\treader: R,\n\tendian: Endian,\n}\n\nimpl<R: Read + Seek> TiffRead<R> {\n\tpub fn new(mut reader: R) -> Result<Self> {\n\t\tlet error = Error::new(ErrorKind::InvalidData, \"Invalid Tiff format\");\n\n\t\tlet mut data = [0_u8; 2];\n\t\treader.read_exact(&mut data)?;\n\t\tlet endian = if data[0] == 0x49 && data[1] == 0x49 {\n\t\t\tEndian::Little\n\t\t} else if data[0] == 0x4d && data[1] == 0x4d {\n\t\t\tEndian::Big\n\t\t} else {\n\t\t\treturn Err(error);\n\t\t};\n\n\t\treader.read_exact(&mut data)?;\n\t\tlet magic_number = match endian {\n\t\t\tEndian::Little => u16::from_le_bytes(data),\n\t\t\tEndian::Big => u16::from_be_bytes(data),\n\t\t};\n\t\tif magic_number != 42 {\n\t\t\treturn Err(error);","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/GraphiteEditor/Graphite/blob/c507b356453361e31638b8bff8f6d46b6da2961e/libraries/rawkit/src/tiff/file.rs#L1-L34","documentation":"rawkit's TIFF reader validates the two-byte byte-order marker at the start of the stream: 0x49 0x49 (\"II\", little-endian) or 0x4D 0x4D (\"MM\", big-endian). Anything else returns std::io::Error with ErrorKind::InvalidData and this message — the classic \"not actually a TIFF\" guard. A file shorter than two bytes instead fails read_exact with UnexpectedEof, which is a different error.","triggerScenarios":"TiffRead::new() is handed a JPEG/PNG/WebP or arbitrary binary data; a file renamed to .tif/.tiff with different content; a buffer sliced from the wrong offset so the magic bytes are not at position 0.","commonSituations":"Format dispatch based on file extension rather than magic bytes; user-supplied uploads trusted by filename; RAW-processing pipelines where the container type is assumed but the payload is something else.","solutions":["Verify the first bytes: a TIFF starts with II 2A 00 or MM 00 2A","Sniff the real format (e.g. the image crate's guess_format or file(1)) and route to the correct decoder before calling rawkit","Re-export or redownload a genuine TIFF if the file is simply the wrong content"],"exampleFix":"// before\nlet tiff = TiffRead::new(Cursor::new(&bytes))?; // any non-TIFF input errors here\n\n// after: verify the TIFF magic before parsing\nfn is_tiff(buf: &[u8]) -> bool {\n\tbuf.starts_with(b\"II\\x2A\\x00\") || buf.starts_with(b\"MM\\x00\\x2A\")\n}\nif !is_tiff(&bytes) {\n\treturn Err(Error::new(ErrorKind::InvalidData, \"Not a TIFF file\"));\n}\nlet tiff = TiffRead::new(Cursor::new(&bytes))?;","handlingStrategy":"validation","validationCode":"// Read and sniff the header before choosing a decoder\nlet buf = Vec::new();\nfile.read_to_end(&mut buf)?;\nif !(buf.starts_with(b\"II\\x2A\\x00\") || buf.starts_with(b\"MM\\x00\\x2A\")) {\n\treturn Err(Error::new(ErrorKind::InvalidData, \"unsupported image format\"));\n}\nlet tiff = TiffRead::new(Cursor::new(buf))?;","typeGuard":"fn is_tiff(buf: &[u8]) -> bool {\n\tbuf.starts_with(b\"II\\x2A\\x00\") || buf.starts_with(b\"MM\\x00\\x2A\")\n}","tryCatchPattern":"// Distinguish \"wrong format\" from \"truncated file\" when surfacing to users\nmatch err.kind() {\n\tErrorKind::InvalidData => show(\"Unsupported file format\"),\n\tErrorKind::UnexpectedEof => show(\"File is truncated\"),\n\t_ => show(\"Could not read file\"),\n}","preventionTips":["Sniff magic bytes before selecting a decoder; never trust the file extension alone","Handle UnexpectedEof separately — it means truncation, not a wrong format","Use a general format sniffer (e.g. image::guess_format) at the import boundary"],"tags":["tiff","rust","image-decoding","file-format","magic-bytes"],"backgroundTag":"invalid-file-format","analyzedSha":"c507b356453361e31638b8bff8f6d46b6da2961e","analyzedAt":"2026-08-16T21:57:18.596Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}