Skip to content

Commit

Permalink
Fix panics for invalid utf-8 json streams
Browse files Browse the repository at this point in the history
`stext_page_as_json_from_page` was using `unwrap` for reading the mupdf
json stream. This was causing panic when the stream was not a valid
utf-8.

The function now returns the `error` in such cases.
  • Loading branch information
pratyushmittal committed Feb 21, 2025
1 parent f3e6bad commit 8cdcdad
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl Page {
}?;
let mut buf = unsafe { Buffer::from_raw(inner) };
let mut res = String::new();
buf.read_to_string(&mut res).unwrap();
buf.read_to_string(&mut res)?;
Ok(res)
}

Expand Down
Binary file added tests/files/no-json.pdf
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/test_issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,11 @@ fn test_issue_i32_box() {
assert!(stext_page.is_ok());
}
}

#[test]
fn test_issue_no_json() {
let doc = PdfDocument::open("tests/files/no-json.pdf").unwrap();
let page = doc.load_page(0).unwrap();
let json = page.stext_page_as_json_from_page(1.0);
assert!(json.is_err());
}

0 comments on commit 8cdcdad

Please sign in to comment.