From 6cab64daba6d0c2a39a7c5fca8014da12ad5e3ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20=C3=9Acar?= Date: Thu, 26 Mar 2026 17:18:06 +0100 Subject: [PATCH] Return null start for empty vectors --- ChangeLog | 5 +++++ inst/include/Rcpp/internal/r_vector.h | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 24f115bb9..3a6208966 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2026-03-26 Iñaki Ucar + + * inst/include/Rcpp/internal/r_vector.h: Return null start for empty vectors + instead of an invalid pointer, which causes UB in e.g. std::copy + 2026-03-06 Dirk Eddelbuettel * DESCRIPTION (Version, Date): Roll micro version and date (twice) diff --git a/inst/include/Rcpp/internal/r_vector.h b/inst/include/Rcpp/internal/r_vector.h index 589c75fed..c6a76b6ce 100644 --- a/inst/include/Rcpp/internal/r_vector.h +++ b/inst/include/Rcpp/internal/r_vector.h @@ -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. // @@ -36,6 +37,8 @@ typename Rcpp::traits::storage_type::type* r_vector_start(SEXP x) { #define RCPP_VECTOR_START_IMPL(__RTYPE__, __ACCESSOR__) \ template <> \ inline typename Rcpp::traits::storage_type<__RTYPE__>::type* r_vector_start<__RTYPE__>(SEXP x) { \ + if (Rf_xlength(x) == 0) \ + return NULL; \ return __ACCESSOR__(x); \ }