Skip to content

Commit af41a63

Browse files
bnoordhuisMyles Borins
authored and
Myles Borins
committedJun 24, 2016
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 ab50e82 commit af41a63

24 files changed

+70
-67
lines changed
 

‎src/async-wrap-inl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ inline AsyncWrap::AsyncWrap(Environment* env,
4949
argv[2] = parent->object();
5050

5151
v8::MaybeLocal<v8::Value> ret =
52-
init_fn->Call(env->context(), object, ARRAY_SIZE(argv), argv);
52+
init_fn->Call(env->context(), object, arraysize(argv), argv);
5353

5454
if (ret.IsEmpty())
5555
FatalError("node::AsyncWrap::AsyncWrap", "init hook threw");

‎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

+8-8
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ void PromiseRejectCallback(PromiseRejectMessage message) {
10971097
Local<Value> args[] = { event, promise, value };
10981098
Local<Object> process = env->process_object();
10991099

1100-
callback->Call(process, ARRAY_SIZE(args), args);
1100+
callback->Call(process, arraysize(args), args);
11011101
}
11021102

11031103
void SetupPromises(const FunctionCallbackInfo<Value>& args) {
@@ -2453,12 +2453,12 @@ static void EnvGetter(Local<String> property,
24532453
WCHAR buffer[32767]; // The maximum size allowed for environment variables.
24542454
DWORD result = GetEnvironmentVariableW(reinterpret_cast<WCHAR*>(*key),
24552455
buffer,
2456-
ARRAY_SIZE(buffer));
2456+
arraysize(buffer));
24572457
// If result >= sizeof buffer the buffer was too small. That should never
24582458
// happen. If result == 0 and result != ERROR_SUCCESS the variable was not
24592459
// not found.
24602460
if ((result > 0 || GetLastError() == ERROR_SUCCESS) &&
2461-
result < ARRAY_SIZE(buffer)) {
2461+
result < arraysize(buffer)) {
24622462
const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(buffer);
24632463
Local<String> rc = String::NewFromTwoByte(isolate, two_byte_buffer);
24642464
return info.GetReturnValue().Set(rc);
@@ -3457,7 +3457,7 @@ static void EnableDebug(Environment* env) {
34573457
FIXED_ONE_BYTE_STRING(env->isolate(), "internalMessage"),
34583458
message
34593459
};
3460-
MakeCallback(env, env->process_object(), "emit", ARRAY_SIZE(argv), argv);
3460+
MakeCallback(env, env->process_object(), "emit", arraysize(argv), argv);
34613461

34623462
// Enabled debugger, possibly making it wait on a semaphore
34633463
env->debugger_agent()->Enable();
@@ -3578,7 +3578,7 @@ static int RegisterDebugSignalHandler() {
35783578

35793579
if (GetDebugSignalHandlerMappingName(pid,
35803580
mapping_name,
3581-
ARRAY_SIZE(mapping_name)) < 0) {
3581+
arraysize(mapping_name)) < 0) {
35823582
return -1;
35833583
}
35843584

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

36423642
if (GetDebugSignalHandlerMappingName(pid,
36433643
mapping_name,
3644-
ARRAY_SIZE(mapping_name)) < 0) {
3644+
arraysize(mapping_name)) < 0) {
36453645
env->ThrowErrnoException(errno, "sprintf");
36463646
goto out;
36473647
}
@@ -3918,7 +3918,7 @@ void EmitBeforeExit(Environment* env) {
39183918
FIXED_ONE_BYTE_STRING(env->isolate(), "beforeExit"),
39193919
process_object->Get(exit_code)->ToInteger(env->isolate())
39203920
};
3921-
MakeCallback(env, process_object, "emit", ARRAY_SIZE(args), args);
3921+
MakeCallback(env, process_object, "emit", arraysize(args), args);
39223922
}
39233923

39243924

@@ -3937,7 +3937,7 @@ int EmitExit(Environment* env) {
39373937
Integer::New(env->isolate(), code)
39383938
};
39393939

3940-
MakeCallback(env, process_object, "emit", ARRAY_SIZE(args), args);
3940+
MakeCallback(env, process_object, "emit", arraysize(args), args);
39413941

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

‎src/node_contextify.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class ContextifyContext {
161161
CHECK(clone_property_method->IsFunction());
162162
}
163163
Local<Value> args[] = { global, key, sandbox_obj };
164-
clone_property_method->Call(global, ARRAY_SIZE(args), args);
164+
clone_property_method->Call(global, arraysize(args), args);
165165
}
166166
}
167167
}

‎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
@@ -760,7 +760,7 @@ void SecureContext::AddRootCerts(const FunctionCallbackInfo<Value>& args) {
760760
if (!root_cert_store) {
761761
root_cert_store = X509_STORE_new();
762762

763-
for (size_t i = 0; i < ARRAY_SIZE(root_certs); i++) {
763+
for (size_t i = 0; i < arraysize(root_certs); i++) {
764764
BIO* bp = NodeBIO::NewFixed(root_certs[i], strlen(root_certs[i]));
765765
if (bp == nullptr) {
766766
return;
@@ -1092,7 +1092,7 @@ int SecureContext::TicketKeyCallback(SSL* ssl,
10921092
Local<Value> ret = node::MakeCallback(env,
10931093
sc->object(),
10941094
env->ticketkeycallback_string(),
1095-
ARRAY_SIZE(argv),
1095+
arraysize(argv),
10961096
argv);
10971097
Local<Array> arr = ret.As<Array>();
10981098

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

12891289
return 0;
12901290
}
@@ -1318,7 +1318,7 @@ void SSLWrap<Base>::OnClientHello(void* arg,
13181318
Boolean::New(env->isolate(), hello.ocsp_request()));
13191319

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

13241324

@@ -1393,8 +1393,8 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
13931393
int nids[] = { NID_subject_alt_name, NID_info_access };
13941394
Local<String> keys[] = { env->subjectaltname_string(),
13951395
env->infoaccess_string() };
1396-
CHECK_EQ(ARRAY_SIZE(nids), ARRAY_SIZE(keys));
1397-
for (unsigned int i = 0; i < ARRAY_SIZE(nids); i++) {
1396+
CHECK_EQ(arraysize(nids), arraysize(keys));
1397+
for (size_t i = 0; i < arraysize(nids); i++) {
13981398
int index = X509_get_ext_by_NID(cert, nids[i], -1);
13991399
if (index < 0)
14001400
continue;
@@ -2128,7 +2128,7 @@ int SSLWrap<Base>::SSLCertCallback(SSL* s, void* arg) {
21282128
info->Set(env->ocsp_request_string(), Boolean::New(env->isolate(), ocsp));
21292129

21302130
Local<Value> argv[] = { info };
2131-
w->MakeCallback(env->oncertcb_string(), ARRAY_SIZE(argv), argv);
2131+
w->MakeCallback(env->oncertcb_string(), arraysize(argv), argv);
21322132

21332133
if (!w->cert_cb_running_)
21342134
return 1;
@@ -2491,7 +2491,7 @@ inline CheckResult CheckWhitelistedServerCert(X509_STORE_CTX* ctx) {
24912491
CHECK(ret);
24922492

24932493
void* result = bsearch(hash, WhitelistedCNNICHashes,
2494-
ARRAY_SIZE(WhitelistedCNNICHashes),
2494+
arraysize(WhitelistedCNNICHashes),
24952495
CNNIC_WHITELIST_HASH_LEN, compar);
24962496
if (result == nullptr) {
24972497
sk_X509_pop_free(chain, X509_free);
@@ -4229,7 +4229,7 @@ void DiffieHellman::DiffieHellmanGroup(
42294229
bool initialized = false;
42304230

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

42354235
if (strcasecmp(*group_name, it->name) != 0)
@@ -4883,7 +4883,7 @@ void EIO_PBKDF2After(uv_work_t* work_req, int status) {
48834883
Context::Scope context_scope(env->context());
48844884
Local<Value> argv[2];
48854885
EIO_PBKDF2After(req, argv);
4886-
req->MakeCallback(env->ondone_string(), ARRAY_SIZE(argv), argv);
4886+
req->MakeCallback(env->ondone_string(), arraysize(argv), argv);
48874887
delete req;
48884888
}
48894889

@@ -5124,7 +5124,7 @@ void RandomBytesAfter(uv_work_t* work_req, int status) {
51245124
Context::Scope context_scope(env->context());
51255125
Local<Value> argv[2];
51265126
RandomBytesCheck(req, argv);
5127-
req->MakeCallback(env->ondone_string(), ARRAY_SIZE(argv), argv);
5127+
req->MakeCallback(env->ondone_string(), arraysize(argv), argv);
51285128
delete req;
51295129
}
51305130

‎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

+2-2
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ Local<Value> BuildStatsObject(Environment* env, const uv_stat_t* s) {
429429

430430
// Call out to JavaScript to create the stats object.
431431
Local<Value> stats =
432-
env->fs_stats_constructor_function()->NewInstance(ARRAY_SIZE(argv), argv);
432+
env->fs_stats_constructor_function()->NewInstance(arraysize(argv), argv);
433433

434434
if (stats.IsEmpty())
435435
return handle_scope.Escape(Local<Object>());
@@ -932,7 +932,7 @@ static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
932932
uv_buf_t s_iovs[1024]; // use stack allocation when possible
933933
uv_buf_t* iovs;
934934

935-
if (chunkCount > ARRAY_SIZE(s_iovs))
935+
if (chunkCount > arraysize(s_iovs))
936936
iovs = new uv_buf_t[chunkCount];
937937
else
938938
iovs = s_iovs;

‎src/node_http_parser.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class Parser : public BaseObject {
189189
if (num_fields_ == num_values_) {
190190
// start of new field name
191191
num_fields_++;
192-
if (num_fields_ == ARRAY_SIZE(fields_)) {
192+
if (num_fields_ == static_cast<int>(arraysize(fields_))) {
193193
// ran out of space - flush to javascript land
194194
Flush();
195195
num_fields_ = 1;
@@ -198,7 +198,7 @@ class Parser : public BaseObject {
198198
fields_[num_fields_ - 1].Reset();
199199
}
200200

201-
CHECK_LT(num_fields_, static_cast<int>(ARRAY_SIZE(fields_)));
201+
CHECK_LT(num_fields_, static_cast<int>(arraysize(fields_)));
202202
CHECK_EQ(num_fields_, num_values_ + 1);
203203

204204
fields_[num_fields_ - 1].Update(at, length);
@@ -214,7 +214,7 @@ class Parser : public BaseObject {
214214
values_[num_values_ - 1].Reset();
215215
}
216216

217-
CHECK_LT(num_values_, static_cast<int>(ARRAY_SIZE(values_)));
217+
CHECK_LT(num_values_, static_cast<int>(arraysize(values_)));
218218
CHECK_EQ(num_values_, num_fields_);
219219

220220
values_[num_values_ - 1].Update(at, length);
@@ -248,7 +248,7 @@ class Parser : public BaseObject {
248248
return 0;
249249

250250
Local<Value> undefined = Undefined(env()->isolate());
251-
for (size_t i = 0; i < ARRAY_SIZE(argv); i++)
251+
for (size_t i = 0; i < arraysize(argv); i++)
252252
argv[i] = undefined;
253253

254254
if (have_flushed_) {
@@ -287,7 +287,7 @@ class Parser : public BaseObject {
287287
argv[A_UPGRADE] = Boolean::New(env()->isolate(), parser_.upgrade);
288288

289289
Local<Value> head_response =
290-
cb.As<Function>()->Call(obj, ARRAY_SIZE(argv), argv);
290+
cb.As<Function>()->Call(obj, arraysize(argv), argv);
291291

292292
if (head_response.IsEmpty()) {
293293
got_exception_ = true;
@@ -322,7 +322,7 @@ class Parser : public BaseObject {
322322
Integer::NewFromUnsigned(env()->isolate(), length)
323323
};
324324

325-
Local<Value> r = cb.As<Function>()->Call(obj, ARRAY_SIZE(argv), argv);
325+
Local<Value> r = cb.As<Function>()->Call(obj, arraysize(argv), argv);
326326

327327
if (r.IsEmpty()) {
328328
got_exception_ = true;
@@ -659,7 +659,7 @@ class Parser : public BaseObject {
659659
url_.ToString(env())
660660
};
661661

662-
Local<Value> r = cb.As<Function>()->Call(obj, ARRAY_SIZE(argv), argv);
662+
Local<Value> r = cb.As<Function>()->Call(obj, arraysize(argv), argv);
663663

664664
if (r.IsEmpty())
665665
got_exception_ = true;

‎src/node_internals.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,11 @@ inline static int snprintf(char *buffer, size_t n, const char *format, ...) {
109109
#endif
110110
#endif
111111

112-
#ifndef ARRAY_SIZE
113-
# define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
112+
#if defined(_MSC_VER) && _MSC_VER < 1900
113+
#define arraysize(a) (sizeof(a) / sizeof(*a)) // Workaround for VS 2013.
114+
#else
115+
template <typename T, size_t N>
116+
constexpr size_t arraysize(const T(&)[N]) { return N; }
114117
#endif
115118

116119
#ifndef ROUND_UP

‎src/node_lttng.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void InitLTTNG(Environment* env, Local<Object> target) {
250250
#undef NODE_PROBE
251251
};
252252

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

0 commit comments

Comments
 (0)
Please sign in to comment.