Skip to content

Commit

Permalink
ensure the parser progresses.
Browse files Browse the repository at this point in the history
fixes #103
  • Loading branch information
s3bk committed Sep 25, 2021
1 parent 316168c commit cc59018
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pdf/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub trait Backend: Sized {
/// Used internally by File, but could also be useful for applications that want to look at the raw PDF objects.
fn read_xref_table_and_trailer(&self, start_offset: usize) -> Result<(XRefTable, Dictionary)> {
let xref_offset = t!(self.locate_xref_offset());
if xref_offset < start_offset {
bail!("Invalid PDF. xref before header.");
}

let mut lexer = Lexer::new(t!(self.read(start_offset + xref_offset..)));

let (xref_sections, trailer) = t!(read_xref_and_trailer_at(&mut lexer, &NoResolve));
Expand Down
5 changes: 3 additions & 2 deletions pdf/src/parser/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ impl<'a> Lexer<'a> {
}
let mut pos = self.skip_whitespace(self.pos)?;
while self.buf.get(pos) == Some(&b'%') {
if let Some(off) = self.buf[pos+1..].iter().position(|&b| b == b'\n') {
pos += off+2;
pos += 1;
if let Some(off) = self.buf[pos..].iter().position(|&b| b == b'\n') {
pos += off+1;
}

// Move away from eventual whitespace
Expand Down
4 changes: 4 additions & 0 deletions pdf/tests/xref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn infinite_loop_invalid_file() {
assert!(pdf::file::File::from_data(b"startxref%PDF-".as_ref()).is_err());
}

0 comments on commit cc59018

Please sign in to comment.