Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
also implement bounds checking in vector access
  • Loading branch information
kevinushey committed Jun 11, 2024
commit d48a6ed7765c2703bba833bb6a5566302128bc30
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* inst/include/Rcpp/internal/r_vector.h: Use template specializations to
avoid DATAPTR usage
* inst/include/Rcpp/vector/traits.h: Implement bounds checks in
r_vector_cache access

2024-06-02 Dirk Eddelbuettel <edd@debian.org>

Expand Down
53 changes: 45 additions & 8 deletions inst/include/Rcpp/vector/traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,59 @@ namespace traits{
typedef typename r_vector_const_proxy<RTYPE>::type const_proxy ;
typedef typename storage_type<RTYPE>::type storage_type ;

r_vector_cache() : start(0){} ;
r_vector_cache() : start(0), size(0) {} ;

inline void update( const VECTOR& v ) {
start = ::Rcpp::internal::r_vector_start<RTYPE>(v) ;
start = ::Rcpp::internal::r_vector_start<RTYPE>(v) ;
size = v.size();
}

inline iterator get() const { return start; }
inline const_iterator get_const() const { return start; }

inline proxy ref() { return *start ;}
inline proxy ref(R_xlen_t i) { return start[i] ; }
inline proxy ref() {
#ifndef RCPP_NO_BOUNDS_CHECK
check_index(0) ;
#endif
return start[0];
}

inline proxy ref() const {
#ifndef RCPP_NO_BOUNDS_CHECK
check_index(0) ;
#endif
return start[0] ;
}

inline proxy ref(R_xlen_t i) {
#ifndef RCPP_NO_BOUNDS_CHECK
check_index(i) ;
#endif
return start[i] ;
}

inline proxy ref() const { return *start ;}
inline proxy ref(R_xlen_t i) const { return start[i] ; }

private:
iterator start ;
inline proxy ref(R_xlen_t i) const {
#ifndef RCPP_NO_BOUNDS_CHECK
check_index(i) ;
#endif
return start[i] ;
}

private:

#ifndef RCPP_NO_BOUNDS_CHECK
void check_index(R_xlen_t i) const {
if (i >= size) {
stop("index error");
}
}
#endif

iterator start ;
R_xlen_t size ;
} ;

template <int RTYPE, template <class> class StoragePolicy = PreserveStorage>
class proxy_cache{
public:
Expand Down