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
1 change: 1 addition & 0 deletions _codeql_detected_source_root
49 changes: 49 additions & 0 deletions cpp11test/src/test-external_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,53 @@ context("external_pointer-C++") {
uniq.reset();
expect_true(deleted == true);
}

test_that("external_pointer preserves attributes when moved (issue #308)") {
// Test move constructor
{
int* value = new int(42);
cpp11::external_pointer<int> p(value);

// Set an attribute on the external pointer
Rf_setAttrib(p, R_ClassSymbol, Rf_mkString("test_class"));

// Verify attribute exists before move
SEXP class_attr = Rf_getAttrib(p, R_ClassSymbol);
expect_true(class_attr != R_NilValue);

// Move the external pointer using move constructor
cpp11::external_pointer<int> p_moved = std::move(p);

// Verify attribute is preserved after move
SEXP class_attr_after = Rf_getAttrib(p_moved, R_ClassSymbol);
expect_true(class_attr_after != R_NilValue);
expect_true(strcmp(CHAR(STRING_ELT(class_attr_after, 0)), "test_class") == 0);

// Clean up
delete p_moved.release();
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling delete on the result of release() will attempt to delete the memory that was already wrapped by the external pointer, but the external pointer's finalizer has already been set up to delete this memory when the R object is garbage collected. This creates a double-free scenario. Since the external pointer owns the memory, simply allowing p_moved to go out of scope is sufficient - the finalizer will handle cleanup.

Copilot uses AI. Check for mistakes.
}

// Test move assignment operator
{
int* value1 = new int(1);
cpp11::external_pointer<int> p1(value1);

// Set an attribute on p1
Rf_setAttrib(p1, R_ClassSymbol, Rf_mkString("test_class"));

// Create p2 with nullptr (no memory leak)
cpp11::external_pointer<int> p2(nullptr);

// Move assign p1 to p2
p2 = std::move(p1);

// Verify attribute is preserved after move assignment
SEXP class_attr_after = Rf_getAttrib(p2, R_ClassSymbol);
expect_true(class_attr_after != R_NilValue);
expect_true(strcmp(CHAR(STRING_ELT(class_attr_after, 0)), "test_class") == 0);

// Clean up
delete p2.release();
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same double-free issue as above. The external pointer's finalizer will delete the memory when the R object is garbage collected. Calling delete on release() creates a double-free scenario. Remove this manual deletion and let the external pointer's destructor handle cleanup.

Copilot uses AI. Check for mistakes.
}
}
}
1 change: 1 addition & 0 deletions inst/include/cpp11/external_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class external_pointer {
external_pointer& operator=(external_pointer&& rhs) noexcept {
data_ = rhs.data_;
rhs.data_ = R_NilValue;
return *this;
}

external_pointer& operator=(std::nullptr_t) noexcept { reset(); };
Expand Down
Loading