Skip to content
Open
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: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2026-03-26 Iñaki Ucar <iucar@fedoraproject.org>

* inst/include/Rcpp/internal/r_vector.h: Use dataptr() again to avoid an
invalid pointer for empty vectors, which causes UB in e.g. std::copy
* inst/tinytest/test_vector.R: Add tests for std::copy
* inst/tinytest/cpp/Vector.cpp: Idem

2026-03-26 Dirk Eddelbuettel <edd@debian.org>

* inst/bib/Rcpp.bib: Refreshed a few more references
Expand Down
18 changes: 2 additions & 16 deletions inst/include/Rcpp/internal/r_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
//
// r_vector.h: Rcpp R/C++ interface class library -- information about R vectors
//
// Copyright (C) 2010 - 2017 Dirk Eddelbuettel and Romain Francois
// Copyright (C) 2010 - 2025 Dirk Eddelbuettel and Romain François
// Copyright (C) 2026 Dirk Eddelbuettel, Romain François and Iñaki Ucar
//
// This file is part of Rcpp.
//
Expand Down Expand Up @@ -32,21 +33,6 @@ typename Rcpp::traits::storage_type<RTYPE>::type* r_vector_start(SEXP x) {
return reinterpret_cast<pointer>(dataptr(x));
}

// add specializations to avoid use of dataptr
#define RCPP_VECTOR_START_IMPL(__RTYPE__, __ACCESSOR__) \
template <> \
inline typename Rcpp::traits::storage_type<__RTYPE__>::type* r_vector_start<__RTYPE__>(SEXP x) { \
return __ACCESSOR__(x); \
}

RCPP_VECTOR_START_IMPL(LGLSXP, LOGICAL);
RCPP_VECTOR_START_IMPL(INTSXP, INTEGER);
RCPP_VECTOR_START_IMPL(RAWSXP, RAW);
RCPP_VECTOR_START_IMPL(CPLXSXP, COMPLEX);
RCPP_VECTOR_START_IMPL(REALSXP, REAL);

#undef RCPP_VECTOR_START_IMPL

/**
* The value 0 statically casted to the appropriate type for
* the given SEXP type
Expand Down
10 changes: 9 additions & 1 deletion inst/tinytest/cpp/Vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
//
// Vector.cpp: Rcpp R/C++ interface class library -- Vector unit tests
//
// Copyright (C) 2012 - 2018 Dirk Eddelbuettel and Romain Francois
// Copyright (C) 2012 - 2025 Dirk Eddelbuettel and Romain François
// Copyright (C) 2026 Dirk Eddelbuettel, Romain François and Iñaki Ucar
//
// This file is part of Rcpp.
//
Expand Down Expand Up @@ -896,3 +897,10 @@ double NumericVector_test_out_of_bounds_read(NumericVector v, R_xlen_t i) {
SEXP CharacterVector_test_out_of_bounds_read(CharacterVector v, R_xlen_t i) {
return v[i];
}

// [[Rcpp::export]]
NumericVector vec_copy(NumericVector vec1) {
NumericVector vec2(vec1.size());
std::copy(vec1.begin(), vec1.end(), vec2.begin());
return vec2;
}
8 changes: 7 additions & 1 deletion inst/tinytest/test_vector.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

## Copyright (C) 2010 - 2019 Dirk Eddelbuettel and Romain Francois
## Copyright (C) 2010 - 2025 Dirk Eddelbuettel and Romain François
## Copyright (C) 2026 Dirk Eddelbuettel, Romain François and Iñaki Ucar
##
## This file is part of Rcpp.
##
Expand Down Expand Up @@ -702,3 +703,8 @@ expect_true( !CharacterVector_test_equality_crosspolicy("foo", "bar") )
#expect_warning(CharacterVector_test_out_of_bounds_read(character(0), 0))
#expect_warning(CharacterVector_test_out_of_bounds_read(character(1), 1))


# https://github.com/RcppCore/Rcpp/issues/1461
expect_equal(vec_copy(as.numeric(1:10)), as.numeric(1:10))
expect_equal(vec_copy(numeric(0)), numeric(0))

Loading