From 00c876dec54f9c515aeed78c75ce61a4e0206d95 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 31 Mar 2016 12:39:37 +0200 Subject: [PATCH 1/3] src: fix ARRAY_SIZE() logic error Bug introduced in commit 21d66d62 ("lib: remove bootstrap global context indirection"). PR-URL: https://github.com/nodejs/node/pull/5969 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- src/node.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node.cc b/src/node.cc index 1cf8e354614307..b6af9e53b67267 100644 --- a/src/node.cc +++ b/src/node.cc @@ -3327,7 +3327,7 @@ void LoadEnvironment(Environment* env) { global->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "global"), global); Local arg = env->process_object(); - f->Call(Null(env->isolate()), ARRAY_SIZE(&arg), &arg); + f->Call(Null(env->isolate()), 1, &arg); } static void PrintHelp(); From a7581d0859ce5139b2d7795c5ac1f8df0e85815c Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 31 Mar 2016 12:47:06 +0200 Subject: [PATCH 2/3] 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: https://github.com/nodejs/node/pull/5969 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- src/async-wrap-inl.h | 2 +- src/async-wrap.cc | 2 +- src/cares_wrap.cc | 8 ++++---- src/debug-agent.cc | 8 ++++---- src/fs_event_wrap.cc | 2 +- src/js_stream.cc | 4 ++-- src/node.cc | 26 +++++++++++++------------- src/node_contextify.cc | 2 +- src/node_counters.cc | 2 +- src/node_crypto.cc | 22 +++++++++++----------- src/node_dtrace.cc | 2 +- src/node_file.cc | 8 ++++---- src/node_http_parser.cc | 16 ++++++++-------- src/node_internals.h | 7 +++++-- src/node_lttng.cc | 2 +- src/node_stat_watcher.cc | 2 +- src/node_win32_etw_provider-inl.h | 2 +- src/node_win32_etw_provider.cc | 2 +- src/node_zlib.cc | 4 ++-- src/pipe_wrap.cc | 6 +++--- src/process_wrap.cc | 2 +- src/stream_base.cc | 10 +++++----- src/tcp_wrap.cc | 4 ++-- src/tls_wrap.cc | 6 +++--- src/udp_wrap.cc | 6 +++--- 25 files changed, 80 insertions(+), 77 deletions(-) diff --git a/src/async-wrap-inl.h b/src/async-wrap-inl.h index e61e2ad4bfbf63..cf7024e7e31461 100644 --- a/src/async-wrap-inl.h +++ b/src/async-wrap-inl.h @@ -54,7 +54,7 @@ inline AsyncWrap::AsyncWrap(Environment* env, v8::TryCatch try_catch(env->isolate()); v8::MaybeLocal 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); diff --git a/src/async-wrap.cc b/src/async-wrap.cc index 05ee7fa02ad035..8129500a922d97 100644 --- a/src/async-wrap.cc +++ b/src/async-wrap.cc @@ -242,7 +242,7 @@ Local AsyncWrap::MakeCallback(const Local cb, Local vals[] = { uid, did_throw }; TryCatch try_catch(env()->isolate()); MaybeLocal 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); diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index a23b839fbb7575..6bd73ae3baf374 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -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 answer, Local family) { @@ -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) { @@ -1037,7 +1037,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; } @@ -1068,7 +1068,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; } diff --git a/src/debug-agent.cc b/src/debug-agent.cc index 2e51c89c987075..e4e0ea061bd30f 100644 --- a/src/debug-agent.cc +++ b/src/debug-agent.cc @@ -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" @@ -176,9 +176,9 @@ void Agent::WorkerRun() { isolate, &child_loop_, context, - ARRAY_SIZE(argv), + arraysize(argv), argv, - ARRAY_SIZE(argv), + arraysize(argv), argv); child_env_ = env; @@ -303,7 +303,7 @@ void Agent::ChildSignalCb(uv_async_t* signal) { MakeCallback(isolate, api, "onmessage", - ARRAY_SIZE(argv), + arraysize(argv), argv); delete msg; } diff --git a/src/fs_event_wrap.cc b/src/fs_event_wrap.cc index 58f2716a6c0be8..48b6f4eca83c4b 100644 --- a/src/fs_event_wrap.cc +++ b/src/fs_event_wrap.cc @@ -176,7 +176,7 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename, } } - wrap->MakeCallback(env->onchange_string(), ARRAY_SIZE(argv), argv); + wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv); } diff --git a/src/js_stream.cc b/src/js_stream.cc index 25938f111ba6ac..e81709a8056965 100644 --- a/src/js_stream.cc +++ b/src/js_stream.cc @@ -75,7 +75,7 @@ int JSStream::DoShutdown(ShutdownWrap* req_wrap) { req_wrap->Dispatched(); Local res = - MakeCallback(env()->onshutdown_string(), ARRAY_SIZE(argv), argv); + MakeCallback(env()->onshutdown_string(), arraysize(argv), argv); return res->Int32Value(); } @@ -103,7 +103,7 @@ int JSStream::DoWrite(WriteWrap* w, w->Dispatched(); Local res = - MakeCallback(env()->onwrite_string(), ARRAY_SIZE(argv), argv); + MakeCallback(env()->onwrite_string(), arraysize(argv), argv); return res->Int32Value(); } diff --git a/src/node.cc b/src/node.cc index b6af9e53b67267..53d390a467983a 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1129,7 +1129,7 @@ void PromiseRejectCallback(PromiseRejectMessage message) { Local args[] = { event, promise, value }; Local process = env->process_object(); - callback->Call(process, ARRAY_SIZE(args), args); + callback->Call(process, arraysize(args), args); } void SetupPromises(const FunctionCallbackInfo& args) { @@ -1213,7 +1213,7 @@ Local MakeCallback(Environment* env, { Undefined(env->isolate()).As(), did_throw }; TryCatch try_catch(env->isolate()); MaybeLocal 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); @@ -1691,7 +1691,7 @@ static void GetActiveRequests(const FunctionCallbackInfo& 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; } @@ -1726,7 +1726,7 @@ void GetActiveHandles(const FunctionCallbackInfo& 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; } @@ -2564,12 +2564,12 @@ static void EnvGetter(Local property, WCHAR buffer[32767]; // The maximum size allowed for environment variables. DWORD result = GetEnvironmentVariableW(reinterpret_cast(*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(buffer); Local rc = String::NewFromTwoByte(isolate, two_byte_buffer); return info.GetReturnValue().Set(rc); @@ -2670,7 +2670,7 @@ static void EnvEnumerator(const PropertyCallbackInfo& info) { var, String::kNormalString, length); - if (++idx >= ARRAY_SIZE(argv)) { + if (++idx >= arraysize(argv)) { fn->Call(ctx, envarr, idx, argv).ToLocalChecked(); idx = 0; } @@ -2702,7 +2702,7 @@ static void EnvEnumerator(const PropertyCallbackInfo& 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; } @@ -3641,7 +3641,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(); @@ -3762,7 +3762,7 @@ static int RegisterDebugSignalHandler() { if (GetDebugSignalHandlerMappingName(pid, mapping_name, - ARRAY_SIZE(mapping_name)) < 0) { + arraysize(mapping_name)) < 0) { return -1; } @@ -3825,7 +3825,7 @@ static void DebugProcess(const FunctionCallbackInfo& args) { if (GetDebugSignalHandlerMappingName(pid, mapping_name, - ARRAY_SIZE(mapping_name)) < 0) { + arraysize(mapping_name)) < 0) { env->ThrowErrnoException(errno, "sprintf"); goto out; } @@ -4102,7 +4102,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); } @@ -4121,7 +4121,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(); diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 48709947893621..6586f79bfedbe1 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -162,7 +162,7 @@ class ContextifyContext { CHECK(clone_property_method->IsFunction()); } Local args[] = { global, key, sandbox_obj }; - clone_property_method->Call(global, ARRAY_SIZE(args), args); + clone_property_method->Call(global, arraysize(args), args); } } } diff --git a/src/node_counters.cc b/src/node_counters.cc index c8d2959091d2d9..092d990d16b3e5 100644 --- a/src/node_counters.cc +++ b/src/node_counters.cc @@ -96,7 +96,7 @@ void InitPerfCounters(Environment* env, Local target) { #undef NODE_PROBE }; - for (int i = 0; i < ARRAY_SIZE(tab); i++) { + for (size_t i = 0; i < arraysize(tab); i++) { Local key = OneByteString(env->isolate(), tab[i].name); Local val = env->NewFunctionTemplate(tab[i].func)->GetFunction(); target->Set(key, val); diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 3dd3b4424cc8eb..2b0f6e6fbb2fa1 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -764,7 +764,7 @@ void SecureContext::AddRootCerts(const FunctionCallbackInfo& 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; @@ -1110,7 +1110,7 @@ int SecureContext::TicketKeyCallback(SSL* ssl, Local ret = node::MakeCallback(env, sc->object(), env->ticketkeycallback_string(), - ARRAY_SIZE(argv), + arraysize(argv), argv); Local arr = ret.As(); @@ -1307,7 +1307,7 @@ int SSLWrap::NewSessionCallback(SSL* s, SSL_SESSION* sess) { sess->session_id_length).ToLocalChecked(); Local 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; } @@ -1341,7 +1341,7 @@ void SSLWrap::OnClientHello(void* arg, Boolean::New(env->isolate(), hello.ocsp_request())); Local argv[] = { hello_obj }; - w->MakeCallback(env->onclienthello_string(), ARRAY_SIZE(argv), argv); + w->MakeCallback(env->onclienthello_string(), arraysize(argv), argv); } @@ -1416,8 +1416,8 @@ static Local X509ToObject(Environment* env, X509* cert) { int nids[] = { NID_subject_alt_name, NID_info_access }; Local 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; @@ -2326,7 +2326,7 @@ int SSLWrap::SSLCertCallback(SSL* s, void* arg) { info->Set(env->ocsp_request_string(), Boolean::New(env->isolate(), ocsp)); Local 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; @@ -2689,7 +2689,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); @@ -4438,7 +4438,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) @@ -5141,7 +5141,7 @@ void EIO_PBKDF2After(uv_work_t* work_req, int status) { Context::Scope context_scope(env->context()); Local 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; } @@ -5382,7 +5382,7 @@ void RandomBytesAfter(uv_work_t* work_req, int status) { Context::Scope context_scope(env->context()); Local argv[2]; RandomBytesCheck(req, argv); - req->MakeCallback(env->ondone_string(), ARRAY_SIZE(argv), argv); + req->MakeCallback(env->ondone_string(), arraysize(argv), argv); delete req; } diff --git a/src/node_dtrace.cc b/src/node_dtrace.cc index 2572bbee585b80..378d8f6b7db2e4 100644 --- a/src/node_dtrace.cc +++ b/src/node_dtrace.cc @@ -255,7 +255,7 @@ void InitDTrace(Environment* env, Local target) { #undef NODE_PROBE }; - for (unsigned int i = 0; i < ARRAY_SIZE(tab); i++) { + for (size_t i = 0; i < arraysize(tab); i++) { Local key = OneByteString(env->isolate(), tab[i].name); Local val = env->NewFunctionTemplate(tab[i].func)->GetFunction(); target->Set(key, val); diff --git a/src/node_file.cc b/src/node_file.cc index a669c0855fd3a5..e63dd3fd37d978 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -282,7 +282,7 @@ static void After(uv_fs_t *req) { } name_argv[name_idx++] = filename; - 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; @@ -491,7 +491,7 @@ Local BuildStatsObject(Environment* env, const uv_stat_t* s) { Local stats = env->fs_stats_constructor_function()->NewInstance( env->context(), - ARRAY_SIZE(argv), + arraysize(argv), argv).FromMaybe(Local()); if (stats.IsEmpty()) @@ -913,7 +913,7 @@ static void ReadDir(const FunctionCallbackInfo& args) { name_v[name_idx++] = filename; - 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; @@ -1030,7 +1030,7 @@ static void WriteBuffers(const FunctionCallbackInfo& 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; diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index 12ae9e1443f534..a9081dc674808f 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -193,7 +193,7 @@ class Parser : public AsyncWrap { if (num_fields_ == num_values_) { // start of new field name num_fields_++; - if (num_fields_ == ARRAY_SIZE(fields_)) { + if (num_fields_ == static_cast(arraysize(fields_))) { // ran out of space - flush to javascript land Flush(); num_fields_ = 1; @@ -202,7 +202,7 @@ class Parser : public AsyncWrap { fields_[num_fields_ - 1].Reset(); } - CHECK_LT(num_fields_, static_cast(ARRAY_SIZE(fields_))); + CHECK_LT(num_fields_, static_cast(arraysize(fields_))); CHECK_EQ(num_fields_, num_values_ + 1); fields_[num_fields_ - 1].Update(at, length); @@ -218,7 +218,7 @@ class Parser : public AsyncWrap { values_[num_values_ - 1].Reset(); } - CHECK_LT(num_values_, static_cast(ARRAY_SIZE(values_))); + CHECK_LT(num_values_, static_cast(arraysize(values_))); CHECK_EQ(num_values_, num_fields_); values_[num_values_ - 1].Update(at, length); @@ -252,7 +252,7 @@ class Parser : public AsyncWrap { return 0; Local 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_) { @@ -293,7 +293,7 @@ class Parser : public AsyncWrap { Environment::AsyncCallbackScope callback_scope(env()); Local head_response = - MakeCallback(cb.As(), ARRAY_SIZE(argv), argv); + MakeCallback(cb.As(), arraysize(argv), argv); if (head_response.IsEmpty()) { got_exception_ = true; @@ -328,7 +328,7 @@ class Parser : public AsyncWrap { Integer::NewFromUnsigned(env()->isolate(), length) }; - Local r = MakeCallback(cb.As(), ARRAY_SIZE(argv), argv); + Local r = MakeCallback(cb.As(), arraysize(argv), argv); if (r.IsEmpty()) { got_exception_ = true; @@ -646,7 +646,7 @@ class Parser : public AsyncWrap { do { size_t j = 0; - while (i < num_values_ && j < ARRAY_SIZE(argv) / 2) { + while (i < num_values_ && j < arraysize(argv) / 2) { argv[j * 2] = fields_[i].ToString(env()); argv[j * 2 + 1] = values_[i].ToString(env()); i++; @@ -676,7 +676,7 @@ class Parser : public AsyncWrap { url_.ToString(env()) }; - Local r = MakeCallback(cb.As(), ARRAY_SIZE(argv), argv); + Local r = MakeCallback(cb.As(), arraysize(argv), argv); if (r.IsEmpty()) got_exception_ = true; diff --git a/src/node_internals.h b/src/node_internals.h index b62c5ff8d50198..1ad9305261775b 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -107,8 +107,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 +constexpr size_t arraysize(const T(&)[N]) { return N; } #endif #ifndef ROUND_UP diff --git a/src/node_lttng.cc b/src/node_lttng.cc index b70f6ca80983e8..490beb6e40007d 100644 --- a/src/node_lttng.cc +++ b/src/node_lttng.cc @@ -248,7 +248,7 @@ void InitLTTNG(Environment* env, Local target) { #undef NODE_PROBE }; - for (unsigned int i = 0; i < ARRAY_SIZE(tab); i++) { + for (size_t i = 0; i < arraysize(tab); i++) { Local key = OneByteString(env->isolate(), tab[i].name); Local val = env->NewFunctionTemplate(tab[i].func)->GetFunction(); target->Set(key, val); diff --git a/src/node_stat_watcher.cc b/src/node_stat_watcher.cc index bd1cb73248e99c..4fa01794f6c6da 100644 --- a/src/node_stat_watcher.cc +++ b/src/node_stat_watcher.cc @@ -70,7 +70,7 @@ void StatWatcher::Callback(uv_fs_poll_t* handle, BuildStatsObject(env, prev), Integer::New(env->isolate(), status) }; - wrap->MakeCallback(env->onchange_string(), ARRAY_SIZE(argv), argv); + wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv); } diff --git a/src/node_win32_etw_provider-inl.h b/src/node_win32_etw_provider-inl.h index 3fef20cc1488c1..04cd31cee3c2f6 100644 --- a/src/node_win32_etw_provider-inl.h +++ b/src/node_win32_etw_provider-inl.h @@ -202,7 +202,7 @@ void NODE_V8SYMBOL_RESET() { #define SETSYMBUF(s) \ wcscpy(symbuf, s); \ - symbol_len = ARRAY_SIZE(s) - 1; + symbol_len = arraysize(s) - 1; void NODE_V8SYMBOL_ADD(LPCSTR symbol, int symbol_len, diff --git a/src/node_win32_etw_provider.cc b/src/node_win32_etw_provider.cc index c6bfbeaaf6c989..6877f1977dae8b 100644 --- a/src/node_win32_etw_provider.cc +++ b/src/node_win32_etw_provider.cc @@ -56,7 +56,7 @@ struct v8tags trace_codes[] = { // If prefix is not in filtered list return -1, // else return length of prefix and marker. int FilterCodeEvents(const char* name, size_t len) { - for (int i = 0; i < ARRAY_SIZE(trace_codes); i++) { + for (size_t i = 0; i < arraysize(trace_codes); i++) { size_t prelen = trace_codes[i].prelen; if (prelen < len) { if (strncmp(name, trace_codes[i].prefix, prelen) == 0) { diff --git a/src/node_zlib.cc b/src/node_zlib.cc index d84344228ecf35..4bf5cc76ba5b01 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -332,7 +332,7 @@ class ZCtx : public AsyncWrap { // call the write() cb Local args[2] = { avail_in, avail_out }; - ctx->MakeCallback(env->callback_string(), ARRAY_SIZE(args), args); + ctx->MakeCallback(env->callback_string(), arraysize(args), args); ctx->Unref(); if (ctx->pending_close_) @@ -354,7 +354,7 @@ class ZCtx : public AsyncWrap { OneByteString(env->isolate(), message), Number::New(env->isolate(), ctx->err_) }; - ctx->MakeCallback(env->onerror_string(), ARRAY_SIZE(args), args); + ctx->MakeCallback(env->onerror_string(), arraysize(args), args); // no hope of rescue. if (ctx->write_in_progress_) diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 1cb6f670867297..db2fd16715c8cd 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -183,7 +183,7 @@ void PipeWrap::OnConnection(uv_stream_t* handle, int status) { }; if (status != 0) { - pipe_wrap->MakeCallback(env->onconnection_string(), ARRAY_SIZE(argv), argv); + pipe_wrap->MakeCallback(env->onconnection_string(), arraysize(argv), argv); return; } @@ -198,7 +198,7 @@ void PipeWrap::OnConnection(uv_stream_t* handle, int status) { // Successful accept. Call the onconnection callback in JavaScript land. argv[1] = client_obj; - pipe_wrap->MakeCallback(env->onconnection_string(), ARRAY_SIZE(argv), argv); + pipe_wrap->MakeCallback(env->onconnection_string(), arraysize(argv), argv); } // TODO(bnoordhuis) Maybe share this with TCPWrap? @@ -233,7 +233,7 @@ void PipeWrap::AfterConnect(uv_connect_t* req, int status) { Boolean::New(env->isolate(), writable) }; - req_wrap->MakeCallback(env->oncomplete_string(), ARRAY_SIZE(argv), argv); + req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv); delete req_wrap; } diff --git a/src/process_wrap.cc b/src/process_wrap.cc index adf1606591f156..7e9d2070bb9b7c 100644 --- a/src/process_wrap.cc +++ b/src/process_wrap.cc @@ -252,7 +252,7 @@ class ProcessWrap : public HandleWrap { OneByteString(env->isolate(), signo_string(term_signal)) }; - wrap->MakeCallback(env->onexit_string(), ARRAY_SIZE(argv), argv); + wrap->MakeCallback(env->onexit_string(), arraysize(argv), argv); } uv_process_t process_; diff --git a/src/stream_base.cc b/src/stream_base.cc index 648c6d3aa59941..7561a09998d445 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -84,7 +84,7 @@ void StreamBase::AfterShutdown(ShutdownWrap* req_wrap, int status) { if (req_wrap->object()->Has(env->context(), env->oncomplete_string()).FromJust()) { - req_wrap->MakeCallback(env->oncomplete_string(), ARRAY_SIZE(argv), argv); + req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv); } delete req_wrap; @@ -132,7 +132,7 @@ int StreamBase::Writev(const FunctionCallbackInfo& args) { if (storage_size > INT_MAX) return UV_ENOBUFS; - if (ARRAY_SIZE(bufs_) < count) + if (arraysize(bufs_) < count) bufs = new uv_buf_t[count]; WriteWrap* req_wrap = WriteWrap::New(env, @@ -391,7 +391,7 @@ void StreamBase::AfterWrite(WriteWrap* req_wrap, int status) { if (req_wrap->object()->Has(env->context(), env->oncomplete_string()).FromJust()) { - req_wrap->MakeCallback(env->oncomplete_string(), ARRAY_SIZE(argv), argv); + req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv); } req_wrap->Dispose(); @@ -420,10 +420,10 @@ void StreamBase::EmitData(ssize_t nread, node::MakeCallback(env, GetObject(), env->onread_string(), - ARRAY_SIZE(argv), + arraysize(argv), argv); } else { - async->MakeCallback(env->onread_string(), ARRAY_SIZE(argv), argv); + async->MakeCallback(env->onread_string(), arraysize(argv), argv); } } diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 5e08f689b4bcec..50fcb506aeaddd 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -269,7 +269,7 @@ void TCPWrap::OnConnection(uv_stream_t* handle, int status) { argv[1] = client_obj; } - tcp_wrap->MakeCallback(env->onconnection_string(), ARRAY_SIZE(argv), argv); + tcp_wrap->MakeCallback(env->onconnection_string(), arraysize(argv), argv); } @@ -295,7 +295,7 @@ void TCPWrap::AfterConnect(uv_connect_t* req, int status) { v8::True(env->isolate()) }; - req_wrap->MakeCallback(env->oncomplete_string(), ARRAY_SIZE(argv), argv); + req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv); delete req_wrap; } diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 2885ef900a533b..ba8f760194d75e 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -284,8 +284,8 @@ void TLSWrap::EncOut() { } char* data[kSimultaneousBufferCount]; - size_t size[ARRAY_SIZE(data)]; - size_t count = ARRAY_SIZE(data); + size_t size[arraysize(data)]; + size_t count = arraysize(data); write_size_ = NodeBIO::FromBIO(enc_out_)->PeekMultiple(data, size, &count); CHECK(write_size_ != 0 && count != 0); @@ -297,7 +297,7 @@ void TLSWrap::EncOut() { this, EncOutCb); - uv_buf_t buf[ARRAY_SIZE(data)]; + uv_buf_t buf[arraysize(data)]; for (size_t i = 0; i < count; i++) buf[i] = uv_buf_init(data[i], size[i]); int err = stream_->DoWrite(write_req, buf, count, nullptr); diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 075800d3a7972b..dc2804812fdf19 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -270,7 +270,7 @@ void UDPWrap::DoSend(const FunctionCallbackInfo& args, int family) { uv_buf_t bufs_[16]; uv_buf_t* bufs = bufs_; - if (ARRAY_SIZE(bufs_) < count) + if (arraysize(bufs_) < count) bufs = new uv_buf_t[count]; // construct uv_buf_t array @@ -406,14 +406,14 @@ void UDPWrap::OnRecv(uv_udp_t* handle, if (nread < 0) { if (buf->base != nullptr) free(buf->base); - wrap->MakeCallback(env->onmessage_string(), ARRAY_SIZE(argv), argv); + wrap->MakeCallback(env->onmessage_string(), arraysize(argv), argv); return; } char* base = static_cast(realloc(buf->base, nread)); argv[2] = Buffer::New(env, base, nread).ToLocalChecked(); argv[3] = AddressToJS(env, addr); - wrap->MakeCallback(env->onmessage_string(), ARRAY_SIZE(argv), argv); + wrap->MakeCallback(env->onmessage_string(), arraysize(argv), argv); } From ea63f79752e286c2987740a52b81cd289d125806 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 31 Mar 2016 12:54:09 +0200 Subject: [PATCH 3/3] src: use size_t for http parser array size fields Make the `num_values_` and `num_fields_` unsigned and remove an erroneous comment. PR-URL: https://github.com/nodejs/node/pull/5969 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- src/node_http_parser.cc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index a9081dc674808f..4087ed263fb1a9 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -193,7 +193,7 @@ class Parser : public AsyncWrap { if (num_fields_ == num_values_) { // start of new field name num_fields_++; - if (num_fields_ == static_cast(arraysize(fields_))) { + if (num_fields_ == arraysize(fields_)) { // ran out of space - flush to javascript land Flush(); num_fields_ = 1; @@ -202,7 +202,7 @@ class Parser : public AsyncWrap { fields_[num_fields_ - 1].Reset(); } - CHECK_LT(num_fields_, static_cast(arraysize(fields_))); + CHECK_LT(num_fields_, arraysize(fields_)); CHECK_EQ(num_fields_, num_values_ + 1); fields_[num_fields_ - 1].Update(at, length); @@ -218,7 +218,7 @@ class Parser : public AsyncWrap { values_[num_values_ - 1].Reset(); } - CHECK_LT(num_values_, static_cast(arraysize(values_))); + CHECK_LT(num_values_, arraysize(values_)); CHECK_EQ(num_values_, num_fields_); values_[num_values_ - 1].Update(at, length); @@ -385,11 +385,11 @@ class Parser : public AsyncWrap { url_.Save(); status_message_.Save(); - for (int i = 0; i < num_fields_; i++) { + for (size_t i = 0; i < num_fields_; i++) { fields_[i].Save(); } - for (int i = 0; i < num_values_; i++) { + for (size_t i = 0; i < num_values_; i++) { values_[i].Save(); } } @@ -637,12 +637,10 @@ class Parser : public AsyncWrap { } Local CreateHeaders() { - // num_values_ is either -1 or the entry # of the last header - // so num_values_ == 0 means there's a single header Local headers = Array::New(env()->isolate()); Local fn = env()->push_values_to_array_function(); Local argv[NODE_PUSH_VAL_TO_ARRAY_MAX * 2]; - int i = 0; + size_t i = 0; do { size_t j = 0; @@ -702,8 +700,8 @@ class Parser : public AsyncWrap { StringPtr values_[32]; // header values StringPtr url_; StringPtr status_message_; - int num_fields_; - int num_values_; + size_t num_fields_; + size_t num_values_; bool have_flushed_; bool got_exception_; Local current_buffer_;