Skip to content

parser: accept /P written as an unsigned 32-bit integer - #295

Closed
apollo-2006 wants to merge 1 commit into
pdf-rs:masterfrom
apollo-2006:parse-unsigned-p
Closed

apollo-2006 wants to merge 1 commit into
pdf-rs:masterfrom
apollo-2006:parse-unsigned-p

Conversation

@apollo-2006

Copy link
Copy Markdown

/P is a 32-bit flag word that the spec says to read as unsigned, and some producers write it that way: /P 4294967292 rather than -4. The parser reads integer tokens with to::<i32>(), which fails on that value, so the encryption dictionary never parses and the file cannot be opened with any password.

As suggested on the issue, this reads such values as negative numbers. A small parse_integer helper, used at the two places the parser builds a Primitive::Integer, tries i32 first and falls back to u32, keeping the same bits, so 4294967292 becomes -4. Anything above u32::MAX or below i32::MIN is still an error. Every value it can produce could already come from a negative literal, so nothing downstream sees anything new, and since the value is still an i32 it is written back out as -4.

Testing:

  • A new test in parser::tests parses an array that goes through both call sites (reverting either one makes it fail), /P 4294967292 in a dictionary, and the u32::MAX + 1 error.
  • bad-encrypted.pdf from the issue now opens with the password foo, like good-encrypted.pdf. For comparison, poppler's pdfinfo opens both and reports the same permissions for each.
  • cargo test --workspace passes.

Fixes #293

/P is a 32-bit flag word that the spec reads as unsigned, and some
producers write it that way (4294967292 rather than -4). Integer tokens
were parsed with to::<i32>(), so such a file could not be opened.

Parse as i32 and fall back to u32, keeping the same bits. Values above
u32::MAX are still an error.

Fixes pdf-rs#293
Copilot AI lite review requested due to automatic review settings September 11, 2026 17:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@s3bk

s3bk commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

fixed in b9327bb

@s3bk s3bk closed this Oct 13, 2026
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.

Encrypted PDF with valid /P bit pattern fails to open due to i32 parse overflow

3 participants