Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ for the rationale. The short version:

- Every PDFium handle (`FPDF_DOCUMENT`, `FPDF_PAGE`, etc.) lives behind an
R `externalptr`. Every handle with a finalizer is in one registry,
`src/handle_registry.{h,cpp}` (ADR-024, ADR-025, ADR-028): annotation
contexts, pages, fonts and XObjects under their document; documents,
`pdf_clip_path_new()` clip paths and bitmaps under the library.
`src/handle_registry.{h,cpp}` (ADR-024, ADR-025, ADR-028, ADR-031,
ADR-032): annotation contexts, pages, fonts, XObjects and annotation
page-objects under their document; documents, `pdf_clip_path_new()`
clip paths and bitmaps under the library.
- One mint and one release path per kind: `make_*_handle()` alone creates
the externalptr (finalizer attached, handle registered), and
`release_*_handle()` alone closes and clears it — the finalizer, the
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@
move, read them again through `pdf_annot_objects()` and
`pdf_form_objects()`. They used to read the freed objects once the
form was destroyed (ADR-029).
* `pdf_annot_remove_object()` closes the handles to the object it
removes, from every earlier `pdf_annot_objects()` call on the
annotation handle, together with the clip paths and nested objects
read from them; they now fail with "The object was removed from its
annotation by pdf_annot_remove_object()." They used to read the freed
object (ADR-032).
* Shutting PDFium down, as unloading the package does, first closes
every open document together with its pages, annotations, fonts and
XObjects, and every clip path and bitmap from `pdf_clip_path_new()`
Expand Down
12 changes: 8 additions & 4 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ cpp_annot_append_object <- function(annot_ptr, page_ptr, obj_ptr) {
.Call(`_pdfium_cpp_annot_append_object`, annot_ptr, page_ptr, obj_ptr)
}

cpp_annot_remove_object <- function(annot_ptr, index_zero) {
.Call(`_pdfium_cpp_annot_remove_object`, annot_ptr, index_zero)
cpp_annot_remove_object <- function(annot_ptr, doc_ptr, index_zero) {
.Call(`_pdfium_cpp_annot_remove_object`, annot_ptr, doc_ptr, index_zero)
}

cpp_annot_update_object <- function(annot_ptr, obj_ptr) {
Expand All @@ -209,8 +209,8 @@ cpp_annot_object_count <- function(annot_ptr) {
.Call(`_pdfium_cpp_annot_object_count`, annot_ptr)
}

cpp_annot_get_object <- function(annot_ptr, index_zero) {
.Call(`_pdfium_cpp_annot_get_object`, annot_ptr, index_zero)
cpp_annot_get_object <- function(annot_ptr, doc_ptr, index_zero) {
.Call(`_pdfium_cpp_annot_get_object`, annot_ptr, doc_ptr, index_zero)
}

cpp_annot_set_uri <- function(annot_ptr, uri) {
Expand Down Expand Up @@ -621,6 +621,10 @@ cpp_doc_handle_counts <- function(doc_ptr) {
.Call(`_pdfium_cpp_doc_handle_counts`, doc_ptr)
}

cpp_annot_object_handle_count <- function(doc_ptr) {
.Call(`_pdfium_cpp_annot_object_handle_count`, doc_ptr)
}

cpp_library_handle_counts <- function() {
.Call(`_pdfium_cpp_library_handle_counts`)
}
Expand Down
20 changes: 13 additions & 7 deletions R/api_completion.R
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,8 @@ pdf_annot_object_count <- function(annot) {
#' from the annotation's normal (`/AP /N`) appearance stream. Returns a
#' list of `pdfium_obj` handles; each handle's externalptr pins the
#' parent annotation, so the embedded objects can't dangle past the
#' annot's lifetime.
#' annot's lifetime, and [pdf_annot_remove_object()] closes the handles
#' to the object it removes.
#'
#' Each handle's `type` is the object's own type (`"path"`, `"text"`,
#' `"image"`, `"shading"` or `"form"`), as in [pdf_page_objects()] and
Expand Down Expand Up @@ -628,7 +629,7 @@ pdf_annot_objects <- function(annot) {
out <- vector("list", n)
page <- annot$page
for (i in seq_len(n)) {
ptr <- cpp_annot_get_object(annot$ptr, i - 1L)
ptr <- cpp_annot_get_object(annot$ptr, page$doc$ptr, i - 1L)
type_int <- cpp_obj_type(ptr)
out[[i]] <- new_pdfium_obj(ptr, page, i, pdfium_obj_type_name(type_int),
parent_annot = annot)
Expand Down Expand Up @@ -700,10 +701,15 @@ pdf_annot_append_object <- function(annot, obj) {
#' Wraps `FPDFAnnot_RemoveObject`. The object is identified by its
#' position within the annotation's embedded content (one-based,
#' matching [pdf_annot_objects()]). PDFium destroys the object and
#' regenerates the annotation's appearance stream, so handles to it
#' from an earlier [pdf_annot_objects()] call, and the clip paths and
#' nested objects read from them, are stale and must not be used; call
#' [pdf_annot_objects()] again for the remaining ones.
#' regenerates the annotation's appearance stream. The handles to the
#' object from earlier [pdf_annot_objects()] calls on `annot` are
#' closed, and so are the clip paths and nested objects read from
#' them: further calls on them error cleanly. The handles to the
#' remaining objects stay valid, but their positions shift down by
#' one; call [pdf_annot_objects()] again for the new positions.
#' Objects read through another handle to the same annotation, for
#' example from a second [pdf_annotations()] call, are separate copies
#' and are not affected.
#'
#' @param annot A `pdfium_annot` of subtype `"ink"` or `"stamp"`, the
#' only subtypes PDFium edits embedded objects of. Parent doc must
Expand All @@ -716,7 +722,7 @@ pdf_annot_remove_object <- function(annot, index) {
checkmate::assert_int(index, lower = 1L)
ctx <- assert_annot_writable(annot)
expect_setter_ok(
cpp_annot_remove_object(annot$ptr, as.integer(index) - 1L),
cpp_annot_remove_object(annot$ptr, annot$page$doc$ptr, as.integer(index) - 1L),
"FPDFAnnot_RemoveObject")
finalize_annot_setter(ctx)
}
Expand Down
17 changes: 13 additions & 4 deletions R/obj_extras.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ check_pdfium_obj <- function(obj, allowed_types = NULL, arg = "obj") {
# document was closed, which closes its pages (ADR-025); "annotation"
# when the annotation whose appearance stream holds it was deleted
# with pdf_annot_delete(); "form" when a form object it is nested in
# was closed by a mutator; otherwise "page" (its page was closed, or a
# mutator cleared the handle itself).
# was closed by a mutator; "removed" when pdf_annot_remove_object()
# removed it from its annotation (ADR-032); otherwise "page" (its page
# was closed, or a mutator cleared the handle itself).
obj_closed_cause <- function(obj) {
if (!is_open(obj$page$doc)) {
return("document")
Expand All @@ -47,6 +48,9 @@ obj_closed_cause <- function(obj) {
}
form <- form$parent_form
}
if (!is.null(obj$parent_annot) && !cpp_handle_is_valid(obj$ptr)) {
return("removed")
}
"page"
}

Expand All @@ -64,8 +68,13 @@ closed_cause_message <- function(cause, what) {
),
form = paste0(
"Parent form object has been closed: it was deleted, removed ",
"from its form or moved into an annotation. The ", what,
" is no longer valid."
"from its form or annotation, or moved into an annotation. The ",
what, " is no longer valid."
),
removed = paste0(
if (what == "clip path") "The clip path's page-object" else "The object",
" was removed from its annotation by pdf_annot_remove_object(). ",
"The ", what, " is no longer valid."
)
)
}
Expand Down
4 changes: 3 additions & 1 deletion dev/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ Read [ADR-005](decisions/ADR-005-memory-model.md) first. Summary:
chain is closed (ADR-029).
5. Closing a document, explicitly or in its finalizer, first closes
the annotation, page, font and XObject handles still open on it:
each is registered under its document (ADR-024, ADR-025).
each is registered under its document (ADR-024, ADR-025). So are
the handles to an annotation's page-objects, which
`pdf_annot_remove_object()` clears by address (ADR-032).
Documents, clip paths and bitmaps are registered under the
library, and `cpp_destroy_library()` releases them, each document
with its handles, before `FPDF_DestroyLibrary` (ADR-028). A
Expand Down
87 changes: 87 additions & 0 deletions dev/decisions/ADR-032-annotation-object-handles-close-on-remove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# ADR-032 — Removing an annotation's page-object closes its handles

- Status: Accepted (extends ADR-029)
- Date: 2026-09-27
- Deciders: Bill Denney

## Context

ADR-029 refuses a handle once any of its owners is closed, but left
one path open: `pdf_annot_remove_object()` destroys an object of an
annotation's appearance stream by position (`FPDFAnnot_RemoveObject`),
and nothing could find the handles made to that object earlier. They
still pinned an open annotation, so they, and the clip paths and nested
objects read through them, passed every check and read the freed
object. Under valgrind on the ADR-029 branch:

- the removed object's own handle, and a second handle to it from
another `pdf_annot_objects()` call: 4 invalid reads;
- a clip path read from the object: 1 invalid read;
- an object nested in a form removed from its annotation: 5 invalid
reads.

Each annotation handle has its own annotation context, which parses the
appearance stream into its own objects: objects read through another
handle to the same annotation are separate copies, at other addresses,
and the removal does not touch them.

## Decision

1. The handles `pdf_annot_objects()` returns are registered in the
ADR-025 registry, under the annotation's document, as a new kind
whose release only clears the handle: the annotation owns the
object. `make_annot_object_handle()` mints them; like every other
registered handle they are released by their finalizer and by their
document's close.
2. After `FPDFAnnot_RemoveObject` succeeds, `cpp_annot_remove_object()`
releases every handle of that kind registered under the document
whose address is the removed object's, with
`release_annot_object_handles()`. Only addresses are compared, so
the object being freed already does not matter; a removal PDFium
refuses leaves the handles open. The clip paths and nested objects
read through the released handles are then refused by the ADR-029
chain.
3. As with annotation handles (ADR-024), the shims take the document
from R: `cpp_annot_get_object()` and `cpp_annot_remove_object()`
gain a document argument.
4. The closed-handle messages name the cause: "The object was removed
from its annotation by pdf_annot_remove_object()."

## Consequences

- No handle reachable from `pdf_annot_objects()` outlives the object it
points to.
- Every annotation page-object handle carries a finalizer, which only
deregisters it.
- A second handle to one page-object of a page or form, from a separate
`pdf_page_objects()` or `pdf_form_objects()` call, still stays stale
after the object is deleted through the first. Those objects have no
registry entry; registering them the same way would close that gap
too.
- `cpp_annot_object_handle_count()` reports the registered annotation
page-object handles of a document, for tests;
`cpp_doc_handle_counts()` is unchanged.

## Alternatives considered

- **Document the handles as stale**, as ADR-029 did: leaves a
use-after-free one call away from an exported function.
- **Register the handles under their annotation handle** instead of the
document: the registry is keyed by document, and the address already
tells the removed object's handles apart from those of other
annotation contexts.
- **Release the handles before the removal**: a removal PDFium refuses,
for an annotation subtype that cannot hold objects, would close
handles to an object that still exists.

## References

- ADR-024, ADR-025, ADR-029.
- `src/handle_registry.h`, `src/handle_registry.cpp`,
`src/api_completion.cpp` (`cpp_annot_get_object`,
`cpp_annot_remove_object`).
- PDFium `public/fpdf_annot.h` (`FPDFAnnot_GetObject`,
`FPDFAnnot_RemoveObject`).
- `tests/testthat/test-api-completion.R`, section
"pdf_annot_remove_object() closes the removed object's handles
(ADR-032)".
1 change: 1 addition & 0 deletions dev/decisions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Markdown ADR following the
| [029](ADR-029-handles-follow-their-owners.md) | Accepted (extends ADR-020 §4 and ADR-025) | A handle is refused once any of its owners is closed: clip paths pin their page-object and nested objects their form, and `validate_handle()` and `is_open()` walk the whole ownership chain |
| [030](ADR-030-rhub-v2-cran-flavour-checks.md) | Supersedes ADR-007's `cran-check.yaml` row | Weekly CRAN-flavour checks with rhub v2 (`rhub.yaml`): windows and macos-arm64 on R-devel, gcc-asan, clang-asan, rchk, nosuggests; replaces the defunct `rhub::check_for_cran()` job |
| [031](ADR-031-r-level-finalizers.md) | Supersedes ADR-005's C finalizers and ADR-028's memory-buffer handle | Handle finalizers are R functions that call in only while the shared library is loaded: `make_handle()` registers `finalize_handle()`, which looks its routine up by name each time and releases only handles the loaded library registered; a memory document's bytes are a raw vector in its handle's `prot` slot |
| [032](ADR-032-annotation-object-handles-close-on-remove.md) | Accepted (extends ADR-029) | Removing an annotation's page-object closes its handles: `pdf_annot_objects()` handles are registered under their document, and `pdf_annot_remove_object()` releases those whose address is the removed object's |

## Policy

Expand Down
3 changes: 2 additions & 1 deletion man/pdf_annot_objects.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions man/pdf_annot_remove_object.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 22 additions & 8 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,14 +629,15 @@ BEGIN_RCPP
END_RCPP
}
// cpp_annot_remove_object
bool cpp_annot_remove_object(SEXP annot_ptr, int index_zero);
RcppExport SEXP _pdfium_cpp_annot_remove_object(SEXP annot_ptrSEXP, SEXP index_zeroSEXP) {
bool cpp_annot_remove_object(SEXP annot_ptr, SEXP doc_ptr, int index_zero);
RcppExport SEXP _pdfium_cpp_annot_remove_object(SEXP annot_ptrSEXP, SEXP doc_ptrSEXP, SEXP index_zeroSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< SEXP >::type annot_ptr(annot_ptrSEXP);
Rcpp::traits::input_parameter< SEXP >::type doc_ptr(doc_ptrSEXP);
Rcpp::traits::input_parameter< int >::type index_zero(index_zeroSEXP);
rcpp_result_gen = Rcpp::wrap(cpp_annot_remove_object(annot_ptr, index_zero));
rcpp_result_gen = Rcpp::wrap(cpp_annot_remove_object(annot_ptr, doc_ptr, index_zero));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -664,14 +665,15 @@ BEGIN_RCPP
END_RCPP
}
// cpp_annot_get_object
SEXP cpp_annot_get_object(SEXP annot_ptr, int index_zero);
RcppExport SEXP _pdfium_cpp_annot_get_object(SEXP annot_ptrSEXP, SEXP index_zeroSEXP) {
SEXP cpp_annot_get_object(SEXP annot_ptr, SEXP doc_ptr, int index_zero);
RcppExport SEXP _pdfium_cpp_annot_get_object(SEXP annot_ptrSEXP, SEXP doc_ptrSEXP, SEXP index_zeroSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< SEXP >::type annot_ptr(annot_ptrSEXP);
Rcpp::traits::input_parameter< SEXP >::type doc_ptr(doc_ptrSEXP);
Rcpp::traits::input_parameter< int >::type index_zero(index_zeroSEXP);
rcpp_result_gen = Rcpp::wrap(cpp_annot_get_object(annot_ptr, index_zero));
rcpp_result_gen = Rcpp::wrap(cpp_annot_get_object(annot_ptr, doc_ptr, index_zero));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -1874,6 +1876,17 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// cpp_annot_object_handle_count
int cpp_annot_object_handle_count(SEXP doc_ptr);
RcppExport SEXP _pdfium_cpp_annot_object_handle_count(SEXP doc_ptrSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< SEXP >::type doc_ptr(doc_ptrSEXP);
rcpp_result_gen = Rcpp::wrap(cpp_annot_object_handle_count(doc_ptr));
return rcpp_result_gen;
END_RCPP
}
// cpp_library_handle_counts
Rcpp::IntegerVector cpp_library_handle_counts();
RcppExport SEXP _pdfium_cpp_library_handle_counts() {
Expand Down Expand Up @@ -3395,10 +3408,10 @@ static const R_CallMethodDef CallEntries[] = {
{"_pdfium_cpp_annot_add_ink_stroke", (DL_FUNC) &_pdfium_cpp_annot_add_ink_stroke, 2},
{"_pdfium_cpp_annot_remove_ink_list", (DL_FUNC) &_pdfium_cpp_annot_remove_ink_list, 1},
{"_pdfium_cpp_annot_append_object", (DL_FUNC) &_pdfium_cpp_annot_append_object, 3},
{"_pdfium_cpp_annot_remove_object", (DL_FUNC) &_pdfium_cpp_annot_remove_object, 2},
{"_pdfium_cpp_annot_remove_object", (DL_FUNC) &_pdfium_cpp_annot_remove_object, 3},
{"_pdfium_cpp_annot_update_object", (DL_FUNC) &_pdfium_cpp_annot_update_object, 2},
{"_pdfium_cpp_annot_object_count", (DL_FUNC) &_pdfium_cpp_annot_object_count, 1},
{"_pdfium_cpp_annot_get_object", (DL_FUNC) &_pdfium_cpp_annot_get_object, 2},
{"_pdfium_cpp_annot_get_object", (DL_FUNC) &_pdfium_cpp_annot_get_object, 3},
{"_pdfium_cpp_annot_set_uri", (DL_FUNC) &_pdfium_cpp_annot_set_uri, 2},
{"_pdfium_cpp_annot_set_appearance", (DL_FUNC) &_pdfium_cpp_annot_set_appearance, 3},
{"_pdfium_cpp_annot_add_file_attachment", (DL_FUNC) &_pdfium_cpp_annot_add_file_attachment, 3},
Expand Down Expand Up @@ -3501,6 +3514,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_pdfium_cpp_set_handle_finalizer", (DL_FUNC) &_pdfium_cpp_set_handle_finalizer, 1},
{"_pdfium_cpp_finalize_handle", (DL_FUNC) &_pdfium_cpp_finalize_handle, 1},
{"_pdfium_cpp_doc_handle_counts", (DL_FUNC) &_pdfium_cpp_doc_handle_counts, 1},
{"_pdfium_cpp_annot_object_handle_count", (DL_FUNC) &_pdfium_cpp_annot_object_handle_count, 1},
{"_pdfium_cpp_library_handle_counts", (DL_FUNC) &_pdfium_cpp_library_handle_counts, 0},
{"_pdfium_cpp_image_new_from_jpeg", (DL_FUNC) &_pdfium_cpp_image_new_from_jpeg, 3},
{"_pdfium_cpp_image_new_blank", (DL_FUNC) &_pdfium_cpp_image_new_blank, 2},
Expand Down
Loading
Loading