[gjs: 1/3] js-value-inl: Consider NaN results to be out of range values for integers




commit d3d365ba7cce1ccacaffab97ef91afe9c494528c
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Mon Mar 8 21:58:25 2021 +0100

    js-value-inl: Consider NaN results to be out of range values for integers
    
    When converting to integers using a bigger container (such as double) we
    may end up having our value to be set to NaN, which JS::ToNumber would
    return in case the JS value is undefined.
    
    In such case we should avoid to silently use the NaN value, but instead
    mark it as an out of range value as well.

 gi/js-value-inl.h | 3 +++
 1 file changed, 3 insertions(+)
---
diff --git a/gi/js-value-inl.h b/gi/js-value-inl.h
index ac00f172..da1d5135 100644
--- a/gi/js-value-inl.h
+++ b/gi/js-value-inl.h
@@ -205,6 +205,9 @@ GJS_JSAPI_RETURN_CONVENTION inline bool js_value_to_c_checked(
                      static_cast<T>(std::numeric_limits<WantedType>::max()) ||
                  *out <
                      static_cast<T>(std::numeric_limits<WantedType>::lowest()));
+
+            if constexpr (std::is_integral_v<WantedType>)
+                *out_of_range |= std::isnan(*out);
         }
         return ret;
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]