Skip to content

Commit

Permalink
src: avoid implicit type conversions
Browse files Browse the repository at this point in the history
This fixes a bunch of C4244 ('conversion' conversion from 'type1' to
'type2', possible loss of data) MSVC warnings in the code base.
  • Loading branch information
targos committed Feb 7, 2021
1 parent 907d6b6 commit 95e3ba0
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 82 deletions.
6 changes: 3 additions & 3 deletions src/base64.h
Expand Up @@ -36,9 +36,9 @@ static inline const char* base64_select_table(Base64Mode mode) {
static inline constexpr size_t base64_encoded_size(
size_t size,
Base64Mode mode = Base64Mode::NORMAL) {
return mode == Base64Mode::NORMAL
? ((size + 2) / 3 * 4)
: std::ceil(static_cast<double>(size * 4) / 3);
return mode == Base64Mode::NORMAL ? ((size + 2) / 3 * 4)
: static_cast<size_t>(std::ceil(
static_cast<double>(size * 4) / 3));
}

// Doesn't check for padding at the end. Can be 1-2 bytes over.
Expand Down
2 changes: 1 addition & 1 deletion src/cares_wrap.cc
Expand Up @@ -1901,7 +1901,7 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
Null(env->isolate())
};

uint64_t n = 0;
uint32_t n = 0;
const bool verbatim = req_wrap->verbatim();

if (status == 0) {
Expand Down
14 changes: 7 additions & 7 deletions src/heap_utils.cc
Expand Up @@ -118,16 +118,16 @@ class JSGraph : public EmbedderGraph {
name_str += " ";
name_str += n->Name();
}
if (!String::NewFromUtf8(isolate_, name_str.c_str())
.ToLocal(&value) ||
if (!String::NewFromUtf8(isolate_, name_str.c_str()).ToLocal(&value) ||
obj->Set(context, name_string, value).IsNothing() ||
obj->Set(context,
is_root_string,
Boolean::New(isolate_, n->IsRootNode()))
.IsNothing() ||
obj->Set(context,
size_string,
Number::New(isolate_, n->SizeInBytes()))
obj->Set(
context,
size_string,
Number::New(isolate_, static_cast<double>(n->SizeInBytes())))
.IsNothing() ||
obj->Set(context, edges_string, Array::New(isolate_)).IsNothing()) {
return MaybeLocal<Array>();
Expand Down Expand Up @@ -172,7 +172,7 @@ class JSGraph : public EmbedderGraph {
return MaybeLocal<Array>();
}
} else {
edge_name_value = Number::New(isolate_, j++);
edge_name_value = Number::New(isolate_, static_cast<double>(j++));
}
if (edge_obj->Set(context, name_string, edge_name_value).IsNothing() ||
edge_obj->Set(context, to_string, to_object).IsNothing() ||
Expand Down Expand Up @@ -262,7 +262,7 @@ class HeapSnapshotStream : public AsyncWrap,
avail = buf.len;
memcpy(buf.base, data, avail);
data += avail;
len -= avail;
len -= static_cast<int>(avail);
EmitRead(size, buf);
}
return kContinue;
Expand Down
2 changes: 1 addition & 1 deletion src/js_stream.cc
Expand Up @@ -178,7 +178,7 @@ void JSStream::ReadBuffer(const FunctionCallbackInfo<Value>& args) {

memcpy(buf.base, data, avail);
data += avail;
len -= avail;
len -= static_cast<int>(avail);
wrap->EmitRead(avail, buf);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/js_udp_wrap.cc
Expand Up @@ -161,7 +161,7 @@ void JSUDPWrap::EmitReceived(const FunctionCallbackInfo<Value>& args) {
ssize_t avail = std::min<size_t>(buf.len, len);
memcpy(buf.base, data, avail);
data += avail;
len -= avail;
len -= static_cast<int>(avail);
wrap->listener()->OnRecv(
avail, buf, reinterpret_cast<sockaddr*>(&addr), flags);
}
Expand Down
2 changes: 1 addition & 1 deletion src/node.cc
Expand Up @@ -1023,7 +1023,7 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv) {
#endif // HAVE_OPENSSL

per_process::v8_platform.Initialize(
per_process::cli_options->v8_thread_pool_size);
static_cast<int>(per_process::cli_options->v8_thread_pool_size));
V8::Initialize();
performance::performance_v8_start = PERFORMANCE_NOW();
per_process::v8_initialized = true;
Expand Down
12 changes: 7 additions & 5 deletions src/node_dir.cc
Expand Up @@ -217,9 +217,10 @@ static void AfterDirRead(uv_fs_t* req) {
Local<Array> js_array;
if (!DirentListToArray(env,
dir->dirents,
req->result,
static_cast<int>(req->result),
req_wrap->encoding(),
&error).ToLocal(&js_array)) {
&error)
.ToLocal(&js_array)) {
// Clear libuv resources *before* delivering results to JS land because
// that can schedule another operation on the same uv_dir_t. Ditto below.
after.Clear();
Expand All @@ -244,7 +245,7 @@ void DirHandle::Read(const FunctionCallbackInfo<Value>& args) {
ASSIGN_OR_RETURN_UNWRAP(&dir, args.Holder());

CHECK(args[1]->IsNumber());
uint64_t buffer_size = args[1].As<Number>()->Value();
uint64_t buffer_size = static_cast<uint64_t>(args[1].As<Number>()->Value());

if (buffer_size != dir->dirents_.size()) {
dir->dirents_.resize(buffer_size);
Expand Down Expand Up @@ -280,9 +281,10 @@ void DirHandle::Read(const FunctionCallbackInfo<Value>& args) {
Local<Array> js_array;
if (!DirentListToArray(env,
dir->dir()->dirents,
req_wrap_sync.req.result,
static_cast<int>(req_wrap_sync.req.result),
encoding,
&error).ToLocal(&js_array)) {
&error)
.ToLocal(&js_array)) {
Local<Object> ctx = args[2].As<Object>();
USE(ctx->Set(env->context(), env->error_string(), error));
return;
Expand Down
34 changes: 18 additions & 16 deletions src/node_file.cc
Expand Up @@ -367,7 +367,8 @@ MaybeLocal<Promise> FileHandle::ClosePromise() {
Isolate* isolate = close->env()->isolate();
if (req->result < 0) {
HandleScope handle_scope(isolate);
close->Reject(UVException(isolate, req->result, "close"));
close->Reject(
UVException(isolate, static_cast<int>(req->result), "close"));
} else {
close->Resolve();
}
Expand Down Expand Up @@ -491,7 +492,7 @@ int FileHandle::ReadStart() {
BaseObjectPtr<FileHandleReadWrap> read_wrap =
std::move(handle->current_read_);

int result = req->result;
ssize_t result = req->result;
uv_buf_t buffer = read_wrap->buffer_;

uv_fs_req_cleanup(req);
Expand Down Expand Up @@ -555,7 +556,7 @@ int FileHandle::DoShutdown(ShutdownWrap* req_wrap) {
FileHandle* handle = static_cast<FileHandle*>(wrap->stream());
handle->AfterClose();

int result = req->result;
int result = static_cast<int>(req->result);
uv_fs_req_cleanup(req);
wrap->Done(result);
}});
Expand Down Expand Up @@ -623,13 +624,12 @@ void FSReqAfterScope::Clear() {
// in JS for more flexibility.
void FSReqAfterScope::Reject(uv_fs_t* req) {
BaseObjectPtr<FSReqBase> wrap { wrap_ };
Local<Value> exception =
UVException(wrap_->env()->isolate(),
req->result,
wrap_->syscall(),
nullptr,
req->path,
wrap_->data());
Local<Value> exception = UVException(wrap_->env()->isolate(),
static_cast<int>(req->result),
wrap_->syscall(),
nullptr,
req->path,
wrap_->data());
Clear();
wrap->Reject(exception);
}
Expand Down Expand Up @@ -663,19 +663,21 @@ void AfterInteger(uv_fs_t* req) {
FSReqBase* req_wrap = FSReqBase::from_req(req);
FSReqAfterScope after(req_wrap, req);

if (req->result >= 0 && req_wrap->is_plain_open())
req_wrap->env()->AddUnmanagedFd(req->result);
int result = static_cast<int>(req->result);
if (result >= 0 && req_wrap->is_plain_open())
req_wrap->env()->AddUnmanagedFd(result);

if (after.Proceed())
req_wrap->Resolve(Integer::New(req_wrap->env()->isolate(), req->result));
req_wrap->Resolve(Integer::New(req_wrap->env()->isolate(), result));
}

void AfterOpenFileHandle(uv_fs_t* req) {
FSReqBase* req_wrap = FSReqBase::from_req(req);
FSReqAfterScope after(req_wrap, req);

if (after.Proceed()) {
FileHandle* fd = FileHandle::New(req_wrap->binding_data(), req->result);
FileHandle* fd = FileHandle::New(req_wrap->binding_data(),
static_cast<int>(req->result));
if (fd == nullptr) return;
req_wrap->Resolve(fd->object());
}
Expand Down Expand Up @@ -1430,7 +1432,7 @@ int MKDirpAsync(uv_loop_t* loop,
Environment* env = req_wrap->env();
uv_loop_t* loop = env->event_loop();
std::string path = req->path;
int err = req->result;
int err = static_cast<int>(req->result);

while (true) {
switch (err) {
Expand Down Expand Up @@ -1476,7 +1478,7 @@ int MKDirpAsync(uv_loop_t* loop,
int err = uv_fs_stat(loop, req, path.c_str(),
uv_fs_callback_t{[](uv_fs_t* req) {
FSReqBase* req_wrap = FSReqBase::from_req(req);
int err = req->result;
int err = static_cast<int>(req->result);
if (reinterpret_cast<intptr_t>(req->data) == UV_EEXIST &&
req_wrap->continuation_data()->paths().size() > 0) {
if (err == 0 && S_ISDIR(req->statbuf.st_mode)) {
Expand Down
22 changes: 12 additions & 10 deletions src/node_http_parser.cc
Expand Up @@ -380,7 +380,7 @@ class Parser : public AsyncWrap, public StreamListener {
return -1;
}

return val;
return static_cast<int>(val);
}


Expand All @@ -403,10 +403,10 @@ class Parser : public AsyncWrap, public StreamListener {
}

Local<Value> argv[3] = {
current_buffer_,
Integer::NewFromUnsigned(env()->isolate(), at - current_buffer_data_),
Integer::NewFromUnsigned(env()->isolate(), length)
};
current_buffer_,
Integer::NewFromUnsigned(
env()->isolate(), static_cast<uint32_t>(at - current_buffer_data_)),
Integer::NewFromUnsigned(env()->isolate(), length)};

MaybeLocal<Value> r = MakeCallback(cb.As<Function>(),
arraysize(argv),
Expand Down Expand Up @@ -549,15 +549,16 @@ class Parser : public AsyncWrap, public StreamListener {

if (args.Length() > 2) {
CHECK(args[2]->IsNumber());
max_http_header_size = args[2].As<Number>()->Value();
max_http_header_size =
static_cast<uint64_t>(args[2].As<Number>()->Value());
}
if (max_http_header_size == 0) {
max_http_header_size = env->options()->max_http_header_size;
}

if (args.Length() > 4) {
CHECK(args[4]->IsInt32());
headers_timeout = args[4].As<Number>()->Value();
headers_timeout = args[4].As<Int32>()->Value();
}

llhttp_type_t type =
Expand Down Expand Up @@ -683,7 +684,7 @@ class Parser : public AsyncWrap, public StreamListener {
// check header parsing time
if (header_parsing_start_time_ != 0 && headers_timeout_ != 0) {
uint64_t now = uv_hrtime();
uint64_t parsing_time = (now - header_parsing_start_time_) / 1e6;
uint64_t parsing_time = (now - header_parsing_start_time_) / 1000000;

if (parsing_time > headers_timeout_) {
Local<Value> cb =
Expand Down Expand Up @@ -781,8 +782,9 @@ class Parser : public AsyncWrap, public StreamListener {
if (err == HPE_USER) {
const char* colon = strchr(errno_reason, ':');
CHECK_NOT_NULL(colon);
code = OneByteString(env()->isolate(), errno_reason,
colon - errno_reason);
code = OneByteString(env()->isolate(),
errno_reason,
static_cast<int>(colon - errno_reason));
reason = OneByteString(env()->isolate(), colon + 1);
} else {
code = OneByteString(env()->isolate(), llhttp_errno_name(err));
Expand Down
10 changes: 6 additions & 4 deletions src/node_options.cc
Expand Up @@ -945,12 +945,14 @@ void GetOptions(const FunctionCallbackInfo<Value>& args) {
*_ppop_instance.Lookup<bool>(field, opts));
break;
case kInteger:
value = Number::New(isolate,
*_ppop_instance.Lookup<int64_t>(field, opts));
value = Number::New(
isolate,
static_cast<double>(*_ppop_instance.Lookup<int64_t>(field, opts)));
break;
case kUInteger:
value = Number::New(isolate,
*_ppop_instance.Lookup<uint64_t>(field, opts));
value = Number::New(
isolate,
static_cast<double>(*_ppop_instance.Lookup<uint64_t>(field, opts)));
break;
case kString:
if (!ToV8Value(context,
Expand Down
19 changes: 12 additions & 7 deletions src/node_os.cc
Expand Up @@ -118,11 +118,16 @@ static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) {
uv_cpu_info_t* ci = cpu_infos + i;
result.emplace_back(OneByteString(isolate, ci->model));
result.emplace_back(Number::New(isolate, ci->speed));
result.emplace_back(Number::New(isolate, ci->cpu_times.user));
result.emplace_back(Number::New(isolate, ci->cpu_times.nice));
result.emplace_back(Number::New(isolate, ci->cpu_times.sys));
result.emplace_back(Number::New(isolate, ci->cpu_times.idle));
result.emplace_back(Number::New(isolate, ci->cpu_times.irq));
result.emplace_back(
Number::New(isolate, static_cast<double>(ci->cpu_times.user)));
result.emplace_back(
Number::New(isolate, static_cast<double>(ci->cpu_times.nice)));
result.emplace_back(
Number::New(isolate, static_cast<double>(ci->cpu_times.sys)));
result.emplace_back(
Number::New(isolate, static_cast<double>(ci->cpu_times.idle)));
result.emplace_back(
Number::New(isolate, static_cast<double>(ci->cpu_times.irq)));
}

uv_free_cpu_info(cpu_infos, count);
Expand All @@ -131,13 +136,13 @@ static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) {


static void GetFreeMemory(const FunctionCallbackInfo<Value>& args) {
double amount = uv_get_free_memory();
double amount = static_cast<double>(uv_get_free_memory());
args.GetReturnValue().Set(amount);
}


static void GetTotalMemory(const FunctionCallbackInfo<Value>& args) {
double amount = uv_get_total_memory();
double amount = static_cast<double>(uv_get_total_memory());
args.GetReturnValue().Set(amount);
}

Expand Down
2 changes: 1 addition & 1 deletion src/node_perf.cc
Expand Up @@ -88,7 +88,7 @@ std::ostream& operator<<(std::ostream& o,

void PerformanceState::Mark(enum PerformanceMilestone milestone,
uint64_t ts) {
this->milestones[milestone] = ts;
this->milestones[milestone] = static_cast<double>(ts);
TRACE_EVENT_INSTANT_WITH_TIMESTAMP0(
TRACING_CATEGORY_NODE1(bootstrap),
GetPerformanceMilestoneName(milestone),
Expand Down

0 comments on commit 95e3ba0

Please sign in to comment.