Skip to content

Commit e0439e3

Browse files
Fix panics for invalid utf-8 json streams (#119)
`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.
1 parent f3e6bad commit e0439e3

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

src/page.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ impl Page {
252252
}?;
253253
let mut buf = unsafe { Buffer::from_raw(inner) };
254254
let mut res = String::new();
255-
buf.read_to_string(&mut res).unwrap();
255+
buf.read_to_string(&mut res)?;
256256
Ok(res)
257257
}
258258

tests/files/no-json.pdf

84.9 KB
Binary file not shown.

tests/test_issues.rs

+8
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,11 @@ fn test_issue_i32_box() {
117117
assert!(stext_page.is_ok());
118118
}
119119
}
120+
121+
#[test]
122+
fn test_issue_no_json() {
123+
let doc = PdfDocument::open("tests/files/no-json.pdf").unwrap();
124+
let page = doc.load_page(0).unwrap();
125+
let json = page.stext_page_as_json_from_page(1.0);
126+
assert!(json.is_err());
127+
}

0 commit comments

Comments
 (0)