Skip to content

Commit

Permalink
Open multiple architectures with same offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderOMara committed Jan 19, 2025
1 parent 09e2be5 commit 904e4a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
28 changes: 27 additions & 1 deletion macho/universal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Deno.test('open under arch', async () => {
Deno.test('open under count archs', async () => {
const data = new ArrayBuffer(
Math.max(FatHeader.BYTE_LENGTH, MachHeader.BYTE_LENGTH) +
FatArch.BYTE_LENGTH + 512,
FatArch.BYTE_LENGTH * 2 + 512,
);
const header = new FatHeader(data);
header.magic = FAT_MAGIC;
Expand All @@ -152,6 +152,32 @@ Deno.test('open under count archs', async () => {
assertEquals(archs.size, 2);
});

Deno.test('open duplicate offset', async () => {
const data = new ArrayBuffer(
Math.max(FatHeader.BYTE_LENGTH, MachHeader.BYTE_LENGTH) +
FatArch.BYTE_LENGTH * 2,
);
const header = new FatHeader(data);
header.magic = FAT_MAGIC;
header.nfatArch = 2;

const arch1 = new FatArch(data, header.byteLength);
arch1.offset = data.byteLength;
arch1.size = 0;

const arch2 = new FatArch(data, arch1.byteOffset + arch1.byteLength);
arch2.offset = data.byteLength;
arch2.size = 0;

const blob = new Blob([data]);
const uni = new Universal();
await assertRejects(
() => uni.open(blob),
RangeError,
`Multiple architectures at offset: 0x${data.byteLength.toString(16)}`,
);
});

Deno.test('typeOf under header', async () => {
const blob = new Blob([new ArrayBuffer(MachHeader.BYTE_LENGTH - 1)]);
assertEquals(await Universal.typeOf(blob), 0);
Expand Down
4 changes: 3 additions & 1 deletion macho/universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ export class Universal {
for (const { offset, size, align } of sortedList) {
if (mSizes.has(offset)) {
throw new RangeError(
`Two architectures have the same offset: ${offset}`,
`Multiple architectures at offset: 0x${
offset.toString(16)
}`,
);
}
mSizes.set(offset, size);
Expand Down

0 comments on commit 904e4a5

Please sign in to comment.