Skip to content

Inf outside of representable range of type 'int' in sugar functions #1455

@Enchufa2

Description

@Enchufa2

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions