Skip to content

Commit 2fd8be2

Browse files
bnoordhuisMyles Borins
authored and
Myles Borins
committed
src: replace ARRAY_SIZE with typesafe arraysize
To prevent `ARRAY_SIZE(&arg)` (i.e., taking the array size of a pointer) from happening again. PR-URL: #5969 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 60a73a2 commit 2fd8be2

25 files changed

+80
-77
lines changed

src/async-wrap-inl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ inline AsyncWrap::AsyncWrap(Environment* env,
5454
v8::TryCatch try_catch(env->isolate());
5555

5656
v8::MaybeLocal<v8::Value> ret =
57-
init_fn->Call(env->context(), object, ARRAY_SIZE(argv), argv);
57+
init_fn->Call(env->context(), object, arraysize(argv), argv);
5858

5959
if (ret.IsEmpty()) {
6060
ClearFatalExceptionHandlers(env);

src/async-wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
242242
Local<Value> vals[] = { uid, did_throw };
243243
TryCatch try_catch(env()->isolate());
244244
MaybeLocal<Value> ar =
245-
post_fn->Call(env()->context(), context, ARRAY_SIZE(vals), vals);
245+
post_fn->Call(env()->context(), context, arraysize(vals), vals);
246246
if (ar.IsEmpty()) {
247247
ClearFatalExceptionHandlers(env());
248248
FatalException(env()->isolate(), try_catch);

src/cares_wrap.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ class QueryWrap : public AsyncWrap {
310310
Integer::New(env()->isolate(), 0),
311311
answer
312312
};
313-
MakeCallback(env()->oncomplete_string(), ARRAY_SIZE(argv), argv);
313+
MakeCallback(env()->oncomplete_string(), arraysize(argv), argv);
314314
}
315315

316316
void CallOnComplete(Local<Value> answer, Local<Value> family) {
@@ -321,7 +321,7 @@ class QueryWrap : public AsyncWrap {
321321
answer,
322322
family
323323
};
324-
MakeCallback(env()->oncomplete_string(), ARRAY_SIZE(argv), argv);
324+
MakeCallback(env()->oncomplete_string(), arraysize(argv), argv);
325325
}
326326

327327
void ParseError(int status) {
@@ -994,7 +994,7 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
994994
uv_freeaddrinfo(res);
995995

996996
// Make the callback into JavaScript
997-
req_wrap->MakeCallback(env->oncomplete_string(), ARRAY_SIZE(argv), argv);
997+
req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
998998

999999
delete req_wrap;
10001000
}
@@ -1025,7 +1025,7 @@ void AfterGetNameInfo(uv_getnameinfo_t* req,
10251025
}
10261026

10271027
// Make the callback into JavaScript
1028-
req_wrap->MakeCallback(env->oncomplete_string(), ARRAY_SIZE(argv), argv);
1028+
req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
10291029

10301030
delete req_wrap;
10311031
}

src/debug-agent.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "debug-agent.h"
2323

2424
#include "node.h"
25-
#include "node_internals.h" // ARRAY_SIZE
25+
#include "node_internals.h" // arraysize
2626
#include "env.h"
2727
#include "env-inl.h"
2828
#include "v8.h"
@@ -175,9 +175,9 @@ void Agent::WorkerRun() {
175175
isolate,
176176
&child_loop_,
177177
context,
178-
ARRAY_SIZE(argv),
178+
arraysize(argv),
179179
argv,
180-
ARRAY_SIZE(argv),
180+
arraysize(argv),
181181
argv);
182182

183183
child_env_ = env;
@@ -301,7 +301,7 @@ void Agent::ChildSignalCb(uv_async_t* signal) {
301301
MakeCallback(isolate,
302302
api,
303303
"onmessage",
304-
ARRAY_SIZE(argv),
304+
arraysize(argv),
305305
argv);
306306
delete msg;
307307
}

src/fs_event_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
159159
argv[2] = OneByteString(env->isolate(), filename);
160160
}
161161

162-
wrap->MakeCallback(env->onchange_string(), ARRAY_SIZE(argv), argv);
162+
wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv);
163163
}
164164

165165

src/js_stream.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ int JSStream::DoShutdown(ShutdownWrap* req_wrap) {
7575

7676
req_wrap->Dispatched();
7777
Local<Value> res =
78-
MakeCallback(env()->onshutdown_string(), ARRAY_SIZE(argv), argv);
78+
MakeCallback(env()->onshutdown_string(), arraysize(argv), argv);
7979

8080
return res->Int32Value();
8181
}
@@ -103,7 +103,7 @@ int JSStream::DoWrite(WriteWrap* w,
103103

104104
w->Dispatched();
105105
Local<Value> res =
106-
MakeCallback(env()->onwrite_string(), ARRAY_SIZE(argv), argv);
106+
MakeCallback(env()->onwrite_string(), arraysize(argv), argv);
107107

108108
return res->Int32Value();
109109
}

src/node.cc

+13-13
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ void PromiseRejectCallback(PromiseRejectMessage message) {
11131113
Local<Value> args[] = { event, promise, value };
11141114
Local<Object> process = env->process_object();
11151115

1116-
callback->Call(process, ARRAY_SIZE(args), args);
1116+
callback->Call(process, arraysize(args), args);
11171117
}
11181118

11191119
void SetupPromises(const FunctionCallbackInfo<Value>& args) {
@@ -1196,7 +1196,7 @@ Local<Value> MakeCallback(Environment* env,
11961196
{ Undefined(env->isolate()).As<Value>(), did_throw };
11971197
TryCatch try_catch(env->isolate());
11981198
MaybeLocal<Value> ar =
1199-
post_fn->Call(env->context(), object, ARRAY_SIZE(vals), vals);
1199+
post_fn->Call(env->context(), object, arraysize(vals), vals);
12001200
if (ar.IsEmpty()) {
12011201
ClearFatalExceptionHandlers(env);
12021202
FatalException(env->isolate(), try_catch);
@@ -1651,7 +1651,7 @@ static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
16511651
if (w->persistent().IsEmpty())
16521652
continue;
16531653
argv[idx] = w->object();
1654-
if (++idx >= ARRAY_SIZE(argv)) {
1654+
if (++idx >= arraysize(argv)) {
16551655
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
16561656
idx = 0;
16571657
}
@@ -1686,7 +1686,7 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
16861686
if (owner->IsUndefined())
16871687
owner = object;
16881688
argv[idx] = owner;
1689-
if (++idx >= ARRAY_SIZE(argv)) {
1689+
if (++idx >= arraysize(argv)) {
16901690
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
16911691
idx = 0;
16921692
}
@@ -2520,12 +2520,12 @@ static void EnvGetter(Local<String> property,
25202520
WCHAR buffer[32767]; // The maximum size allowed for environment variables.
25212521
DWORD result = GetEnvironmentVariableW(reinterpret_cast<WCHAR*>(*key),
25222522
buffer,
2523-
ARRAY_SIZE(buffer));
2523+
arraysize(buffer));
25242524
// If result >= sizeof buffer the buffer was too small. That should never
25252525
// happen. If result == 0 and result != ERROR_SUCCESS the variable was not
25262526
// not found.
25272527
if ((result > 0 || GetLastError() == ERROR_SUCCESS) &&
2528-
result < ARRAY_SIZE(buffer)) {
2528+
result < arraysize(buffer)) {
25292529
const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(buffer);
25302530
Local<String> rc = String::NewFromTwoByte(isolate, two_byte_buffer);
25312531
return info.GetReturnValue().Set(rc);
@@ -2626,7 +2626,7 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
26262626
var,
26272627
String::kNormalString,
26282628
length);
2629-
if (++idx >= ARRAY_SIZE(argv)) {
2629+
if (++idx >= arraysize(argv)) {
26302630
fn->Call(ctx, envarr, idx, argv).ToLocalChecked();
26312631
idx = 0;
26322632
}
@@ -2658,7 +2658,7 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
26582658
two_byte_buffer,
26592659
String::kNormalString,
26602660
two_byte_buffer_len);
2661-
if (++idx >= ARRAY_SIZE(argv)) {
2661+
if (++idx >= arraysize(argv)) {
26622662
fn->Call(ctx, envarr, idx, argv).ToLocalChecked();
26632663
idx = 0;
26642664
}
@@ -3556,7 +3556,7 @@ static void EnableDebug(Environment* env) {
35563556
FIXED_ONE_BYTE_STRING(env->isolate(), "internalMessage"),
35573557
message
35583558
};
3559-
MakeCallback(env, env->process_object(), "emit", ARRAY_SIZE(argv), argv);
3559+
MakeCallback(env, env->process_object(), "emit", arraysize(argv), argv);
35603560

35613561
// Enabled debugger, possibly making it wait on a semaphore
35623562
env->debugger_agent()->Enable();
@@ -3677,7 +3677,7 @@ static int RegisterDebugSignalHandler() {
36773677

36783678
if (GetDebugSignalHandlerMappingName(pid,
36793679
mapping_name,
3680-
ARRAY_SIZE(mapping_name)) < 0) {
3680+
arraysize(mapping_name)) < 0) {
36813681
return -1;
36823682
}
36833683

@@ -3740,7 +3740,7 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
37403740

37413741
if (GetDebugSignalHandlerMappingName(pid,
37423742
mapping_name,
3743-
ARRAY_SIZE(mapping_name)) < 0) {
3743+
arraysize(mapping_name)) < 0) {
37443744
env->ThrowErrnoException(errno, "sprintf");
37453745
goto out;
37463746
}
@@ -4017,7 +4017,7 @@ void EmitBeforeExit(Environment* env) {
40174017
FIXED_ONE_BYTE_STRING(env->isolate(), "beforeExit"),
40184018
process_object->Get(exit_code)->ToInteger(env->isolate())
40194019
};
4020-
MakeCallback(env, process_object, "emit", ARRAY_SIZE(args), args);
4020+
MakeCallback(env, process_object, "emit", arraysize(args), args);
40214021
}
40224022

40234023

@@ -4036,7 +4036,7 @@ int EmitExit(Environment* env) {
40364036
Integer::New(env->isolate(), code)
40374037
};
40384038

4039-
MakeCallback(env, process_object, "emit", ARRAY_SIZE(args), args);
4039+
MakeCallback(env, process_object, "emit", arraysize(args), args);
40404040

40414041
// Reload exit code, it may be changed by `emit('exit')`
40424042
return process_object->Get(exitCode)->Int32Value();

src/node_contextify.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class ContextifyContext {
163163
CHECK(clone_property_method->IsFunction());
164164
}
165165
Local<Value> args[] = { global, key, sandbox_obj };
166-
clone_property_method->Call(global, ARRAY_SIZE(args), args);
166+
clone_property_method->Call(global, arraysize(args), args);
167167
}
168168
}
169169
}

src/node_counters.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void InitPerfCounters(Environment* env, Local<Object> target) {
9898
#undef NODE_PROBE
9999
};
100100

101-
for (int i = 0; i < ARRAY_SIZE(tab); i++) {
101+
for (size_t i = 0; i < arraysize(tab); i++) {
102102
Local<String> key = OneByteString(env->isolate(), tab[i].name);
103103
Local<Value> val = env->NewFunctionTemplate(tab[i].func)->GetFunction();
104104
target->Set(key, val);

src/node_crypto.cc

+11-11
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ void SecureContext::AddRootCerts(const FunctionCallbackInfo<Value>& args) {
752752
if (!root_cert_store) {
753753
root_cert_store = X509_STORE_new();
754754

755-
for (size_t i = 0; i < ARRAY_SIZE(root_certs); i++) {
755+
for (size_t i = 0; i < arraysize(root_certs); i++) {
756756
BIO* bp = NodeBIO::NewFixed(root_certs[i], strlen(root_certs[i]));
757757
if (bp == nullptr) {
758758
return;
@@ -1084,7 +1084,7 @@ int SecureContext::TicketKeyCallback(SSL* ssl,
10841084
Local<Value> ret = node::MakeCallback(env,
10851085
sc->object(),
10861086
env->ticketkeycallback_string(),
1087-
ARRAY_SIZE(argv),
1087+
arraysize(argv),
10881088
argv);
10891089
Local<Array> arr = ret.As<Array>();
10901090

@@ -1281,7 +1281,7 @@ int SSLWrap<Base>::NewSessionCallback(SSL* s, SSL_SESSION* sess) {
12811281
sess->session_id_length).ToLocalChecked();
12821282
Local<Value> argv[] = { session, buff };
12831283
w->new_session_wait_ = true;
1284-
w->MakeCallback(env->onnewsession_string(), ARRAY_SIZE(argv), argv);
1284+
w->MakeCallback(env->onnewsession_string(), arraysize(argv), argv);
12851285

12861286
return 0;
12871287
}
@@ -1315,7 +1315,7 @@ void SSLWrap<Base>::OnClientHello(void* arg,
13151315
Boolean::New(env->isolate(), hello.ocsp_request()));
13161316

13171317
Local<Value> argv[] = { hello_obj };
1318-
w->MakeCallback(env->onclienthello_string(), ARRAY_SIZE(argv), argv);
1318+
w->MakeCallback(env->onclienthello_string(), arraysize(argv), argv);
13191319
}
13201320

13211321

@@ -1390,8 +1390,8 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
13901390
int nids[] = { NID_subject_alt_name, NID_info_access };
13911391
Local<String> keys[] = { env->subjectaltname_string(),
13921392
env->infoaccess_string() };
1393-
CHECK_EQ(ARRAY_SIZE(nids), ARRAY_SIZE(keys));
1394-
for (unsigned int i = 0; i < ARRAY_SIZE(nids); i++) {
1393+
CHECK_EQ(arraysize(nids), arraysize(keys));
1394+
for (size_t i = 0; i < arraysize(nids); i++) {
13951395
int index = X509_get_ext_by_NID(cert, nids[i], -1);
13961396
if (index < 0)
13971397
continue;
@@ -2282,7 +2282,7 @@ int SSLWrap<Base>::SSLCertCallback(SSL* s, void* arg) {
22822282
info->Set(env->ocsp_request_string(), Boolean::New(env->isolate(), ocsp));
22832283

22842284
Local<Value> argv[] = { info };
2285-
w->MakeCallback(env->oncertcb_string(), ARRAY_SIZE(argv), argv);
2285+
w->MakeCallback(env->oncertcb_string(), arraysize(argv), argv);
22862286

22872287
if (!w->cert_cb_running_)
22882288
return 1;
@@ -2645,7 +2645,7 @@ inline CheckResult CheckWhitelistedServerCert(X509_STORE_CTX* ctx) {
26452645
CHECK(ret);
26462646

26472647
void* result = bsearch(hash, WhitelistedCNNICHashes,
2648-
ARRAY_SIZE(WhitelistedCNNICHashes),
2648+
arraysize(WhitelistedCNNICHashes),
26492649
CNNIC_WHITELIST_HASH_LEN, compar);
26502650
if (result == nullptr) {
26512651
sk_X509_pop_free(chain, X509_free);
@@ -4383,7 +4383,7 @@ void DiffieHellman::DiffieHellmanGroup(
43834383
bool initialized = false;
43844384

43854385
const node::Utf8Value group_name(env->isolate(), args[0]);
4386-
for (unsigned int i = 0; i < ARRAY_SIZE(modp_groups); ++i) {
4386+
for (size_t i = 0; i < arraysize(modp_groups); ++i) {
43874387
const modp_group* it = modp_groups + i;
43884388

43894389
if (strcasecmp(*group_name, it->name) != 0)
@@ -5086,7 +5086,7 @@ void EIO_PBKDF2After(uv_work_t* work_req, int status) {
50865086
Context::Scope context_scope(env->context());
50875087
Local<Value> argv[2];
50885088
EIO_PBKDF2After(req, argv);
5089-
req->MakeCallback(env->ondone_string(), ARRAY_SIZE(argv), argv);
5089+
req->MakeCallback(env->ondone_string(), arraysize(argv), argv);
50905090
delete req;
50915091
}
50925092

@@ -5327,7 +5327,7 @@ void RandomBytesAfter(uv_work_t* work_req, int status) {
53275327
Context::Scope context_scope(env->context());
53285328
Local<Value> argv[2];
53295329
RandomBytesCheck(req, argv);
5330-
req->MakeCallback(env->ondone_string(), ARRAY_SIZE(argv), argv);
5330+
req->MakeCallback(env->ondone_string(), arraysize(argv), argv);
53315331
delete req;
53325332
}
53335333

src/node_dtrace.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ void InitDTrace(Environment* env, Local<Object> target) {
257257
#undef NODE_PROBE
258258
};
259259

260-
for (unsigned int i = 0; i < ARRAY_SIZE(tab); i++) {
260+
for (size_t i = 0; i < arraysize(tab); i++) {
261261
Local<String> key = OneByteString(env->isolate(), tab[i].name);
262262
Local<Value> val = env->NewFunctionTemplate(tab[i].func)->GetFunction();
263263
target->Set(key, val);

src/node_file.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ static void After(uv_fs_t *req) {
240240
name_argv[name_idx++] =
241241
String::NewFromUtf8(env->isolate(), ent.name);
242242

243-
if (name_idx >= ARRAY_SIZE(name_argv)) {
243+
if (name_idx >= arraysize(name_argv)) {
244244
fn->Call(env->context(), names, name_idx, name_argv)
245245
.ToLocalChecked();
246246
name_idx = 0;
@@ -447,7 +447,7 @@ Local<Value> BuildStatsObject(Environment* env, const uv_stat_t* s) {
447447

448448
// Call out to JavaScript to create the stats object.
449449
Local<Value> stats =
450-
env->fs_stats_constructor_function()->NewInstance(ARRAY_SIZE(argv), argv);
450+
env->fs_stats_constructor_function()->NewInstance(arraysize(argv), argv);
451451

452452
if (stats.IsEmpty())
453453
return handle_scope.Escape(Local<Object>());
@@ -845,7 +845,7 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
845845

846846
name_v[name_idx++] = String::NewFromUtf8(env->isolate(), ent.name);
847847

848-
if (name_idx >= ARRAY_SIZE(name_v)) {
848+
if (name_idx >= arraysize(name_v)) {
849849
fn->Call(env->context(), names, name_idx, name_v)
850850
.ToLocalChecked();
851851
name_idx = 0;
@@ -962,7 +962,7 @@ static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
962962
uv_buf_t s_iovs[1024]; // use stack allocation when possible
963963
uv_buf_t* iovs;
964964

965-
if (chunkCount > ARRAY_SIZE(s_iovs))
965+
if (chunkCount > arraysize(s_iovs))
966966
iovs = new uv_buf_t[chunkCount];
967967
else
968968
iovs = s_iovs;

0 commit comments

Comments
 (0)