{"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":null,"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/533346cc374382b41ec5ff70536252b2e96e725c/src/system/packages/brew/macho.rs#L72-L108","documentation":"Thrown when an individual load command inside the table is invalid: either its cmdsize is smaller than the minimum 8 bytes or the command extends past lc_end. This is a finer-grained check than the table-level error and indicates the specific command at the current offset is corrupt.","triggerScenarios":"patch_slice reads cmd and cmdsize at the current offset; `cmdsize < 8 || off + cmdsize > lc_end` triggers the bail. Occurs with binaries whose load commands were truncated, misaligned, or produced by a broken linker/post-processor.","commonSituations":"Binaries processed by outdated or buggy strip/install_name_tool-like utilities; corrupted bottles; patching binaries built with unusual linkers; slices misaligned by earlier incorrect parsing.","solutions":["Reinstall the formula to replace the corrupt binary.","Identify the offending command with `otool -l` and restore the binary from a known-good bottle.","Have the patcher report the offset and cmd value for diagnosis before failing.","Skip non-essential patching for binaries with anomalous load commands."],"exampleFix":"// before: blind patch\nbrew_patch(&path)?;\n// after: verify parseability first\nlet out = Command::new(\"otool\").args([\"-l\"]).arg(&path).output()?;\nif !out.status.success() { anyhow::bail!(\"bad load command in {}\", path.display()); }\nbrew_patch(&path)?;","handlingStrategy":"validation","validationCode":"fn has_min_table(content: &[u8]) -> bool {\n    content.len() >= 32\n        && u32::from_le_bytes(content[16..20].try_into().unwrap()) > 0\n        && u32::from_le_bytes(content[20..24].try_into().unwrap()) as usize + 32 <= content.len()\n}","typeGuard":null,"tryCatchPattern":"if let Err(e) = brew_patch(&path) {\n    if format!(\"{e}\").contains(\"malformed load command in\") {\n        // fall back to reinstall\n        let _ = Command::new(\"brew\").args([\"reinstall\", formula]).status();\n    }\n}","preventionTips":["Reinstall rather than patching binaries flagged by otool/codesign.","Avoid third-party post-install strippers on bottles.","Snapshot binaries before rewriting so failures are recoverable."],"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"}