{"record":{"id":"f61b50ccda0572a7","repo":"jdx/mise","slug":"malformed-load-command-in","errorCode":null,"errorMessage":"malformed load command in {}","messagePattern":"malformed load command in (.+?)","errorType":"exception","errorClass":"eyre::Report","httpStatus":null,"severity":"error","filePath":"src/system/packages/brew/macho.rs","lineNumber":90,"sourceCode":"    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                    }\n                }\n            }\n            off += cmdsize;\n        }\n    }","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/jdx/mise/blob/9dcfcaa0dc8747a2577d3270b69bb9d8313b2807/src/system/packages/brew/macho.rs#L72-L108","documentation":"A single load command in a 64-bit Mach-O declared a cmdsize smaller than the mandatory 8-byte command header, or large enough to run past the end of the load-command table. Since the parser steps through commands by cmdsize, an invalid size would desynchronize the whole table, so it bails.","triggerScenarios":"patch_slice first scan loop: cmdsize < 8 || off + cmdsize > lc_end for some command. Happens with corrupted or malformed binaries where a command's size field is zero/garbage, or where sizeofcmds under-reports the table extent.","commonSituations":"Truncated or bit-flipped bottle artifacts; Mach-O files processed by broken tooling; fuzzing corpora.","solutions":["Clear the bottle cache and re-pour from a fresh download","Validate with otool -l <file>; malformed size fields will fail there too if the file is truly corrupt","Report the specific binary upstream if it is reproducibly patchable by ruby-macho/otool"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Check each command's size is at least 8 and stays inside the table.\nfn command_sizes_ok(slice: &[u8]) -> bool {\n    let lc_end = 32 + u32_at(slice, 20) as usize;\n    let mut off = 32;\n    for _ in 0..u32_at(slice, 16) as usize {\n        if off + 8 > lc_end { return false; }\n        let cmdsize = u32_at(slice, off + 4) as usize;\n        if cmdsize < 8 || off + cmdsize > lc_end { return false; }\n        off += cmdsize;\n    }\n    true\n}","typeGuard":"fn macho_commands_wellformed(slice: &[u8]) -> bool {\n    macho_header_ok(slice) && command_sizes_ok(slice)\n}","tryCatchPattern":"match patch_macho(&mut content, &replacements, &path) {\n    Ok(_) => {}\n    Err(e) => warn!(\"{}: malformed Mach-O, left unpatched: {e:#}\", path.display()),\n}","preventionTips":["Checksum bottles before pouring; malformed command sizes almost always mean corruption","Report binaries that otool parses fine but the rewriter rejects — parser gaps matter upstream"],"tags":["brew","macho","macos","binary-patching","validation"],"backgroundTag":"mach-o-header-validation-failed","analyzedSha":"9dcfcaa0dc8747a2577d3270b69bb9d8313b2807","analyzedAt":"2026-08-17T14:28:50.624Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}