Skip to content

Commit

Permalink
src: replace ARRAY_SIZE with typesafe arraysize
Browse files Browse the repository at this point in the history
To prevent `ARRAY_SIZE(&arg)` (i.e., taking the array size of a pointer)
from happening again.

PR-URL: #5969
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
bnoordhuis authored and Myles Borins committed Apr 14, 2016
1 parent 60a73a2 commit 2fd8be2
Show file tree
Hide file tree
Showing 25 changed files with 80 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/async-wrap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ inline AsyncWrap::AsyncWrap(Environment* env,
v8::TryCatch try_catch(env->isolate());

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

if (ret.IsEmpty()) {
ClearFatalExceptionHandlers(env);
Expand Down
2 changes: 1 addition & 1 deletion src/async-wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
Local<Value> vals[] = { uid, did_throw };
TryCatch try_catch(env()->isolate());
MaybeLocal<Value> ar =
post_fn->Call(env()->context(), context, ARRAY_SIZE(vals), vals);
post_fn->Call(env()->context(), context, arraysize(vals), vals);
if (ar.IsEmpty()) {
ClearFatalExceptionHandlers(env());
FatalException(env()->isolate(), try_catch);
Expand Down
8 changes: 4 additions & 4 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class QueryWrap : public AsyncWrap {
Integer::New(env()->isolate(), 0),
answer
};
MakeCallback(env()->oncomplete_string(), ARRAY_SIZE(argv), argv);
MakeCallback(env()->oncomplete_string(), arraysize(argv), argv);
}

void CallOnComplete(Local<Value> answer, Local<Value> family) {
Expand All @@ -321,7 +321,7 @@ class QueryWrap : public AsyncWrap {
answer,
family
};
MakeCallback(env()->oncomplete_string(), ARRAY_SIZE(argv), argv);
MakeCallback(env()->oncomplete_string(), arraysize(argv), argv);
}

void ParseError(int status) {
Expand Down Expand Up @@ -994,7 +994,7 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
uv_freeaddrinfo(res);

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

delete req_wrap;
}
Expand Down Expand Up @@ -1025,7 +1025,7 @@ void AfterGetNameInfo(uv_getnameinfo_t* req,
}

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

delete req_wrap;
}
Expand Down
8 changes: 4 additions & 4 deletions src/debug-agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "debug-agent.h"

#include "node.h"
#include "node_internals.h" // ARRAY_SIZE
#include "node_internals.h" // arraysize
#include "env.h"
#include "env-inl.h"
#include "v8.h"
Expand Down Expand Up @@ -175,9 +175,9 @@ void Agent::WorkerRun() {
isolate,
&child_loop_,
context,
ARRAY_SIZE(argv),
arraysize(argv),
argv,
ARRAY_SIZE(argv),
arraysize(argv),
argv);

child_env_ = env;
Expand Down Expand Up @@ -301,7 +301,7 @@ void Agent::ChildSignalCb(uv_async_t* signal) {
MakeCallback(isolate,
api,
"onmessage",
ARRAY_SIZE(argv),
arraysize(argv),
argv);
delete msg;
}
Expand Down
2 changes: 1 addition & 1 deletion src/fs_event_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
argv[2] = OneByteString(env->isolate(), filename);
}

wrap->MakeCallback(env->onchange_string(), ARRAY_SIZE(argv), argv);
wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv);
}


Expand Down
4 changes: 2 additions & 2 deletions src/js_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int JSStream::DoShutdown(ShutdownWrap* req_wrap) {

req_wrap->Dispatched();
Local<Value> res =
MakeCallback(env()->onshutdown_string(), ARRAY_SIZE(argv), argv);
MakeCallback(env()->onshutdown_string(), arraysize(argv), argv);

return res->Int32Value();
}
Expand Down Expand Up @@ -103,7 +103,7 @@ int JSStream::DoWrite(WriteWrap* w,

w->Dispatched();
Local<Value> res =
MakeCallback(env()->onwrite_string(), ARRAY_SIZE(argv), argv);
MakeCallback(env()->onwrite_string(), arraysize(argv), argv);

return res->Int32Value();
}
Expand Down
26 changes: 13 additions & 13 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ void PromiseRejectCallback(PromiseRejectMessage message) {
Local<Value> args[] = { event, promise, value };
Local<Object> process = env->process_object();

callback->Call(process, ARRAY_SIZE(args), args);
callback->Call(process, arraysize(args), args);
}

void SetupPromises(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -1196,7 +1196,7 @@ Local<Value> MakeCallback(Environment* env,
{ Undefined(env->isolate()).As<Value>(), did_throw };
TryCatch try_catch(env->isolate());
MaybeLocal<Value> ar =
post_fn->Call(env->context(), object, ARRAY_SIZE(vals), vals);
post_fn->Call(env->context(), object, arraysize(vals), vals);
if (ar.IsEmpty()) {
ClearFatalExceptionHandlers(env);
FatalException(env->isolate(), try_catch);
Expand Down Expand Up @@ -1651,7 +1651,7 @@ static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
if (w->persistent().IsEmpty())
continue;
argv[idx] = w->object();
if (++idx >= ARRAY_SIZE(argv)) {
if (++idx >= arraysize(argv)) {
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
idx = 0;
}
Expand Down Expand Up @@ -1686,7 +1686,7 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
if (owner->IsUndefined())
owner = object;
argv[idx] = owner;
if (++idx >= ARRAY_SIZE(argv)) {
if (++idx >= arraysize(argv)) {
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
idx = 0;
}
Expand Down Expand Up @@ -2520,12 +2520,12 @@ static void EnvGetter(Local<String> property,
WCHAR buffer[32767]; // The maximum size allowed for environment variables.
DWORD result = GetEnvironmentVariableW(reinterpret_cast<WCHAR*>(*key),
buffer,
ARRAY_SIZE(buffer));
arraysize(buffer));
// If result >= sizeof buffer the buffer was too small. That should never
// happen. If result == 0 and result != ERROR_SUCCESS the variable was not
// not found.
if ((result > 0 || GetLastError() == ERROR_SUCCESS) &&
result < ARRAY_SIZE(buffer)) {
result < arraysize(buffer)) {
const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(buffer);
Local<String> rc = String::NewFromTwoByte(isolate, two_byte_buffer);
return info.GetReturnValue().Set(rc);
Expand Down Expand Up @@ -2626,7 +2626,7 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
var,
String::kNormalString,
length);
if (++idx >= ARRAY_SIZE(argv)) {
if (++idx >= arraysize(argv)) {
fn->Call(ctx, envarr, idx, argv).ToLocalChecked();
idx = 0;
}
Expand Down Expand Up @@ -2658,7 +2658,7 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
two_byte_buffer,
String::kNormalString,
two_byte_buffer_len);
if (++idx >= ARRAY_SIZE(argv)) {
if (++idx >= arraysize(argv)) {
fn->Call(ctx, envarr, idx, argv).ToLocalChecked();
idx = 0;
}
Expand Down Expand Up @@ -3556,7 +3556,7 @@ static void EnableDebug(Environment* env) {
FIXED_ONE_BYTE_STRING(env->isolate(), "internalMessage"),
message
};
MakeCallback(env, env->process_object(), "emit", ARRAY_SIZE(argv), argv);
MakeCallback(env, env->process_object(), "emit", arraysize(argv), argv);

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

if (GetDebugSignalHandlerMappingName(pid,
mapping_name,
ARRAY_SIZE(mapping_name)) < 0) {
arraysize(mapping_name)) < 0) {
return -1;
}

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

if (GetDebugSignalHandlerMappingName(pid,
mapping_name,
ARRAY_SIZE(mapping_name)) < 0) {
arraysize(mapping_name)) < 0) {
env->ThrowErrnoException(errno, "sprintf");
goto out;
}
Expand Down Expand Up @@ -4017,7 +4017,7 @@ void EmitBeforeExit(Environment* env) {
FIXED_ONE_BYTE_STRING(env->isolate(), "beforeExit"),
process_object->Get(exit_code)->ToInteger(env->isolate())
};
MakeCallback(env, process_object, "emit", ARRAY_SIZE(args), args);
MakeCallback(env, process_object, "emit", arraysize(args), args);
}


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

MakeCallback(env, process_object, "emit", ARRAY_SIZE(args), args);
MakeCallback(env, process_object, "emit", arraysize(args), args);

// Reload exit code, it may be changed by `emit('exit')`
return process_object->Get(exitCode)->Int32Value();
Expand Down
2 changes: 1 addition & 1 deletion src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class ContextifyContext {
CHECK(clone_property_method->IsFunction());
}
Local<Value> args[] = { global, key, sandbox_obj };
clone_property_method->Call(global, ARRAY_SIZE(args), args);
clone_property_method->Call(global, arraysize(args), args);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/node_counters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void InitPerfCounters(Environment* env, Local<Object> target) {
#undef NODE_PROBE
};

for (int i = 0; i < ARRAY_SIZE(tab); i++) {
for (size_t i = 0; i < arraysize(tab); i++) {
Local<String> key = OneByteString(env->isolate(), tab[i].name);
Local<Value> val = env->NewFunctionTemplate(tab[i].func)->GetFunction();
target->Set(key, val);
Expand Down
22 changes: 11 additions & 11 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ void SecureContext::AddRootCerts(const FunctionCallbackInfo<Value>& args) {
if (!root_cert_store) {
root_cert_store = X509_STORE_new();

for (size_t i = 0; i < ARRAY_SIZE(root_certs); i++) {
for (size_t i = 0; i < arraysize(root_certs); i++) {
BIO* bp = NodeBIO::NewFixed(root_certs[i], strlen(root_certs[i]));
if (bp == nullptr) {
return;
Expand Down Expand Up @@ -1084,7 +1084,7 @@ int SecureContext::TicketKeyCallback(SSL* ssl,
Local<Value> ret = node::MakeCallback(env,
sc->object(),
env->ticketkeycallback_string(),
ARRAY_SIZE(argv),
arraysize(argv),
argv);
Local<Array> arr = ret.As<Array>();

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

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

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


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

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

if (!w->cert_cb_running_)
return 1;
Expand Down Expand Up @@ -2645,7 +2645,7 @@ inline CheckResult CheckWhitelistedServerCert(X509_STORE_CTX* ctx) {
CHECK(ret);

void* result = bsearch(hash, WhitelistedCNNICHashes,
ARRAY_SIZE(WhitelistedCNNICHashes),
arraysize(WhitelistedCNNICHashes),
CNNIC_WHITELIST_HASH_LEN, compar);
if (result == nullptr) {
sk_X509_pop_free(chain, X509_free);
Expand Down Expand Up @@ -4383,7 +4383,7 @@ void DiffieHellman::DiffieHellmanGroup(
bool initialized = false;

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

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

Expand Down Expand Up @@ -5327,7 +5327,7 @@ void RandomBytesAfter(uv_work_t* work_req, int status) {
Context::Scope context_scope(env->context());
Local<Value> argv[2];
RandomBytesCheck(req, argv);
req->MakeCallback(env->ondone_string(), ARRAY_SIZE(argv), argv);
req->MakeCallback(env->ondone_string(), arraysize(argv), argv);
delete req;
}

Expand Down
2 changes: 1 addition & 1 deletion src/node_dtrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void InitDTrace(Environment* env, Local<Object> target) {
#undef NODE_PROBE
};

for (unsigned int i = 0; i < ARRAY_SIZE(tab); i++) {
for (size_t i = 0; i < arraysize(tab); i++) {
Local<String> key = OneByteString(env->isolate(), tab[i].name);
Local<Value> val = env->NewFunctionTemplate(tab[i].func)->GetFunction();
target->Set(key, val);
Expand Down
8 changes: 4 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ static void After(uv_fs_t *req) {
name_argv[name_idx++] =
String::NewFromUtf8(env->isolate(), ent.name);

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

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

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

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

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

if (chunkCount > ARRAY_SIZE(s_iovs))
if (chunkCount > arraysize(s_iovs))
iovs = new uv_buf_t[chunkCount];
else
iovs = s_iovs;
Expand Down
Loading

0 comments on commit 2fd8be2

Please sign in to comment.