Again, from the full test suite under the UBSAN container, as reported by @eddelbuettel:
test_sugar.R.................. 452 tests OK /usr/local/lib/R/site-library/Rcpp/include/Rcpp/sugar/functions/min.h:36:53: runtime error: inf is outside the range of representable values of type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/local/lib/R/site-library/Rcpp/include/Rcpp/sugar/functions/min.h:36:53
test_sugar.R.................. 454 tests OK /usr/local/lib/R/site-library/Rcpp/include/Rcpp/sugar/functions/max.h:36:53: runtime error: -inf is outside the range of representable values of type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/local/lib/R/site-library/Rcpp/include/Rcpp/sugar/functions/max.h:36:53
Same issue: we are returning R_PosInf/R_NegInf if the object has no elements:
|
Max( const T& obj_) : obj(obj_) {} |
|
|
|
operator STORAGE() const { |
|
R_xlen_t n = obj.size(); |
|
if (n == 0) return(static_cast<STORAGE>(R_NegInf)); |
|
|
|
STORAGE max, current ; |
|
max = obj[0] ; |
|
if( Rcpp::traits::is_na<RTYPE>( max ) ) return max ; |
|
for( R_xlen_t i=1; i<n; i++){ |
|
current = obj[i] ; |
|
if( Rcpp::traits::is_na<RTYPE>( current ) ) return current; |
|
if( current > max ) max = current ; |
|
} |
|
return max ; |
|
} |
So one possible approach would be similar to this one:
|
Comparator_With_One_Value( const VEC_TYPE& lhs_, STORAGE rhs_ ) : |
|
lhs(lhs_), rhs(rhs_), m(), op() { |
|
|
|
m = Rcpp::traits::is_na<RTYPE>(rhs) ? |
|
&Comparator_With_One_Value::rhs_is_na : |
|
&Comparator_With_One_Value::rhs_is_not_na ; |
|
|
|
} |
I.e., check the object size on object creation and assign the proper operator: one that returns a double with infinity if the object has no elements, or one that returns STORAGE with the computed min/max.
Maybe there's a more sophisticated solution using std::enable_if.
Again, from the full test suite under the UBSAN container, as reported by @eddelbuettel:
Same issue: we are returning
R_PosInf/R_NegInfif the object has no elements:Rcpp/inst/include/Rcpp/sugar/functions/max.h
Lines 32 to 47 in 7ff6ba2
So one possible approach would be similar to this one:
Rcpp/inst/include/Rcpp/sugar/operators/Comparator_With_One_Value.h
Lines 36 to 43 in 7ff6ba2
I.e., check the object size on object creation and assign the proper operator: one that returns a
doublewith infinity if the object has no elements, or one that returnsSTORAGEwith the computed min/max.Maybe there's a more sophisticated solution using
std::enable_if.