Skip to content

Commit ecbe616

Browse files
leeightBridgeAR
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 026e03c commit ecbe616

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

@@ -219,7 +220,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
219220
int count, i;
220221
char ip[INET6_ADDRSTRLEN];
221222
char netmask[INET6_ADDRSTRLEN];
222-
char mac[18];
223+
std::array<char, 18> mac;
223224
Local<Object> ret, o;
224225
Local<String> name, family;
225226
Local<Array> ifarr;
@@ -256,8 +257,8 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
256257
ret->Set(env->context(), name, ifarr).FromJust();
257258
}
258259

259-
snprintf(mac,
260-
18,
260+
snprintf(mac.data(),
261+
mac.size(),
261262
"%02x:%02x:%02x:%02x:%02x:%02x",
262263
static_cast<unsigned char>(interfaces[i].phys_addr[0]),
263264
static_cast<unsigned char>(interfaces[i].phys_addr[1]),

src/node_url.cc

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

788788
void URLHost::ParseIPv6Host(const char* input, size_t length) {
789789
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++)
791792
value_.ipv6[n] = 0;
792793
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;
794795
uint16_t* compress_pointer = nullptr;
795796
const char* pointer = input;
796797
const char* end = pointer + length;
@@ -952,7 +953,7 @@ void URLHost::ParseIPv4Host(const char* input, size_t length, bool* is_ipv4) {
952953
const char ch = pointer < end ? pointer[0] : kEOL;
953954
const int remaining = end - pointer - 1;
954955
if (ch == '.' || ch == kEOL) {
955-
if (++parts > 4)
956+
if (++parts > static_cast<int>(arraysize(numbers)))
956957
return;
957958
if (pointer == mark)
958959
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 {
@@ -223,6 +224,14 @@ inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING(
223224
return OneByteString(isolate, data, N - 1);
224225
}
225226

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

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

0 commit comments

Comments
 (0)