Skip to content

Commit fa4f833

Browse files
committed
fix operators/minus.h
1 parent 638f329 commit fa4f833

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

inst/include/Rcpp/sugar/operators/minus.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//
33
// minus.h: Rcpp R/C++ interface class library -- operator-
44
//
5-
// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2010 - 2025 Dirk Eddelbuettel and Romain Francois
6+
// Copyright (C) 2026 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
67
//
78
// This file is part of Rcpp.
89
//
@@ -42,7 +43,7 @@ namespace sugar{
4243
STORAGE x = lhs[i] ;
4344
if( Rcpp::traits::is_na<RTYPE>( x ) ) return x ;
4445
STORAGE y = rhs[i] ;
45-
return Rcpp::traits::is_na<RTYPE>( y ) ? y : ( x - y ) ;
46+
return Rcpp::traits::is_na<RTYPE>( y ) ? y : RCPP_SAFE_SUB(x, y);
4647
}
4748

4849
inline R_xlen_t size() const { return lhs.size() ; }
@@ -91,7 +92,7 @@ namespace sugar{
9192
inline STORAGE operator[]( R_xlen_t i ) const {
9293
STORAGE y = rhs[i] ;
9394
if( Rcpp::traits::is_na<RTYPE>( y ) ) return y ;
94-
return lhs[i] - y ;
95+
return RCPP_SAFE_SUB(lhs[i], y);
9596
}
9697

9798
inline R_xlen_t size() const { return lhs.size() ; }
@@ -140,7 +141,7 @@ namespace sugar{
140141
inline STORAGE operator[]( R_xlen_t i ) const {
141142
STORAGE x = lhs[i] ;
142143
if( Rcpp::traits::is_na<RTYPE>( x ) ) return x ;
143-
return x - rhs[i] ;
144+
return RCPP_SAFE_SUB(x, rhs[i]);
144145
}
145146

146147
inline R_xlen_t size() const { return lhs.size() ; }
@@ -188,7 +189,7 @@ namespace sugar{
188189
lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
189190

190191
inline STORAGE operator[]( R_xlen_t i ) const {
191-
return lhs[i] - rhs[i] ;
192+
return RCPP_SAFE_SUB(lhs[i], rhs[i]);
192193
}
193194

194195
inline R_xlen_t size() const { return lhs.size() ; }
@@ -238,7 +239,7 @@ namespace sugar{
238239
inline STORAGE operator[]( R_xlen_t i ) const {
239240
if( rhs_na ) return rhs ;
240241
STORAGE x = lhs[i] ;
241-
return Rcpp::traits::is_na<RTYPE>(x) ? x : (x - rhs) ;
242+
return Rcpp::traits::is_na<RTYPE>(x) ? x : RCPP_SAFE_SUB(x, rhs);
242243
}
243244

244245
inline R_xlen_t size() const { return lhs.size() ; }
@@ -284,7 +285,7 @@ namespace sugar{
284285
inline STORAGE operator[]( R_xlen_t i ) const {
285286
if( rhs_na ) return rhs ;
286287
STORAGE x = lhs[i] ;
287-
return Rcpp::traits::is_na<RTYPE>(x) ? x : (x - rhs) ;
288+
return Rcpp::traits::is_na<RTYPE>(x) ? x : RCPP_SAFE_SUB(x, rhs);
288289
}
289290

290291
inline R_xlen_t size() const { return lhs.size() ; }
@@ -333,7 +334,7 @@ namespace sugar{
333334

334335
inline STORAGE operator[]( R_xlen_t i ) const {
335336
if( lhs_na ) return lhs ;
336-
return lhs - rhs[i] ;
337+
return RCPP_SAFE_SUB(lhs, rhs[i]);
337338
}
338339
inline R_xlen_t size() const { return rhs.size() ; }
339340

@@ -377,7 +378,7 @@ namespace sugar{
377378

378379
inline STORAGE operator[]( R_xlen_t i ) const {
379380
if( lhs_na ) return lhs ;
380-
return lhs - rhs[i] ;
381+
return RCPP_SAFE_SUB(lhs, rhs[i]);
381382
}
382383

383384
inline R_xlen_t size() const { return rhs.size() ; }

inst/tinytest/cpp/sugar.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,21 @@ List runit_minus( IntegerVector xx ){
285285
) ;
286286
}
287287

288+
// [[Rcpp::export]]
289+
IntegerVector runit_minus_ivv( IntegerVector x, IntegerVector y ){
290+
return x - y;
291+
}
292+
293+
// [[Rcpp::export]]
294+
IntegerVector runit_minus_ivp( IntegerVector x, int y ){
295+
return x - y;
296+
}
297+
298+
// [[Rcpp::export]]
299+
IntegerVector runit_minus_ipv( int x, IntegerVector y ){
300+
return x - y;
301+
}
302+
288303
// [[Rcpp::export]]
289304
LogicalVector runit_any_equal_not( NumericVector xx, NumericVector yy){
290305
return any( !( xx == yy) ) ;

inst/tinytest/test_sugar.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ expect_equal( fx(1:10, 1:10*2) , mapply(seq, 1:10, 1:10*2) )
367367

368368

369369
# test.sugar.minus <- function( ){
370+
expect_error(runit_minus_ivv(-.Machine$integer.max, 2), "overflow")
371+
expect_error(runit_minus_ivp(-.Machine$integer.max, 2), "overflow")
372+
expect_error(runit_minus_ipv(-.Machine$integer.max, 2), "overflow")
370373
fx <- runit_minus
371374
expect_equal(fx(1:10) ,
372375
list( (1:10)-10L, 10L-(1:10), rep(0L,10), (1:10)-10L, 10L-(1:10) ))

0 commit comments

Comments
 (0)