Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: reduce scope of variables throughout codebase #23297

Merged
merged 7 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -429,17 +429,16 @@ void cares_wrap_hostent_cpy(struct hostent* dest, struct hostent* src) {

/* copy `h_aliases` */
size_t alias_count;
size_t cur_alias_length;
for (alias_count = 0;
src->h_aliases[alias_count] != nullptr;
alias_count++) {
}

dest->h_aliases = node::Malloc<char*>(alias_count + 1);
for (size_t i = 0; i < alias_count; i++) {
cur_alias_length = strlen(src->h_aliases[i]);
dest->h_aliases[i] = node::Malloc(cur_alias_length + 1);
memcpy(dest->h_aliases[i], src->h_aliases[i], cur_alias_length + 1);
const size_t cur_alias_size = strlen(src->h_aliases[i]) + 1;
dest->h_aliases[i] = node::Malloc(cur_alias_size);
memcpy(dest->h_aliases[i], src->h_aliases[i], cur_alias_size);
}
dest->h_aliases[alias_count] = nullptr;

Expand Down Expand Up @@ -1065,7 +1064,6 @@ int ParseSoaReply(Environment* env,
/* Can't use ares_parse_soa_reply() here which can only parse single record */
unsigned int ancount = cares_get_16bit(buf + 6);
unsigned char* ptr = buf + NS_HFIXEDSZ;
int rr_type, rr_len;
char* name;
char* rr_name;
long temp_len; // NOLINT(runtime/int)
Expand Down Expand Up @@ -1094,8 +1092,8 @@ int ParseSoaReply(Environment* env,
break;
}

rr_type = cares_get_16bit(ptr);
rr_len = cares_get_16bit(ptr + 8);
const int rr_type = cares_get_16bit(ptr);
const int rr_len = cares_get_16bit(ptr + 8);
ptr += NS_RRFIXEDSZ;

/* only need SOA */
Expand Down
15 changes: 7 additions & 8 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -449,16 +449,15 @@ std::string ReadFile(uv_file file) {
uv_fs_t req;
char buffer_memory[4096];
uv_buf_t buf = uv_buf_init(buffer_memory, sizeof(buffer_memory));
int r;

do {
r = uv_fs_read(uv_default_loop(),
&req,
file,
&buf,
1,
contents.length(), // offset
nullptr);
const int r = uv_fs_read(uv_default_loop(),
&req,
file,
&buf,
1,
contents.length(), // offset
nullptr);
uv_fs_req_cleanup(&req);

if (r <= 0)
Expand Down
25 changes: 8 additions & 17 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -573,17 +573,13 @@ int SSL_CTX_use_certificate_chain(SSL_CTX* ctx,
if (ret) {
// If we could set up our certificate, now proceed to
// the CA certificates.
int r;

SSL_CTX_clear_extra_chain_certs(ctx);

for (int i = 0; i < sk_X509_num(extra_certs); i++) {
X509* ca = sk_X509_value(extra_certs, i);

// NOTE: Increments reference count on `ca`
r = SSL_CTX_add1_chain_cert(ctx, ca);

if (!r) {
if (!SSL_CTX_add1_chain_cert(ctx, ca)) {
ret = 0;
issuer = nullptr;
break;
Expand Down Expand Up @@ -1580,15 +1576,11 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
if (index < 0)
continue;

X509_EXTENSION* ext;
int rv;

ext = X509_get_ext(cert, index);
X509_EXTENSION* ext = X509_get_ext(cert, index);
CHECK_NOT_NULL(ext);

if (!SafeX509ExtPrint(bio.get(), ext)) {
rv = X509V3_EXT_print(bio.get(), ext, 0, 0);
CHECK_EQ(rv, 1);
CHECK_EQ(1, X509V3_EXT_print(bio.get(), ext, 0, 0));
}

BIO_get_mem_ptr(bio.get(), &mem);
Expand Down Expand Up @@ -3746,7 +3738,6 @@ SignBase::Error Verify::VerifyFinal(const char* key_pem,
EVPKeyPointer pkey;
unsigned char m[EVP_MAX_MD_SIZE];
unsigned int m_len;
int r = 0;
*verify_result = false;
EVPMDPointer mdctx = std::move(mdctx_);

Expand All @@ -3762,11 +3753,11 @@ SignBase::Error Verify::VerifyFinal(const char* key_pem,
ApplyRSAOptions(pkey, pkctx.get(), padding, saltlen) &&
EVP_PKEY_CTX_set_signature_md(pkctx.get(),
EVP_MD_CTX_md(mdctx.get())) > 0) {
r = EVP_PKEY_verify(pkctx.get(),
reinterpret_cast<const unsigned char*>(sig),
siglen,
m,
m_len);
const int r = EVP_PKEY_verify(pkctx.get(),
reinterpret_cast<const unsigned char*>(sig),
siglen,
m,
m_len);
*verify_result = r == 1;
}

Expand Down
4 changes: 2 additions & 2 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ void URLHost::ParseIPv6Host(const char* input, size_t length) {
uint16_t* compress_pointer = nullptr;
const char* pointer = input;
const char* end = pointer + length;
unsigned value, len, swaps, numbers_seen;
unsigned value, len, numbers_seen;
char ch = pointer < end ? pointer[0] : kEOL;
if (ch == ':') {
if (length < 2 || pointer[1] != ':')
Expand Down Expand Up @@ -881,7 +881,7 @@ void URLHost::ParseIPv6Host(const char* input, size_t length) {
}

if (compress_pointer != nullptr) {
swaps = piece_pointer - compress_pointer;
unsigned swaps = piece_pointer - compress_pointer;
piece_pointer = buffer_end - 1;
while (piece_pointer != &value_.ipv6[0] && swaps > 0) {
uint16_t temp = *piece_pointer;
Expand Down
2 changes: 1 addition & 1 deletion src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ void Worker::Run() {
TRACE_STR_COPY(name.c_str()));
MultiIsolatePlatform* platform = isolate_data_->platform();
CHECK_NE(platform, nullptr);
bool inspector_started = false;

Debug(this, "Starting worker with id %llu", thread_id_);
{
Locker locker(isolate_);
Isolate::Scope isolate_scope(isolate_);
SealHandleScope outer_seal(isolate_);
bool inspector_started = false;

{
Context::Scope context_scope(env_->context());
Expand Down
4 changes: 1 addition & 3 deletions src/stream_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,6 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
if (args[2]->IsObject())
send_handle_obj = args[2].As<Object>();

int err;

// Compute the size of the storage that the string will be flattened into.
// For UTF8 strings that are very long, go ahead and take the hit for
// computing their actual size, rather than tripling the storage.
Expand Down Expand Up @@ -243,7 +241,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {

uv_buf_t* bufs = &buf;
size_t count = 1;
err = DoTryWrite(&bufs, &count);
const int err = DoTryWrite(&bufs, &count);
// Keep track of the bytes written here, because we're taking a shortcut
// by using `DoTryWrite()` directly instead of using the utilities
// provided by `Write()`.
Expand Down
4 changes: 1 addition & 3 deletions src/tracing/node_trace_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ void NodeTraceWriter::WriteSuffix() {
NodeTraceWriter::~NodeTraceWriter() {
WriteSuffix();
uv_fs_t req;
int err;
if (fd_ != -1) {
err = uv_fs_close(nullptr, &req, fd_, nullptr);
CHECK_EQ(err, 0);
CHECK_EQ(0, uv_fs_close(nullptr, &req, fd_, nullptr));
uv_fs_req_cleanup(&req);
}
uv_async_send(&exit_signal_);
Expand Down