Skip to content

fix: read 16-byte integers as signed - #65

Open
bardrop wants to merge 1 commit into
joeferner:masterfrom
bardrop-oss:fix-signed-128-bit-integers
Open

bardrop wants to merge 1 commit into
joeferner:masterfrom
bardrop-oss:fix-signed-128-bit-integers

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

What is wrong

A 16-byte integer with the top bit set is read as a huge positive number. It should be negative.

Apple's CFBinaryPList.c says 1, 2 and 4-byte integers are unsigned, and 8-byte integers are signed "(and 16-byte when available)". Python's plistlib does the same (signed=tokenL >= 3). This parser only applied the sign for 8 bytes.

Repro: take plistlib.dumps([2**64 - 1], fmt=plistlib.FMT_BINARY) and replace the 16 integer bytes with (-5).to_bytes(16, 'big', signed=True).

  • plistlib.loads(...) gives [-5]
  • bplist.parseBuffer(...) gives [340282366920938463463374607431768211451n] (2^128 - 5)

The fix

In readInteger, apply the sign for both 8 and 16-byte integers: subtract 2^(bytes * 8) when the top bit is set. Positive 16-byte values (like 2^64 - 1, which plistlib writes as 16 bytes) are not changed.

The test

Added signed 128-bit integers next to the 64-bit test, using the existing makeIntegerArray helper. It covers -1, -5, -2^63 - 1, -2^127 and 2^127 - 1.

  • Before the fix: 1 failed, 20 passed
  • After the fix: 21 passed

npm run lint, npm run typecheck and npm run smoke pass. I also ran 2000 random plists made by plistlib through the parser; there was no difference before or after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant