{"record":{"id":"55534a24e2636d0f","repo":"jdx/mise","slug":"malformed-load-command-table-in","errorCode":null,"errorMessage":"malformed load command table in {}","messagePattern":"malformed load command table in (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/system/packages/brew/macho.rs","lineNumber":85,"sourceCode":"        // 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 {\n                let nsects = u32_at(slice, off + 64) as usize;\n                for i in 0..nsects {\n                    // struct section_64 is 80 bytes; offset field at +48\n                    let sect = off + 72 + i * 80;\n                    if sect + 80 > off + cmdsize {\n                        break;\n                    }\n                    let file_off = u32_at(slice, sect + 48) as usize;\n                    if file_off > 0 {\n                        first_data = first_data.min(file_off);\n                    }","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/jdx/mise/blob/533346cc374382b41ec5ff70536252b2e96e725c/src/system/packages/brew/macho.rs#L67-L103","documentation":"Thrown during the validation pass over the Mach-O load-command table when a command header would start or extend beyond lc_end (the end of the command table). It means ncmds and sizeofcmds disagree — the table claims more commands than its declared size can hold.","triggerScenarios":"patch_slice iterates ncmds commands starting at HEADER_SIZE_64; if off + 8 > lc_end for any iteration it bails. Caused by corrupt headers where ncmds was incremented or sizeofcmds truncated, or a slice that matched the 64-bit magic check but is not a valid MH_MAGIC_64 image.","commonSituations":"Hand-edited or tool-mangled binaries; patching a file whose first 32 bytes coincidentally matched the magic check; partially overwritten binaries; wrong arch slice offsets inside fat binaries.","solutions":["Reinstall the formula to restore a pristine binary.","Run `otool -l <binary>` to confirm the load-command table parses with standard tools before patching.","Reject binaries whose ncmds/sizeofcmds are inconsistent instead of patching.","Check whether another post-install step modified the binary before the patcher ran."],"exampleFix":"// before\npatch(&path, &replacements)?;\n// after: sanity-check with otool first\nif !Command::new(\"otool\").args([\"-l\"]).arg(&path).status()?.success() {\n    anyhow::bail!(\"{} has an unparseable load-command table; reinstall\", path.display());\n}\npatch(&path, &replacements)?;","handlingStrategy":"validation","validationCode":"fn table_consistent(content: &[u8]) -> bool {\n    let ncmds = u32::from_le_bytes(content[16..20].try_into().unwrap()) as usize;\n    let sizeofcmds = u32::from_le_bytes(content[20..24].try_into().unwrap()) as usize;\n    32 + ncmds * 8 <= 32 + sizeofcmds\n}","typeGuard":null,"tryCatchPattern":"match patch(&path, &replacements) {\n    Err(e) if e.to_string().contains(\"malformed load command\") => {\n        eprintln!(\"corrupt binary {}; reinstall formula\", path.display());\n    }\n    other => other?,\n}","preventionTips":["Run `otool -l` as a pre-flight parseability check.","Avoid running multiple binary-mangling tools on the same file.","Keep binaries in their original bottle form until patch time."],"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"}