Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bplistParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ const EPOCH = 978307200000;

/** Wrapper for a CoreFoundation keyed-archiver UID value. */
export class UID {
UID: number;
UID: number | bigint;

constructor(id: number) {
constructor(id: number | bigint) {
this.UID = id;
}
}
Expand Down Expand Up @@ -214,10 +214,10 @@ export function parseBuffer<T = any>(buffer: Buffer): [T] {

function parseUID(): UID {
const length = objInfo + 1;
if (length < maxObjectSize) {
return new UID(readUInt(buffer.slice(offset + 1, offset + 1 + length)));
if (length > 8) {
throw new Error("Invalid binary plist. UID length must be between 1 and 8 bytes.");
}
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
return new UID(simplifyInteger(readBigUInt(buffer.slice(offset + 1, offset + 1 + length))));
}

function parseReal(): number | undefined {
Expand Down
41 changes: 41 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,49 @@ describe('bplist-parser', function () {
bplist.parseBuffer(Buffer.from('bplist00'));
}, /Invalid binary plist/);
});

it('parses UIDs larger than 2^53 without losing precision', function () {
const cases = [
{ value: 0n, bytes: 1 },
{ value: 255n, bytes: 1 },
{ value: 256n, bytes: 2 },
{ value: 4294967296n, bytes: 8 },
{ value: 2n ** 63n, bytes: 8 }
];

for (const {value, bytes} of cases) {
const object = Buffer.concat([Buffer.from([0x80 | (bytes - 1)]), writeUIntBE(value, bytes)]);
const [parsed] = bplist.parseBuffer(makeSingleObjectPlist(object));
const expected = value <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(value) : value;
assert.strictEqual(parsed.UID, expected);
}
});

it('rejects UIDs wider than 8 bytes', function () {
const object = Buffer.concat([Buffer.from([0x88]), writeUIntBE(1n, 9)]);
assert.throws(() => bplist.parseBuffer(makeSingleObjectPlist(object)), /UID length/);
});
});

function makeSingleObjectPlist(object: Buffer): Buffer {
const offsetTableOffset = 8 + object.length;
const offsetSize = byteWidth(offsetTableOffset);
const trailer = Buffer.concat([
Buffer.alloc(6),
Buffer.from([offsetSize, 1]),
writeUIntBE(1, 8),
writeUIntBE(0, 8),
writeUIntBE(offsetTableOffset, 8)
]);

return Buffer.concat([
Buffer.from('bplist00'),
object,
writeUIntBE(8, offsetSize),
trailer
]);
}

type IntegerCase = { value: bigint; bytes: number; expected: number | bigint };

function makeIntegerArray(cases: IntegerCase[]): Buffer {
Expand Down