GeezerVison is showing github.com — src: use arraysize instead of hardcode number · nodejs/node@ecbe616
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 |
| @@ -219,7 +220,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) { | 219 | 220 | int count, i; | 220 | 221 | char ip[INET6_ADDRSTRLEN]; | 221 | 222 | char netmask[INET6_ADDRSTRLEN]; | 222 | | - char mac[18]; | | 223 | + std::array<char, 18> mac; | 223 | 224 | Local<Object> ret, o; | 224 | 225 | Local<String> name, family; | 225 | 226 | Local<Array> ifarr; | @@ -256,8 +257,8 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) { | 256 | 257 | ret->Set(env->context(), name, ifarr).FromJust(); | 257 | 258 | } | 258 | 259 |
| 259 | | - snprintf(mac, | 260 | | - 18, | | 260 | + snprintf(mac.data(), | | 261 | + mac.size(), | 261 | 262 | "%02x:%02x:%02x:%02x:%02x:%02x", | 262 | 263 | static_cast<unsigned char>(interfaces[i].phys_addr[0]), | 263 | 264 | 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 |
|---|
@@ -787,10 +787,11 @@ inline bool ToASCII(const std::string& input, std::string* output) { | 787 | 787 |
| 788 | 788 | void URLHost::ParseIPv6Host(const char* input, size_t length) { | 789 | 789 | CHECK_EQ(type_, HostType::H_FAILED); | 790 | | - for (unsigned n = 0; n < 8; n++) | | 790 | + unsigned size = arraysize(value_.ipv6); | | 791 | + for (unsigned n = 0; n < size; n++) | 791 | 792 | value_.ipv6[n] = 0; | 792 | 793 | uint16_t* piece_pointer = &value_.ipv6[0]; | 793 | | - uint16_t* const buffer_end = piece_pointer + 8; | | 794 | + uint16_t* const buffer_end = piece_pointer + size; | 794 | 795 | uint16_t* compress_pointer = nullptr; | 795 | 796 | const char* pointer = input; | 796 | 797 | const char* end = pointer + length; | @@ -952,7 +953,7 @@ void URLHost::ParseIPv4Host(const char* input, size_t length, bool* is_ipv4) { | 952 | 953 | const char ch = pointer < end ? pointer[0] : kEOL; | 953 | 954 | const int remaining = end - pointer - 1; | 954 | 955 | if (ch == '.' || ch == kEOL) { | 955 | | - if (++parts > 4) | | 956 | + if (++parts > static_cast<int>(arraysize(numbers))) | 956 | 957 | return; | 957 | 958 | if (pointer == mark) | 958 | 959 | 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 { | @@ -223,6 +224,14 @@ inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING( | 223 | 224 | return OneByteString(isolate, data, N - 1); | 224 | 225 | } | 225 | 226 |
| | 227 | +template <std::size_t N> | | 228 | +inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING( | | 229 | + v8::Isolate* isolate, | | 230 | + const std::array<char, N>& arr) { | | 231 | + return OneByteString(isolate, arr.data(), N - 1); | | 232 | +} | | 233 | + | | 234 | + | 226 | 235 |
| 227 | 236 | // Swaps bytes in place. nbytes is the number of bytes to swap and must be a | 228 | 237 | // multiple of the word size (checked by function). | |
|