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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
bnoordhuis authored and Myles Borins committed Jun 24, 2016
1 parent ab50e82 commit af41a63
Show file tree
Hide file tree
Showing 24 changed files with 70 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/async-wrap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ inline AsyncWrap::AsyncWrap(Environment* env,
argv[2] = parent->object();

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())
FatalError("node::AsyncWrap::AsyncWrap", "init hook threw");
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
16 changes: 8 additions & 8 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,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 @@ -2453,12 +2453,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 @@ -3457,7 +3457,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 @@ -3578,7 +3578,7 @@ static int RegisterDebugSignalHandler() {

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

Expand Down Expand Up @@ -3641,7 +3641,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 @@ -3918,7 +3918,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 @@ -3937,7 +3937,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 @@ -161,7 +161,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 @@ -760,7 +760,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 @@ -1092,7 +1092,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 @@ -1284,7 +1284,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 @@ -1318,7 +1318,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 @@ -1393,8 +1393,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 @@ -2128,7 +2128,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 @@ -2491,7 +2491,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 @@ -4229,7 +4229,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 @@ -4883,7 +4883,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 @@ -5124,7 +5124,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
4 changes: 2 additions & 2 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,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 @@ -932,7 +932,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
14 changes: 7 additions & 7 deletions src/node_http_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class Parser : public BaseObject {
if (num_fields_ == num_values_) {
// start of new field name
num_fields_++;
if (num_fields_ == ARRAY_SIZE(fields_)) {
if (num_fields_ == static_cast<int>(arraysize(fields_))) {
// ran out of space - flush to javascript land
Flush();
num_fields_ = 1;
Expand All @@ -198,7 +198,7 @@ class Parser : public BaseObject {
fields_[num_fields_ - 1].Reset();
}

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

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

CHECK_LT(num_values_, static_cast<int>(ARRAY_SIZE(values_)));
CHECK_LT(num_values_, static_cast<int>(arraysize(values_)));
CHECK_EQ(num_values_, num_fields_);

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

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

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

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

if (head_response.IsEmpty()) {
got_exception_ = true;
Expand Down Expand Up @@ -322,7 +322,7 @@ class Parser : public BaseObject {
Integer::NewFromUnsigned(env()->isolate(), length)
};

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

if (r.IsEmpty()) {
got_exception_ = true;
Expand Down Expand Up @@ -659,7 +659,7 @@ class Parser : public BaseObject {
url_.ToString(env())
};

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

if (r.IsEmpty())
got_exception_ = true;
Expand Down
7 changes: 5 additions & 2 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ inline static int snprintf(char *buffer, size_t n, const char *format, ...) {
#endif
#endif

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

#ifndef ROUND_UP
Expand Down
2 changes: 1 addition & 1 deletion src/node_lttng.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ void InitLTTNG(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
Loading

0 comments on commit af41a63

Please sign in to comment.