Repository navigation
Description
Disclaimer: This detail in the PDF spec is out of my depth so, while I validated the details below the best I could, I did rely heavily on Claude to chase this down
Summary
pdf::file::FileOptions::open fails with a ParseIntError { kind: PosOverflow }
(wrapped in PdfError::Try) when opening a PDF whose /Encrypt dictionary
writes the /P permissions value as its raw unsigned 32-bit literal
(e.g. 4294967292) instead of the canonical signed decimal form (-4).
Per the PDF spec, /P is a signed 32-bit integer. Some PDF producers write it
as the positive bit-pattern equivalent instead of the negative decimal value.
Both Adobe Acrobat/Reader and qpdf tolerate this and reinterpret the value
via two's-complement, but pdf-rs's integer parser does not, and the file
fails to open entirely — even with the correct password — because the
failure happens while parsing the /Encrypt dictionary itself, before any
password validation takes place.
Environment
- Crate:
pdf - Version:
0.10.0 - Failing call:
pdf::file::FileOptions::open
Reproduction
Given an encrypted PDF whose /Encrypt dictionary looks like:
<< /Filter /Standard /Length 128 /O <...> /P 4294967292 /R 3 /U <...> /V 2 >>
(where 4294967292 is 0xFFFFFFFC, i.e. -4 as an unsigned 32-bit value)
use pdf::file::FileOptions;
use std::path::PathBuf;
fn main() {
let file = FileOptions::cached()
.password(b"foo") // correct password
.open(PathBuf::from("bad-encrypted.pdf"));
println!("{:?}", file.err());
}Observed output
Some(Try { file: ".../pdf-0.10.0/src/file.rs", line: 261, column: 29,
context: Context([("r", "PlainRef { id: 15, gen: 0 }")]),
source: Try { file: ".../pdf-0.10.0/src/parser/parse_object.rs", line: 26, column: 15,
context: Context([]),
source: Try { file: ".../pdf-0.10.0/src/parser/mod.rs", line: 151, column: 20,
context: Context([]),
source: Try { file: ".../pdf-0.10.0/src/parser/mod.rs", line: 81, column: 23,
context: Context([]),
source: Try { file: ".../pdf-0.10.0/src/parser/mod.rs", line: 186, column: 32,
context: Context([]),
source: Parse { source: ParseIntError { kind: PosOverflow } } } } } } })
The error occurs while parsing the object holding the /Encrypt dictionary
(object 15 0 in the original file this was found in), well before password
verification (PdfError::InvalidPassword) is ever reached. The correct
password makes no difference — the file fails to open with any password,
including the correct one.
Expected behavior
The file should open successfully with the correct password (as it does in
Adobe Acrobat/Reader and when checked with qpdf --check), and should fail
with PdfError::InvalidPassword — not a parse error — when given an
incorrect password.
Root cause (best guess)
/P is defined by the PDF spec (ISO 32000-1 §7.6.3.2, Table 22) as a signed
32-bit integer, encoded as a plain PDF integer literal. Somewhere in the
tokenizer/number-parsing path (parser/mod.rs, around the numeric literal
parsing logic reached via parser/mod.rs:186), pdf-rs appears to parse
integer literals directly into a type that cannot represent values above
i32::MAX (2,147,483,647) as an intermediate step, even when the final
target representation should tolerate the unsigned bit-pattern form.
4294967292 is 2,147,483,647 + 2,147,483,645, well past that boundary,
so the parse fails outright with PosOverflow rather than wrapping around to
the intended -4.
Other PDF libraries have hit the same interoperability issue from the
opposite direction — see PDFsharp issue
#1, which documents a related
class of bug (object streams parsed before decryption) in the same problem
space of "PDF producers write technically off-spec-but-widely-tolerated
values, and strict libraries choke on them."
Suggested fix
When parsing /P, either:
- Parse the literal into a wider integer type (
i64/u32) first, then cast
down toi32viaas i32(which performs the two's-complement
reinterpretation qpdf and Acrobat effectively do), or - Special-case
/Pspecifically to accept values in0..=u32::MAXand
reinterpret them as their signed bit-pattern equivalent when they exceed
i32::MAX.
Reproduction files attached
good-encrypted.pdf— same PDF, encrypted normally byqpdf(canonical
/P -4). Opens successfully with user passwordfoo.bad-encrypted.pdf— byte-identical togood-encrypted.pdfexcept for a
single field:/P -4replaced with/P 4294967292(and thestartxref
offset adjusted accordingly). Fails to open with any password.decrypted.pdf— the unencrypted content, included for reference/diffing.
All three contain only placeholder text ("This is a synthetic test PDF. It
contains no personal or sensitive data.") — no real content or personal data.
qpdf --check --password=foo bad-encrypted.pdf reports the file as
structurally valid and correctly reports P = -4 (i.e. qpdf performs the
two's-complement reinterpretation), confirming the file itself is a
legitimate — if non-canonical — PDF, and that this is a parser strictness gap
rather than a malformed test fixture.
Activity
s3bk commentedon Aug 8, 2026
Now that is a fun bug.
The issue is that the type used is also the one used to encode a document, so this needs a special case to convert numbers > i32::MAX into negative numbers.
s3bk commentedon Oct 13, 2026
please verify that it works now.