GeezerVison is showing github.com — src: use arraysize instead of hardcode number · nodejs/node@37047fc
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_os.cc +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | #include "node_internals.h" | 23 | 23 | #include "string_bytes.h" | 24 | 24 |
| | 25 | +#include <array> | 25 | 26 | #include <errno.h> | 26 | 27 | #include <string.h> | 27 | 28 |
| @@ -237,7 +238,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) { | 237 | 238 | int count, i; | 238 | 239 | char ip[INET6_ADDRSTRLEN]; | 239 | 240 | char netmask[INET6_ADDRSTRLEN]; | 240 | | - char mac[18]; | | 241 | + std::array<char, 18> mac; | 241 | 242 | Local<Object> ret, o; | 242 | 243 | Local<String> name, family; | 243 | 244 | Local<Array> ifarr; | @@ -273,8 +274,8 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) { | 273 | 274 | ret->Set(name, ifarr); | 274 | 275 | } | 275 | 276 |
| 276 | | - snprintf(mac, | 277 | | - 18, | | 277 | + snprintf(mac.data(), | | 278 | + mac.size(), | 278 | 279 | "%02x:%02x:%02x:%02x:%02x:%02x", | 279 | 280 | static_cast<unsigned char>(interfaces[i].phys_addr[0]), | 280 | 281 | static_cast<unsigned char>(interfaces[i].phys_addr[1]), | |
Collapse file Copy file name to clipboardExpand all lines: src/node_url.cc +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -786,10 +786,11 @@ inline bool ToASCII(const std::string& input, std::string* output) { | 786 | 786 |
| 787 | 787 | void URLHost::ParseIPv6Host(const char* input, size_t length) { | 788 | 788 | CHECK_EQ(type_, HostType::H_FAILED); | 789 | | - for (unsigned n = 0; n < 8; n++) | | 789 | + unsigned size = arraysize(value_.ipv6); | | 790 | + for (unsigned n = 0; n < size; n++) | 790 | 791 | value_.ipv6[n] = 0; | 791 | 792 | uint16_t* piece_pointer = &value_.ipv6[0]; | 792 | | - uint16_t* const buffer_end = piece_pointer + 8; | | 793 | + uint16_t* const buffer_end = piece_pointer + size; | 793 | 794 | uint16_t* compress_pointer = nullptr; | 794 | 795 | const char* pointer = input; | 795 | 796 | const char* end = pointer + length; | @@ -951,7 +952,7 @@ void URLHost::ParseIPv4Host(const char* input, size_t length, bool* is_ipv4) { | 951 | 952 | const char ch = pointer < end ? pointer[0] : kEOL; | 952 | 953 | const int remaining = end - pointer - 1; | 953 | 954 | if (ch == '.' || ch == kEOL) { | 954 | | - if (++parts > 4) | | 955 | + if (++parts > static_cast<int>(arraysize(numbers))) | 955 | 956 | return; | 956 | 957 | if (pointer == mark) | 957 | 958 | return; | |
Collapse file Copy file name to clipboardExpand all lines: src/util.h +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 37 | 37 | #include <functional> // std::function | 38 | 38 | #include <set> | 39 | 39 | #include <string> | | 40 | +#include <array> | 40 | 41 | #include <unordered_map> | 41 | 42 |
| 42 | 43 | namespace node { | @@ -245,6 +246,14 @@ inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING( | 245 | 246 | return OneByteString(isolate, data, N - 1); | 246 | 247 | } | 247 | 248 |
| | 249 | +template <std::size_t N> | | 250 | +inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING( | | 251 | + v8::Isolate* isolate, | | 252 | + const std::array<char, N>& arr) { | | 253 | + return OneByteString(isolate, arr.data(), N - 1); | | 254 | +} | | 255 | + | | 256 | + | 248 | 257 |
| 249 | 258 | // Swaps bytes in place. nbytes is the number of bytes to swap and must be a | 250 | 259 | // multiple of the word size (checked by function). | |
|