{"record":{"id":"e12ab172df56e6c3","repo":"jdx/mise","slug":"malformed-mach-o-in","errorCode":null,"errorMessage":"malformed Mach-O in {}","messagePattern":"malformed Mach-O in (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/system/packages/brew/macho.rs","lineNumber":74,"sourceCode":"            .position(|w| w == r.placeholder)\n        {\n            out.splice(pos..pos + r.placeholder.len(), r.value.iter().cloned());\n        }\n    }\n    out\n}\n\n/// Patch one 64-bit LE Mach-O slice in place. Returns whether it changed.\nfn patch_slice(slice: &mut [u8], replacements: &[Replacement], path: &Path) -> Result<bool> {\n    if slice.len() < HEADER_SIZE_64 || u32_at(slice, 0) != MH_MAGIC_64_LE {\n        // not a 64-bit LE Mach-O (32-bit or big-endian) — nothing modern on\n        // arm64 macOS; leave it to the caller's generic byte-level pass\n        return Ok(false);\n    }\n    let ncmds = u32_at(slice, 16) as usize;\n    let sizeofcmds = u32_at(slice, 20) as usize;\n    if HEADER_SIZE_64 + sizeofcmds > slice.len() {\n        bail!(\"malformed Mach-O in {}\", path.display());\n    }\n\n    // upper bound for growing the load-command table: the first byte of\n    // section data (everything between sizeofcmds and there is padding)\n    let lc_end = HEADER_SIZE_64 + sizeofcmds;\n    let mut first_data = slice.len();\n    {\n        let mut off = HEADER_SIZE_64;\n        for _ in 0..ncmds {\n            if off + 8 > lc_end {\n                bail!(\"malformed load command table in {}\", path.display());\n            }\n            let cmd = u32_at(slice, off);\n            let cmdsize = u32_at(slice, off + 4) as usize;\n            if cmdsize < 8 || off + cmdsize > lc_end {\n                bail!(\"malformed load command in {}\", path.display());\n            }\n            if cmd == LC_SEGMENT_64 {","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/jdx/mise/blob/533346cc374382b41ec5ff70536252b2e96e725c/src/system/packages/brew/macho.rs#L56-L92","documentation":"This error is thrown when the Mach-O binary slice being patched has a load-command table that extends past the end of the file: header size (32 bytes for MH_MAGIC_64) plus sizeofcmds exceeds the slice length. The patcher refuses to parse or rewrite a truncated/corrupt binary rather than producing a damaged executable.","triggerScenarios":"patch_slice (invoked via patch) reads ncmds at offset 16 and sizeofcmds at offset 20, and bails when HEADER_SIZE_64 + sizeofcmds > slice.len(). Happens when a brew-installed arm64 macOS binary is truncated, corrupted on disk, or the slice passed in is a malformed sub-range.","commonSituations":"Interrupted downloads or installs leaving partial binaries; disk corruption; a fat-binary arch range pointing at wrong offsets; vendored Mach-O files modified by other tools; antivirus/quarantine mangling.","solutions":["Reinstall the affected formula so the binary is re-downloaded intact (e.g. `brew reinstall <formula>`).","Verify the binary with `file` / `codesign -v` and check its size against the bottle's expected artifact.","If this is your own tooling, ensure the slice passed to patch is a complete, untruncated Mach-O image.","Skip patching for binaries that fail validation and report them as corrupt instead of rewriting them."],"exampleFix":"// before: patching a possibly-truncated download\npatch(&mut content, &replacements, &path)?;\n// after: validate length/size first\nlet expected = fs::metadata(&path)?.len();\nif content.len() != expected as usize { anyhow::bail!(\"truncated binary {}; reinstall\", path.display()); }\npatch(&mut content, &replacements, &path)?;","handlingStrategy":"validation","validationCode":"fn validate_macho(content: &[u8]) -> Result<(), String> {\n    if content.len() < 32 { return Err(\"too short\".into()); }\n    let sizeofcmds = u32::from_le_bytes(content[20..24].try_into().unwrap()) as usize;\n    if 32 + sizeofcmds > content.len() { return Err(\"truncated load commands\".into()); }\n    Ok(())\n}","typeGuard":"fn is_intact_file(path: &Path, content: &[u8]) -> bool {\n    std::fs::metadata(path).map(|m| m.len() as usize == content.len()).unwrap_or(false)\n}","tryCatchPattern":null,"preventionTips":["Verify file size against the bottle manifest before patching.","Reinstall binaries that fail `codesign -v` or `otool -l`.","Never patch files that were interrupted mid-download.","Take a backup copy of the binary before any in-place rewrite."],"tags":["macos","binary-parsing","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"}