jdx/mise · error

malformed Mach-O in

Error message

malformed Mach-O in {}

What it means

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.

Solutions

  1. Reinstall the affected formula so the binary is re-downloaded intact (e.g. `brew reinstall <formula>`).
  2. Verify the binary with `file` / `codesign -v` and check its size against the bottle's expected artifact.
  3. If this is your own tooling, ensure the slice passed to patch is a complete, untruncated Mach-O image.
  4. Skip patching for binaries that fail validation and report them as corrupt instead of rewriting them.

Example fix

// before: patching a possibly-truncated download
patch(&mut content, &replacements, &path)?;
// after: validate length/size first
let expected = fs::metadata(&path)?.len();
if content.len() != expected as usize { anyhow::bail!("truncated binary {}; reinstall", path.display()); }
patch(&mut content, &replacements, &path)?;
Defensive patterns

Strategy: validation

Validate before calling

fn validate_macho(content: &[u8]) -> Result<(), String> {
    if content.len() < 32 { return Err("too short".into()); }
    let sizeofcmds = u32::from_le_bytes(content[20..24].try_into().unwrap()) as usize;
    if 32 + sizeofcmds > content.len() { return Err("truncated load commands".into()); }
    Ok(())
}

Type guard

fn is_intact_file(path: &Path, content: &[u8]) -> bool {
    std::fs::metadata(path).map(|m| m.len() as usize == content.len()).unwrap_or(false)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of jdx/mise@533346cc37 (2026-09-17). Data as JSON: /api/errors/e12ab172df56e6c3. Report an issue: GitHub.

Appendix: source

Thrown at src/system/packages/brew/macho.rs:74

            .position(|w| w == r.placeholder)
        {
            out.splice(pos..pos + r.placeholder.len(), r.value.iter().cloned());
        }
    }
    out
}

/// Patch one 64-bit LE Mach-O slice in place. Returns whether it changed.
fn patch_slice(slice: &mut [u8], replacements: &[Replacement], path: &Path) -> Result<bool> {
    if slice.len() < HEADER_SIZE_64 || u32_at(slice, 0) != MH_MAGIC_64_LE {
        // not a 64-bit LE Mach-O (32-bit or big-endian) — nothing modern on
        // arm64 macOS; leave it to the caller's generic byte-level pass
        return Ok(false);
    }
    let ncmds = u32_at(slice, 16) as usize;
    let sizeofcmds = u32_at(slice, 20) as usize;
    if HEADER_SIZE_64 + sizeofcmds > slice.len() {
        bail!("malformed Mach-O in {}", path.display());
    }

    // upper bound for growing the load-command table: the first byte of
    // section data (everything between sizeofcmds and there is padding)
    let lc_end = HEADER_SIZE_64 + sizeofcmds;
    let mut first_data = slice.len();
    {
        let mut off = HEADER_SIZE_64;
        for _ in 0..ncmds {
            if off + 8 > lc_end {
                bail!("malformed load command table in {}", path.display());
            }
            let cmd = u32_at(slice, off);
            let cmdsize = u32_at(slice, off + 4) as usize;
            if cmdsize < 8 || off + cmdsize > lc_end {
                bail!("malformed load command in {}", path.display());
            }
            if cmd == LC_SEGMENT_64 {

View on GitHub (pinned to 533346cc37)