From 49ada8226da65d220f280a417f4178e8650fdbe5 Mon Sep 17 00:00:00 2001 From: leeight Date: Mon, 19 Nov 2018 12:31:26 +0800 Subject: [PATCH 1/2] src: use arraysize instead of hardcode number --- src/node_os.cc | 7 ++++--- src/node_url.cc | 7 ++++--- src/util.h | 9 +++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/node_os.cc b/src/node_os.cc index 9e5530646a3708..fa38040e3af64a 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -22,6 +22,7 @@ #include "node_internals.h" #include "string_bytes.h" +#include #include #include @@ -219,7 +220,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo& args) { int count, i; char ip[INET6_ADDRSTRLEN]; char netmask[INET6_ADDRSTRLEN]; - char mac[18]; + std::array mac; Local ret, o; Local name, family; Local ifarr; @@ -256,8 +257,8 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo& args) { ret->Set(env->context(), name, ifarr).FromJust(); } - snprintf(mac, - 18, + snprintf(mac.data(), + mac.size(), "%02x:%02x:%02x:%02x:%02x:%02x", static_cast(interfaces[i].phys_addr[0]), static_cast(interfaces[i].phys_addr[1]), diff --git a/src/node_url.cc b/src/node_url.cc index a3f22f7c983e7d..6bf11897d8f2fa 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -786,10 +786,11 @@ inline bool ToASCII(const std::string& input, std::string* output) { void URLHost::ParseIPv6Host(const char* input, size_t length) { CHECK_EQ(type_, HostType::H_FAILED); - for (unsigned n = 0; n < 8; n++) + unsigned size = arraysize(value_.ipv6); + for (unsigned n = 0; n < size; n++) value_.ipv6[n] = 0; uint16_t* piece_pointer = &value_.ipv6[0]; - uint16_t* const buffer_end = piece_pointer + 8; + uint16_t* const buffer_end = piece_pointer + size; uint16_t* compress_pointer = nullptr; const char* pointer = input; const char* end = pointer + length; @@ -951,7 +952,7 @@ void URLHost::ParseIPv4Host(const char* input, size_t length, bool* is_ipv4) { const char ch = pointer < end ? pointer[0] : kEOL; const int remaining = end - pointer - 1; if (ch == '.' || ch == kEOL) { - if (++parts > 4) + if (++parts > static_cast(arraysize(numbers))) return; if (pointer == mark) return; diff --git a/src/util.h b/src/util.h index 086e33933e6b5a..d0e8fe19db484c 100644 --- a/src/util.h +++ b/src/util.h @@ -37,6 +37,7 @@ #include // std::function #include #include +#include #include namespace node { @@ -223,6 +224,14 @@ inline v8::Local FIXED_ONE_BYTE_STRING( return OneByteString(isolate, data, N - 1); } +template +inline v8::Local FIXED_ONE_BYTE_STRING( + v8::Isolate* isolate, + std::array arr) { + return OneByteString(isolate, arr.data(), N - 1); +} + + // Swaps bytes in place. nbytes is the number of bytes to swap and must be a // multiple of the word size (checked by function). From c0a94ef3c92a422c44a5c123efae9bdb6476ebda Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 24 Nov 2018 20:32:22 +0800 Subject: [PATCH 2/2] src: Update src/util.h Co-Authored-By: leeight --- src/util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.h b/src/util.h index d0e8fe19db484c..9dd39fe1860b8e 100644 --- a/src/util.h +++ b/src/util.h @@ -227,7 +227,7 @@ inline v8::Local FIXED_ONE_BYTE_STRING( template inline v8::Local FIXED_ONE_BYTE_STRING( v8::Isolate* isolate, - std::array arr) { + const std::array& arr) { return OneByteString(isolate, arr.data(), N - 1); }