Skip to content

Commit 9546fcc

Browse files
committed
fix: avoid conversion issue for max double limits
1 parent 3288ea0 commit 9546fcc

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/socket.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@ uint64_t NumberCast<uint64_t>(const Napi::Number& num) {
3838
return 0;
3939
}
4040

41-
if (value > static_cast<double>((1ULL << 53) - 1)) {
41+
static constexpr auto max_safe_integer = static_cast<double>((1ULL << 53) - 1);
42+
if (value > max_safe_integer) {
4243
Warn(num.Env(),
4344
"Value is larger than Number.MAX_SAFE_INTEGER and may have been rounded "
4445
"inaccurately.");
4546
}
4647

4748
/* If the next representable value of the double is beyond the maximum
4849
integer, then assume the maximum integer. */
50+
static constexpr auto max_uint64_as_double =
51+
static_cast<double>(std::numeric_limits<uint64_t>::max());
4952
if (std::nextafter(value, std::numeric_limits<double>::infinity())
50-
> std::numeric_limits<uint64_t>::max()) {
53+
> max_uint64_as_double) {
5154
return std::numeric_limits<uint64_t>::max();
5255
}
5356

0 commit comments

Comments
 (0)