{"record":{"id":"8ad53328e1d7998e","repo":"jdx/mise","slug":"malformed-fat-arch-in","errorCode":null,"errorMessage":"malformed fat arch in {}","messagePattern":"malformed fat arch in (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/system/packages/brew/macho.rs","lineNumber":193,"sourceCode":"        return Ok(false);\n    }\n    let be_magic = u32::from_be_bytes(content[..4].try_into().unwrap());\n    if be_magic == FAT_MAGIC_BE {\n        let nfat = u32::from_be_bytes(content[4..8].try_into().unwrap()) as usize;\n        let mut changed = false;\n        // collect slice ranges first (fat headers are big-endian)\n        let mut ranges = vec![];\n        for i in 0..nfat {\n            let entry = 8 + i * 20;\n            if entry + 20 > content.len() {\n                bail!(\"malformed fat header in {}\", path.display());\n            }\n            let offset =\n                u32::from_be_bytes(content[entry + 8..entry + 12].try_into().unwrap()) as usize;\n            let size =\n                u32::from_be_bytes(content[entry + 12..entry + 16].try_into().unwrap()) as usize;\n            if offset + size > content.len() {\n                bail!(\"malformed fat arch in {}\", path.display());\n            }\n            ranges.push(offset..offset + size);\n        }\n        for range in ranges {\n            changed |= patch_slice(&mut content[range], replacements, path)?;\n        }\n        Ok(changed)\n    } else {\n        patch_slice(content, replacements, path)\n    }\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n    use crate::system::packages::brew::relocate::tests::test_replacements;\n\n    /// build a minimal 64-bit Mach-O: header + LC_SEGMENT_64 (one section)","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/jdx/mise/blob/533346cc374382b41ec5ff70536252b2e96e725c/src/system/packages/brew/macho.rs#L175-L211","documentation":"Thrown when a fat header's arch entry declares an offset+size range that extends past the end of the file. Each fat_arch entry's slice must lie entirely within the file; otherwise the universal binary is corrupt and patching is refused.","triggerScenarios":"After reading offset and size (big-endian) from the fat_arch entry, `offset + size > content.len()` bails. Caused by stale offsets after files were modified, wrong nfat counts, truncation, or hand-assembled fat binaries with bad entries.","commonSituations":"Binaries rebuilt/concatenated by custom scripts without recomputing offsets; partially downloaded bottles; tools that appended/prepended data without updating the fat table.","solutions":["Reinstall the formula or re-lipo the binary from valid thin slices (`lipo -create`).","Validate each arch slice with `lipo -detailed_info` before patching.","Reject binaries whose fat ranges do not fit within the file.","Rebuild the fat binary with correct offsets using `lipo` instead of manual concatenation."],"exampleFix":"// before: trust offsets\nlet ranges = read_fat_ranges(&content);\n// after: bounds-check each entry\nfor r in read_fat_ranges(&content) {\n    if r.end > content.len() { anyhow::bail!(\"fat arch out of bounds\"); }\n}\nlet ranges = read_fat_ranges(&content);","handlingStrategy":"validation","validationCode":"fn arch_ranges_valid(content: &[u8]) -> bool {\n    let nfat = u32::from_be_bytes(content[4..8].try_into().unwrap()) as usize;\n    (0..nfat).all(|i| {\n        let e = 8 + i * 20;\n        let off = u32::from_be_bytes(content[e+8..e+12].try_into().unwrap()) as usize;\n        let size = u32::from_be_bytes(content[e+12..e+16].try_into().unwrap()) as usize;\n        off + size <= content.len()\n    })\n}","typeGuard":null,"tryCatchPattern":"match patch(&fat, &repl) {\n    Err(e) if format!(\"{e}\").contains(\"malformed fat arch\") => {\n        // rebuild the fat binary from thin slices instead\n        eprintln!(\"bad fat entry; recreate with lipo -create\");\n    }\n    other => other?,\n}","preventionTips":["Use `lipo` to build universal binaries so offsets are recomputed correctly.","Run `lipo -detailed_info` to validate entries before patching.","Reject binaries whose size does not cover all declared arch ranges."],"tags":["macos","fat-binary","corrupt-file"],"backgroundTag":"schema-validation-failed","analyzedSha":"533346cc374382b41ec5ff70536252b2e96e725c","analyzedAt":"2026-09-17T13:35:38.149Z","contentChangedAt":"2026-09-17T13:35:38.149Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}