refactor(native): extract FAT header format switch in Mach-O slice selection (#632)
Replace inline magic comparisons with an exhaustive fatHeaderFormat switch and add a regression test locking in rejection of 32-bit slice payloads inside little-endian containers. Behavior-preserving: the new test passes on the old implementation.
This commit is contained in:
@@ -253,6 +253,14 @@ describe("byte-swapped universal Apple dispatch metadata", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects a 32-bit slice payload inside a little-endian container", () => {
|
||||
const bytes = wrapped(false);
|
||||
bytes.writeUInt32BE(0xfeedface, 256);
|
||||
expect(() => decodeAppleDispatchMetadata(bytes, 100, provenance)).toThrow(
|
||||
"Only little-endian 64-bit Mach-O metadata is supported",
|
||||
);
|
||||
});
|
||||
|
||||
it("checks the full little-endian FAT64 slice extent", () => {
|
||||
const bytes = wrapped(true);
|
||||
bytes.writeBigUInt64LE(0x100000000n, 16);
|
||||
|
||||
@@ -660,10 +660,9 @@ export const inspectAppleDispatchMetadata = async (
|
||||
|
||||
const selectMachoSlice = (bytes: Buffer, architecture: string) => {
|
||||
const magic = bytes.readUInt32BE(0);
|
||||
const littleEndian = magic === 0xbebafeca || magic === 0xbfbafeca;
|
||||
if (magic !== 0xcafebabe && magic !== 0xcafebabf && !littleEndian)
|
||||
return { slice: 0, sliceEnd: bytes.length };
|
||||
const fat64 = magic === 0xcafebabf || magic === 0xbfbafeca;
|
||||
const header = fatHeaderFormat(magic);
|
||||
if (header === null) return { slice: 0, sliceEnd: bytes.length };
|
||||
const { littleEndian, fat64 } = header;
|
||||
const stride = fat64 ? 32 : 20;
|
||||
const readUInt32 = littleEndian
|
||||
? (offset: number) => bytes.readUInt32LE(offset)
|
||||
@@ -702,3 +701,20 @@ const selectMachoSlice = (bytes: Buffer, architecture: string) => {
|
||||
throw invalid("Requested FAT architecture is absent");
|
||||
return selected;
|
||||
};
|
||||
|
||||
const fatHeaderFormat = (
|
||||
magic: number,
|
||||
): { readonly littleEndian: boolean; readonly fat64: boolean } | null => {
|
||||
switch (magic) {
|
||||
case 0xcafebabe:
|
||||
return { littleEndian: false, fat64: false };
|
||||
case 0xcafebabf:
|
||||
return { littleEndian: false, fat64: true };
|
||||
case 0xbebafeca:
|
||||
return { littleEndian: true, fat64: false };
|
||||
case 0xbfbafeca:
|
||||
return { littleEndian: true, fat64: true };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user