From 7a72ec90a106ddf72a6a49e253354c7fe77e345f Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Fri, 9 May 2025 03:31:51 -0400 Subject: [PATCH 1/4] implement fix for #445 --- inst/include/cpp11/list.hpp | 5 ++++- inst/include/cpp11/r_vector.hpp | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/inst/include/cpp11/list.hpp b/inst/include/cpp11/list.hpp index be29bb2a..66cd99c7 100644 --- a/inst/include/cpp11/list.hpp +++ b/inst/include/cpp11/list.hpp @@ -79,7 +79,8 @@ inline r_vector::r_vector(std::initializer_list il) : cpp11::r_vector(safe[Rf_allocVector](VECSXP, il.size())), capacity_(il.size()) { unwind_protect([&] { - SEXP names = Rf_allocVector(STRSXP, capacity_); + SEXP names; + PROTECT(names = Rf_allocVector(STRSXP, capacity_)); Rf_setAttrib(data_, R_NamesSymbol, names); auto it = il.begin(); @@ -91,6 +92,8 @@ inline r_vector::r_vector(std::initializer_list il) SEXP name = Rf_mkCharCE(it->name(), CE_UTF8); SET_STRING_ELT(names, i, name); } + + UNPROTECT(1); }); } diff --git a/inst/include/cpp11/r_vector.hpp b/inst/include/cpp11/r_vector.hpp index 576f4fe6..b2e512fe 100644 --- a/inst/include/cpp11/r_vector.hpp +++ b/inst/include/cpp11/r_vector.hpp @@ -865,7 +865,8 @@ inline r_vector::r_vector(std::initializer_list il) } unwind_protect([&] { - SEXP names = Rf_allocVector(STRSXP, capacity_); + SEXP names; + PROTECT(names = Rf_allocVector(STRSXP, capacity_)); Rf_setAttrib(data_, R_NamesSymbol, names); auto it = il.begin(); @@ -890,6 +891,8 @@ inline r_vector::r_vector(std::initializer_list il) SEXP name = Rf_mkCharCE(it->name(), CE_UTF8); SET_STRING_ELT(names, i, name); } + + UNPROTECT(1); }); } From 73da82bea732e78ccf9732de17d89ac416491200 Mon Sep 17 00:00:00 2001 From: Davis Vaughan Date: Fri, 16 Jan 2026 20:36:33 -0500 Subject: [PATCH 2/4] Different approach for `names` protection --- inst/include/cpp11/list.hpp | 10 ++++------ inst/include/cpp11/r_vector.hpp | 10 ++++------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/inst/include/cpp11/list.hpp b/inst/include/cpp11/list.hpp index 66cd99c7..6e7dd872 100644 --- a/inst/include/cpp11/list.hpp +++ b/inst/include/cpp11/list.hpp @@ -78,11 +78,9 @@ template <> inline r_vector::r_vector(std::initializer_list il) : cpp11::r_vector(safe[Rf_allocVector](VECSXP, il.size())), capacity_(il.size()) { - unwind_protect([&] { - SEXP names; - PROTECT(names = Rf_allocVector(STRSXP, capacity_)); - Rf_setAttrib(data_, R_NamesSymbol, names); + sexp names = safe[Rf_allocVector](STRSXP, capacity_); + unwind_protect([&] { auto it = il.begin(); for (R_xlen_t i = 0; i < capacity_; ++i, ++it) { @@ -92,9 +90,9 @@ inline r_vector::r_vector(std::initializer_list il) SEXP name = Rf_mkCharCE(it->name(), CE_UTF8); SET_STRING_ELT(names, i, name); } - - UNPROTECT(1); }); + + safe[Rf_setAttrib](data_, R_NamesSymbol, names); } typedef r_vector list; diff --git a/inst/include/cpp11/r_vector.hpp b/inst/include/cpp11/r_vector.hpp index b2e512fe..1131b554 100644 --- a/inst/include/cpp11/r_vector.hpp +++ b/inst/include/cpp11/r_vector.hpp @@ -864,11 +864,9 @@ inline r_vector::r_vector(std::initializer_list il) valid_length(value, 1); } - unwind_protect([&] { - SEXP names; - PROTECT(names = Rf_allocVector(STRSXP, capacity_)); - Rf_setAttrib(data_, R_NamesSymbol, names); + sexp names = safe[Rf_allocVector](STRSXP, capacity_); + unwind_protect([&] { auto it = il.begin(); for (R_xlen_t i = 0; i < capacity_; ++i, ++it) { @@ -891,9 +889,9 @@ inline r_vector::r_vector(std::initializer_list il) SEXP name = Rf_mkCharCE(it->name(), CE_UTF8); SET_STRING_ELT(names, i, name); } - - UNPROTECT(1); }); + + safe[Rf_setAttrib](data_, R_NamesSymbol, names); } template From 1e415c70fbbb537c0350925a29293785f608b214 Mon Sep 17 00:00:00 2001 From: Davis Vaughan Date: Fri, 16 Jan 2026 20:38:38 -0500 Subject: [PATCH 3/4] NEWS bullet --- NEWS.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index f9a44056..5b16fd54 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # cpp11 (development version) +* Fixed an rchk issue related to `std::initializer_list` (#457, @pachadotdev). + # cpp11 0.5.2 * Fixed an issue related to `-Wdeprecated-literal-operator` (#447, @andrjohns). @@ -12,7 +14,7 @@ * Because cpp11 now requires R >=4.0.0, a number of previously optional tools are now always available, allowing us to remove some dead code. In particular: - + * `R_UnwindProtect()` is always available, so the defines `HAS_UNWIND_PROTECT` and `CPP11_UNWIND` are no longer useful. From aedd4b4491bef15c4dfee39c9cfd2bc82476e53f Mon Sep 17 00:00:00 2001 From: Davis Vaughan Date: Fri, 16 Jan 2026 20:52:06 -0500 Subject: [PATCH 4/4] Regenerate snapshots with latest testthat --- tests/testthat/_snaps/register.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testthat/_snaps/register.md b/tests/testthat/_snaps/register.md index 37afbf49..aea86b76 100644 --- a/tests/testthat/_snaps/register.md +++ b/tests/testthat/_snaps/register.md @@ -1,4 +1,4 @@ -# get_call_entries: returns an empty string for packages with .Call entries and NAMESPACE files +# get_call_entries / returns an empty string for packages with .Call entries and NAMESPACE files Code call_entries @@ -11,7 +11,7 @@ [6] " {NULL, NULL, 0}" [7] "};" -# get_call_entries: works with multiple register functions. +# get_call_entries / works with multiple register functions. Code cat(read_file(cpp_bindings)) @@ -60,7 +60,7 @@ R_forceSymbols(dll, TRUE); } -# cpp_register: works with a package that registers a single c++ function +# cpp_register / works with a package that registers a single c++ function Code cat(read_file(r_bindings)) @@ -104,7 +104,7 @@ R_forceSymbols(dll, TRUE); } -# cpp_register: can be run with messages +# cpp_register / can be run with messages Code cpp_register(p, quiet = FALSE)