From 4341b6f1b80036c2dfa74f20158ccfdca4f498f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Tue, 24 Jan 2023 01:18:57 +0000 Subject: [PATCH] src: get rid of fp arithmetic in ParseIPv4Host Even though most compiler should not actually emit FPU instructions, it is unnecessary to use floating-point arithmetic for powers of 2. Also change some signed counters to unsigned integers. --- src/node_url.cc | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/node_url.cc b/src/node_url.cc index d9e8a9b59ea391..b8edc0c21d7e5b 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -6,7 +6,6 @@ #include "util-inl.h" #include -#include #include #include #include @@ -477,10 +476,10 @@ void URLHost::ParseIPv4Host(const char* input, size_t length) { const char* pointer = input; const char* mark = input; const char* end = pointer + length; - int parts = 0; + unsigned int parts = 0; uint32_t val = 0; uint64_t numbers[4]; - int tooBigNumbers = 0; + unsigned int tooBigNumbers = 0; if (length == 0) return; @@ -488,7 +487,7 @@ void URLHost::ParseIPv4Host(const char* input, size_t length) { const char ch = pointer < end ? pointer[0] : kEOL; int64_t remaining = end - pointer - 1; if (ch == '.' || ch == kEOL) { - if (++parts > static_cast(arraysize(numbers))) return; + if (++parts > arraysize(numbers)) return; if (pointer == mark) return; int64_t n = ParseIPv4Number(mark, pointer); @@ -510,18 +509,15 @@ void URLHost::ParseIPv4Host(const char* input, size_t length) { // If any but the last item in numbers is greater than 255, return failure. // If the last item in numbers is greater than or equal to // 256^(5 - the number of items in numbers), return failure. - if (tooBigNumbers > 1 || - (tooBigNumbers == 1 && numbers[parts - 1] <= 255) || - numbers[parts - 1] >= pow(256, static_cast(5 - parts))) { + if (tooBigNumbers > 1 || (tooBigNumbers == 1 && numbers[parts - 1] <= 255) || + numbers[parts - 1] >= UINT64_C(1) << (8 * (5 - parts))) { return; } type_ = HostType::H_IPV4; val = static_cast(numbers[parts - 1]); - for (int n = 0; n < parts - 1; n++) { - double b = 3 - n; - val += - static_cast(numbers[n]) * static_cast(pow(256, b)); + for (unsigned int n = 0; n < parts - 1; n++) { + val += static_cast(numbers[n]) << (8 * (3 - n)); } value_.ipv4 = val;