Skip to content

Commit 37047fc

Browse files
leeightBethGriggs
authored andcommitted
src: use arraysize instead of hardcode number
PR-URL: #24473 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 31c1ee4 commit 37047fc

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/node_os.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "node_internals.h"
2323
#include "string_bytes.h"
2424

25+
#include <array>
2526
#include <errno.h>
2627
#include <string.h>
2728

@@ -237,7 +238,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
237238
int count, i;
238239
char ip[INET6_ADDRSTRLEN];
239240
char netmask[INET6_ADDRSTRLEN];
240-
char mac[18];
241+
std::array<char, 18> mac;
241242
Local<Object> ret, o;
242243
Local<String> name, family;
243244
Local<Array> ifarr;
@@ -273,8 +274,8 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
273274
ret->Set(name, ifarr);
274275
}
275276

276-
snprintf(mac,
277-
18,
277+
snprintf(mac.data(),
278+
mac.size(),
278279
"%02x:%02x:%02x:%02x:%02x:%02x",
279280
static_cast<unsigned char>(interfaces[i].phys_addr[0]),
280281
static_cast<unsigned char>(interfaces[i].phys_addr[1]),

src/node_url.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -786,10 +786,11 @@ inline bool ToASCII(const std::string& input, std::string* output) {
786786

787787
void URLHost::ParseIPv6Host(const char* input, size_t length) {
788788
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++)
790791
value_.ipv6[n] = 0;
791792
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;
793794
uint16_t* compress_pointer = nullptr;
794795
const char* pointer = input;
795796
const char* end = pointer + length;
@@ -951,7 +952,7 @@ void URLHost::ParseIPv4Host(const char* input, size_t length, bool* is_ipv4) {
951952
const char ch = pointer < end ? pointer[0] : kEOL;
952953
const int remaining = end - pointer - 1;
953954
if (ch == '.' || ch == kEOL) {
954-
if (++parts > 4)
955+
if (++parts > static_cast<int>(arraysize(numbers)))
955956
return;
956957
if (pointer == mark)
957958
return;

src/util.h

+9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <functional> // std::function
3838
#include <set>
3939
#include <string>
40+
#include <array>
4041
#include <unordered_map>
4142

4243
namespace node {
@@ -245,6 +246,14 @@ inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING(
245246
return OneByteString(isolate, data, N - 1);
246247
}
247248

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+
248257

249258
// Swaps bytes in place. nbytes is the number of bytes to swap and must be a
250259
// multiple of the word size (checked by function).

0 commit comments

Comments
 (0)