diff --git a/bplistParser.ts b/bplistParser.ts index 38c810e..066bd3e 100644 --- a/bplistParser.ts +++ b/bplistParser.ts @@ -220,19 +220,16 @@ export function parseBuffer(buffer: Buffer): [T] { throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available."); } - function parseReal(): number | undefined { + function parseReal(): number { const length = Math.pow(2, objInfo); - if (length < maxObjectSize) { - const realBuffer = buffer.slice(offset + 1, offset + 1 + length); - if (length === 4) { - return realBuffer.readFloatBE(0); - } - if (length === 8) { - return realBuffer.readDoubleBE(0); - } - } else { - throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available."); + const realBuffer = buffer.slice(offset + 1, offset + 1 + length); + if (length === 4) { + return realBuffer.readFloatBE(0); + } + if (length === 8) { + return realBuffer.readDoubleBE(0); } + throw new Error("Invalid binary plist. Unsupported real length: " + length); } function parseDate(): Date { diff --git a/test/parse.test.ts b/test/parse.test.ts index b598c66..52a757d 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -171,8 +171,41 @@ describe('bplist-parser', function () { bplist.parseBuffer(Buffer.from('bplist00')); }, /Invalid binary plist/); }); + + it('preserves negative zero reals', function () { + const payload = Buffer.alloc(8); + payload.writeDoubleBE(-0, 0); + const [parsed] = bplist.parseBuffer(makeSingleObjectPlist(Buffer.concat([Buffer.from([0x23]), payload]))); + assert.ok(Object.is(parsed, -0)); + }); + + it('rejects unsupported real widths', function () { + for (const info of [0, 1, 4, 5]) { + const object = Buffer.concat([Buffer.from([0x20 | info]), Buffer.alloc(2 ** info)]); + assert.throws(() => bplist.parseBuffer(makeSingleObjectPlist(object)), /Unsupported real 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 {