# HG changeset patch # User Markus Mützel # Date 1648059955 -3600 # Wed Mar 23 19:25:55 2022 +0100 # Branch stable # Node ID 264c9e31c04c7b16b566afa62504ed51d4c25a6a # Parent df26decca96bc6a01dd70e9244bd034097c97776 wip! do not push! diff -r df26decca96b -r 264c9e31c04c libinterp/corefcn/oct-stream.cc --- a/libinterp/corefcn/oct-stream.cc Wed Mar 23 18:27:53 2022 +0100 +++ b/libinterp/corefcn/oct-stream.cc Wed Mar 23 19:25:55 2022 +0100 @@ -140,7 +140,10 @@ ::error ("%s: negative value invalid as size specification", who.c_str ()); - if (d > std::numeric_limits::max ()) + static const double out_of_range_top + = static_cast(std::numeric_limits::max ()) + + 1.; + if (d >= out_of_range_top) ::error ("%s: dimension too large for Octave's index type", who.c_str ()); diff -r df26decca96b -r 264c9e31c04c libinterp/corefcn/xpow.cc --- a/libinterp/corefcn/xpow.cc Wed Mar 23 18:27:53 2022 +0100 +++ b/libinterp/corefcn/xpow.cc Wed Mar 23 19:25:55 2022 +0100 @@ -76,8 +76,18 @@ xisint (T x) { return (octave::math::x_nint (x) == x - && ((x >= 0 && x < std::numeric_limits::max ()) - || (x <= 0 && x > std::numeric_limits::min ()))); + && x <= std::numeric_limits::max () + && x >= std::numeric_limits::min ()); +} + +static inline bool +xisint (float x) +{ + static const float out_of_range_top + = static_cast(std::numeric_limits::max ()) + 1.; + return (octave::math::x_nint (x) == x + && x < out_of_range_top + && x >= std::numeric_limits::min ()); } // Safer pow functions. diff -r df26decca96b -r 264c9e31c04c libinterp/octave-value/ov-base.cc --- a/libinterp/octave-value/ov-base.cc Wed Mar 23 18:27:53 2022 +0100 +++ b/libinterp/octave-value/ov-base.cc Wed Mar 23 19:25:55 2022 +0100 @@ -475,11 +475,13 @@ err_wrong_type_arg (ee, "octave_base_value::" #F "_value ()", type_name ()); \ } \ \ + static const double out_of_range_top \ + = static_cast(std::numeric_limits::max ()) + 1.; \ if (require_int && octave::math::x_nint (d) != d) \ error_with_cfn ("conversion of %g to " #T " value failed", d); \ else if (d < std::numeric_limits::min ()) \ retval = std::numeric_limits::min (); \ - else if (d > std::numeric_limits::max ()) \ + else if (d >= out_of_range_top) \ retval = std::numeric_limits::max (); \ else \ retval = static_cast (octave::math::fix (d)); \ diff -r df26decca96b -r 264c9e31c04c libinterp/octave-value/ov.cc --- a/libinterp/octave-value/ov.cc Wed Mar 23 18:27:53 2022 +0100 +++ b/libinterp/octave-value/ov.cc Wed Mar 23 19:25:55 2022 +0100 @@ -3073,8 +3073,11 @@ double dval = val.double_value (); double intpart; - - if (dval > std::numeric_limits::max () + static const double out_of_range_top + = static_cast (std::numeric_limits::max ()) + + 1.; + + if (dval >= out_of_range_top || dval < std::numeric_limits::min () || std::modf (dval, &intpart) != 0.0) error ("colon operator %s invalid (not an integer or out of range for given integer type)", op_str); @@ -3246,16 +3249,18 @@ || (increment < 0 && base < limit)) return 0; - static const UT max_val = std::numeric_limits::max (); + static const double out_of_range_top + = static_cast (std::numeric_limits::max ()) + 1.; double abs_increment = std::abs (increment); - // Technically, this condition should be `abs_increment > max_val`. + // Technically, this condition should be + // `abs_increment > std::numeric_limits::max ()`. // But intmax('uint64') is not representable exactly as floating point // number. Instead, it "rounds" up by 1 to 2^64. To account for // this, use the following expression which works for all unsigned // integer types. - if ((abs_increment-1.) >= max_val) + if (abs_increment >= out_of_range_top) return 1; UT unsigned_increment = range_increment (increment); diff -r df26decca96b -r 264c9e31c04c liboctave/numeric/lo-mappers.cc --- a/liboctave/numeric/lo-mappers.cc Wed Mar 23 18:27:53 2022 +0100 +++ b/liboctave/numeric/lo-mappers.cc Wed Mar 23 19:25:55 2022 +0100 @@ -183,7 +183,9 @@ octave_idx_type nint_big (double x) { - if (x > std::numeric_limits::max ()) + static const double out_of_range_top + = static_cast(std::numeric_limits::max ())+1.; + if (x >= out_of_range_top) return std::numeric_limits::max (); else if (x < std::numeric_limits::min ()) return std::numeric_limits::min (); @@ -195,7 +197,9 @@ octave_idx_type nint_big (float x) { - if (x > std::numeric_limits::max ()) + static const float out_of_range_top + = static_cast(std::numeric_limits::max ())+1.; + if (x >= out_of_range_top) return std::numeric_limits::max (); else if (x < std::numeric_limits::min ()) return std::numeric_limits::min (); @@ -218,7 +222,9 @@ int nint (float x) { - if (x > std::numeric_limits::max ()) + static const float out_of_range_top + = static_cast(std::numeric_limits::max ()) + 1.; + if (x >= out_of_range_top) return std::numeric_limits::max (); else if (x < std::numeric_limits::min ()) return std::numeric_limits::min ();