Skip to content

Commit

Permalink
Fixed a crash when adding pages from another QPdf object, closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
ancwrd1 committed Nov 23, 2023
1 parent 4528623 commit 9383b6b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
[workspace]
resolver = "2"
members = ["qpdf-sys", "qpdf-rs"]

[workspace.package]
version = "0.1.7"
authors = ["Dmitry Pankratov <[email protected]>"]
edition = "2021"
repository = "https://github.com/ancwrd1/qpdf-rs"
documentation = "https://docs.rs/qpdf"
readme = "README.md"
keywords = ["PDF", "QPDF"]
license = "MIT/Apache-2.0"
16 changes: 8 additions & 8 deletions qpdf-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "qpdf"
version = "0.1.6"
authors = ["Dmitry Pankratov <[email protected]>"]
description = "Rust bindings to QPDF C++ library"
license = "MIT/Apache-2.0"
repository = "https://github.com/ancwrd1/qpdf-rs"
documentation = "https://docs.rs/qpdf"
readme = "README.md"
keywords = ["PDF", "QPDF"]
edition = "2021"
version.workspace = true
authors.workspace = true
edition.workspace = true
repository.workspace = true
documentation.workspace = true
readme.workspace = true
keywords.workspace = true
license.workspace = true

[dependencies]
qpdf-sys = { path = "../qpdf-sys", version = "0.1" }
Expand Down
7 changes: 7 additions & 0 deletions qpdf-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ impl QPdf {

/// Add a page object to PDF. The `first` parameter indicates whether to prepend or append it.
pub fn add_page<T: AsRef<QPdfObject>>(self: &QPdf, new_page: T, first: bool) -> Result<()> {
if new_page.as_ref().owner.inner() != self.inner() {
self.foreign.borrow_mut().insert(new_page.as_ref().owner.clone());
}
self.wrap_ffi_call(|| unsafe {
qpdf_sys::qpdf_add_page(
self.inner(),
Expand All @@ -269,6 +272,9 @@ impl QPdf {
N: AsRef<QPdfObject>,
R: AsRef<QPdfObject>,
{
if new_page.as_ref().owner.inner() != self.inner() {
self.foreign.borrow_mut().insert(new_page.as_ref().owner.clone());
}
self.wrap_ffi_call(|| unsafe {
qpdf_sys::qpdf_add_page_at(
self.inner(),
Expand Down Expand Up @@ -308,6 +314,7 @@ impl QPdf {

/// Remove page object from the PDF.
pub fn remove_page<P: AsRef<QPdfObject>>(self: &QPdf, page: P) -> Result<()> {
self.foreign.borrow_mut().remove(&page.as_ref().owner);
self.wrap_ffi_call(|| unsafe { qpdf_sys::qpdf_remove_page(self.inner(), page.as_ref().inner) })
}

Expand Down
14 changes: 14 additions & 0 deletions qpdf-rs/tests/test_qpdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,17 @@ fn test_foreign_objects() {
.write_to_memory()
.unwrap();
}

#[test]
fn test_foreign_pages() {
let sink = QPdf::empty();

for source in [load_pdf_from_memory(), load_pdf_from_memory(), load_pdf_from_memory()] {
source.get_pages().unwrap().iter().for_each(|p| {
sink.add_page(p, false).unwrap();
});
}

// shouldn't crash
sink.writer().write_to_memory().unwrap();
}
16 changes: 8 additions & 8 deletions qpdf-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "qpdf-sys"
version = "0.1.6"
authors = ["Dmitry Pankratov <[email protected]>"]
description = "Rust bindings to QPDF C++ library via FFI and bindgen"
license = "MIT/Apache-2.0"
repository = "https://github.com/ancwrd1/qpdf-rs"
documentation = "https://docs.rs/qpdf"
readme = "README.md"
keywords = ["PDF", "QPDF"]
edition = "2021"
version.workspace = true
authors.workspace = true
edition.workspace = true
repository.workspace = true
documentation.workspace = true
readme.workspace = true
keywords.workspace = true
license.workspace = true
exclude = [
'qpdf/qpdf/*',
'qpdf/appimage/*',
Expand Down

0 comments on commit 9383b6b

Please sign in to comment.