Skip to content

Commit 3079a78

Browse files
targosdanielleadams
authored andcommitted
src: avoid implicit type conversions
This fixes a bunch of C4244 ('conversion' conversion from 'type1' to 'type2', possible loss of data) MSVC warnings in the code base. PR-URL: #37149 Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent eb0daae commit 3079a78

14 files changed

+98
-82
lines changed

src/base64.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ static inline const char* base64_select_table(Base64Mode mode) {
3636
static inline constexpr size_t base64_encoded_size(
3737
size_t size,
3838
Base64Mode mode = Base64Mode::NORMAL) {
39-
return mode == Base64Mode::NORMAL
40-
? ((size + 2) / 3 * 4)
41-
: std::ceil(static_cast<double>(size * 4) / 3);
39+
return mode == Base64Mode::NORMAL ? ((size + 2) / 3 * 4)
40+
: static_cast<size_t>(std::ceil(
41+
static_cast<double>(size * 4) / 3));
4242
}
4343

4444
// Doesn't check for padding at the end. Can be 1-2 bytes over.

src/cares_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,7 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
19011901
Null(env->isolate())
19021902
};
19031903

1904-
uint64_t n = 0;
1904+
uint32_t n = 0;
19051905
const bool verbatim = req_wrap->verbatim();
19061906

19071907
if (status == 0) {

src/heap_utils.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,16 @@ class JSGraph : public EmbedderGraph {
118118
name_str += " ";
119119
name_str += n->Name();
120120
}
121-
if (!String::NewFromUtf8(isolate_, name_str.c_str())
122-
.ToLocal(&value) ||
121+
if (!String::NewFromUtf8(isolate_, name_str.c_str()).ToLocal(&value) ||
123122
obj->Set(context, name_string, value).IsNothing() ||
124123
obj->Set(context,
125124
is_root_string,
126125
Boolean::New(isolate_, n->IsRootNode()))
127126
.IsNothing() ||
128-
obj->Set(context,
129-
size_string,
130-
Number::New(isolate_, n->SizeInBytes()))
127+
obj->Set(
128+
context,
129+
size_string,
130+
Number::New(isolate_, static_cast<double>(n->SizeInBytes())))
131131
.IsNothing() ||
132132
obj->Set(context, edges_string, Array::New(isolate_)).IsNothing()) {
133133
return MaybeLocal<Array>();
@@ -172,7 +172,7 @@ class JSGraph : public EmbedderGraph {
172172
return MaybeLocal<Array>();
173173
}
174174
} else {
175-
edge_name_value = Number::New(isolate_, j++);
175+
edge_name_value = Number::New(isolate_, static_cast<double>(j++));
176176
}
177177
if (edge_obj->Set(context, name_string, edge_name_value).IsNothing() ||
178178
edge_obj->Set(context, to_string, to_object).IsNothing() ||
@@ -262,7 +262,7 @@ class HeapSnapshotStream : public AsyncWrap,
262262
avail = buf.len;
263263
memcpy(buf.base, data, avail);
264264
data += avail;
265-
len -= avail;
265+
len -= static_cast<int>(avail);
266266
EmitRead(size, buf);
267267
}
268268
return kContinue;

src/js_stream.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ void JSStream::ReadBuffer(const FunctionCallbackInfo<Value>& args) {
178178

179179
memcpy(buf.base, data, avail);
180180
data += avail;
181-
len -= avail;
181+
len -= static_cast<int>(avail);
182182
wrap->EmitRead(avail, buf);
183183
}
184184
}

src/js_udp_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ void JSUDPWrap::EmitReceived(const FunctionCallbackInfo<Value>& args) {
161161
ssize_t avail = std::min<size_t>(buf.len, len);
162162
memcpy(buf.base, data, avail);
163163
data += avail;
164-
len -= avail;
164+
len -= static_cast<int>(avail);
165165
wrap->listener()->OnRecv(
166166
avail, buf, reinterpret_cast<sockaddr*>(&addr), flags);
167167
}

src/node.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv) {
10681068
#endif // HAVE_OPENSSL
10691069

10701070
per_process::v8_platform.Initialize(
1071-
per_process::cli_options->v8_thread_pool_size);
1071+
static_cast<int>(per_process::cli_options->v8_thread_pool_size));
10721072
V8::Initialize();
10731073
performance::performance_v8_start = PERFORMANCE_NOW();
10741074
per_process::v8_initialized = true;

src/node_dir.cc

+7-5
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,10 @@ static void AfterDirRead(uv_fs_t* req) {
217217
Local<Array> js_array;
218218
if (!DirentListToArray(env,
219219
dir->dirents,
220-
req->result,
220+
static_cast<int>(req->result),
221221
req_wrap->encoding(),
222-
&error).ToLocal(&js_array)) {
222+
&error)
223+
.ToLocal(&js_array)) {
223224
// Clear libuv resources *before* delivering results to JS land because
224225
// that can schedule another operation on the same uv_dir_t. Ditto below.
225226
after.Clear();
@@ -244,7 +245,7 @@ void DirHandle::Read(const FunctionCallbackInfo<Value>& args) {
244245
ASSIGN_OR_RETURN_UNWRAP(&dir, args.Holder());
245246

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

249250
if (buffer_size != dir->dirents_.size()) {
250251
dir->dirents_.resize(buffer_size);
@@ -280,9 +281,10 @@ void DirHandle::Read(const FunctionCallbackInfo<Value>& args) {
280281
Local<Array> js_array;
281282
if (!DirentListToArray(env,
282283
dir->dir()->dirents,
283-
req_wrap_sync.req.result,
284+
static_cast<int>(req_wrap_sync.req.result),
284285
encoding,
285-
&error).ToLocal(&js_array)) {
286+
&error)
287+
.ToLocal(&js_array)) {
286288
Local<Object> ctx = args[2].As<Object>();
287289
USE(ctx->Set(env->context(), env->error_string(), error));
288290
return;

src/node_file.cc

+18-16
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ MaybeLocal<Promise> FileHandle::ClosePromise() {
367367
Isolate* isolate = close->env()->isolate();
368368
if (req->result < 0) {
369369
HandleScope handle_scope(isolate);
370-
close->Reject(UVException(isolate, req->result, "close"));
370+
close->Reject(
371+
UVException(isolate, static_cast<int>(req->result), "close"));
371372
} else {
372373
close->Resolve();
373374
}
@@ -491,7 +492,7 @@ int FileHandle::ReadStart() {
491492
BaseObjectPtr<FileHandleReadWrap> read_wrap =
492493
std::move(handle->current_read_);
493494

494-
int result = req->result;
495+
ssize_t result = req->result;
495496
uv_buf_t buffer = read_wrap->buffer_;
496497

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

558-
int result = req->result;
559+
int result = static_cast<int>(req->result);
559560
uv_fs_req_cleanup(req);
560561
wrap->Done(result);
561562
}});
@@ -623,13 +624,12 @@ void FSReqAfterScope::Clear() {
623624
// in JS for more flexibility.
624625
void FSReqAfterScope::Reject(uv_fs_t* req) {
625626
BaseObjectPtr<FSReqBase> wrap { wrap_ };
626-
Local<Value> exception =
627-
UVException(wrap_->env()->isolate(),
628-
req->result,
629-
wrap_->syscall(),
630-
nullptr,
631-
req->path,
632-
wrap_->data());
627+
Local<Value> exception = UVException(wrap_->env()->isolate(),
628+
static_cast<int>(req->result),
629+
wrap_->syscall(),
630+
nullptr,
631+
req->path,
632+
wrap_->data());
633633
Clear();
634634
wrap->Reject(exception);
635635
}
@@ -663,19 +663,21 @@ void AfterInteger(uv_fs_t* req) {
663663
FSReqBase* req_wrap = FSReqBase::from_req(req);
664664
FSReqAfterScope after(req_wrap, req);
665665

666-
if (req->result >= 0 && req_wrap->is_plain_open())
667-
req_wrap->env()->AddUnmanagedFd(req->result);
666+
int result = static_cast<int>(req->result);
667+
if (result >= 0 && req_wrap->is_plain_open())
668+
req_wrap->env()->AddUnmanagedFd(result);
668669

669670
if (after.Proceed())
670-
req_wrap->Resolve(Integer::New(req_wrap->env()->isolate(), req->result));
671+
req_wrap->Resolve(Integer::New(req_wrap->env()->isolate(), result));
671672
}
672673

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

677678
if (after.Proceed()) {
678-
FileHandle* fd = FileHandle::New(req_wrap->binding_data(), req->result);
679+
FileHandle* fd = FileHandle::New(req_wrap->binding_data(),
680+
static_cast<int>(req->result));
679681
if (fd == nullptr) return;
680682
req_wrap->Resolve(fd->object());
681683
}
@@ -1430,7 +1432,7 @@ int MKDirpAsync(uv_loop_t* loop,
14301432
Environment* env = req_wrap->env();
14311433
uv_loop_t* loop = env->event_loop();
14321434
std::string path = req->path;
1433-
int err = req->result;
1435+
int err = static_cast<int>(req->result);
14341436

14351437
while (true) {
14361438
switch (err) {
@@ -1476,7 +1478,7 @@ int MKDirpAsync(uv_loop_t* loop,
14761478
int err = uv_fs_stat(loop, req, path.c_str(),
14771479
uv_fs_callback_t{[](uv_fs_t* req) {
14781480
FSReqBase* req_wrap = FSReqBase::from_req(req);
1479-
int err = req->result;
1481+
int err = static_cast<int>(req->result);
14801482
if (reinterpret_cast<intptr_t>(req->data) == UV_EEXIST &&
14811483
req_wrap->continuation_data()->paths().size() > 0) {
14821484
if (err == 0 && S_ISDIR(req->statbuf.st_mode)) {

src/node_http_parser.cc

+12-10
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ class Parser : public AsyncWrap, public StreamListener {
380380
return -1;
381381
}
382382

383-
return val;
383+
return static_cast<int>(val);
384384
}
385385

386386

@@ -403,10 +403,10 @@ class Parser : public AsyncWrap, public StreamListener {
403403
}
404404

405405
Local<Value> argv[3] = {
406-
current_buffer_,
407-
Integer::NewFromUnsigned(env()->isolate(), at - current_buffer_data_),
408-
Integer::NewFromUnsigned(env()->isolate(), length)
409-
};
406+
current_buffer_,
407+
Integer::NewFromUnsigned(
408+
env()->isolate(), static_cast<uint32_t>(at - current_buffer_data_)),
409+
Integer::NewFromUnsigned(env()->isolate(), length)};
410410

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

550550
if (args.Length() > 2) {
551551
CHECK(args[2]->IsNumber());
552-
max_http_header_size = args[2].As<Number>()->Value();
552+
max_http_header_size =
553+
static_cast<uint64_t>(args[2].As<Number>()->Value());
553554
}
554555
if (max_http_header_size == 0) {
555556
max_http_header_size = env->options()->max_http_header_size;
556557
}
557558

558559
if (args.Length() > 4) {
559560
CHECK(args[4]->IsInt32());
560-
headers_timeout = args[4].As<Number>()->Value();
561+
headers_timeout = args[4].As<Int32>()->Value();
561562
}
562563

563564
llhttp_type_t type =
@@ -683,7 +684,7 @@ class Parser : public AsyncWrap, public StreamListener {
683684
// check header parsing time
684685
if (header_parsing_start_time_ != 0 && headers_timeout_ != 0) {
685686
uint64_t now = uv_hrtime();
686-
uint64_t parsing_time = (now - header_parsing_start_time_) / 1e6;
687+
uint64_t parsing_time = (now - header_parsing_start_time_) / 1000000;
687688

688689
if (parsing_time > headers_timeout_) {
689690
Local<Value> cb =
@@ -781,8 +782,9 @@ class Parser : public AsyncWrap, public StreamListener {
781782
if (err == HPE_USER) {
782783
const char* colon = strchr(errno_reason, ':');
783784
CHECK_NOT_NULL(colon);
784-
code = OneByteString(env()->isolate(), errno_reason,
785-
colon - errno_reason);
785+
code = OneByteString(env()->isolate(),
786+
errno_reason,
787+
static_cast<int>(colon - errno_reason));
786788
reason = OneByteString(env()->isolate(), colon + 1);
787789
} else {
788790
code = OneByteString(env()->isolate(), llhttp_errno_name(err));

src/node_options.cc

+6-4
Original file line numberDiff line numberDiff line change
@@ -945,12 +945,14 @@ void GetOptions(const FunctionCallbackInfo<Value>& args) {
945945
*_ppop_instance.Lookup<bool>(field, opts));
946946
break;
947947
case kInteger:
948-
value = Number::New(isolate,
949-
*_ppop_instance.Lookup<int64_t>(field, opts));
948+
value = Number::New(
949+
isolate,
950+
static_cast<double>(*_ppop_instance.Lookup<int64_t>(field, opts)));
950951
break;
951952
case kUInteger:
952-
value = Number::New(isolate,
953-
*_ppop_instance.Lookup<uint64_t>(field, opts));
953+
value = Number::New(
954+
isolate,
955+
static_cast<double>(*_ppop_instance.Lookup<uint64_t>(field, opts)));
954956
break;
955957
case kString:
956958
if (!ToV8Value(context,

src/node_os.cc

+12-7
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,16 @@ static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) {
118118
uv_cpu_info_t* ci = cpu_infos + i;
119119
result.emplace_back(OneByteString(isolate, ci->model));
120120
result.emplace_back(Number::New(isolate, ci->speed));
121-
result.emplace_back(Number::New(isolate, ci->cpu_times.user));
122-
result.emplace_back(Number::New(isolate, ci->cpu_times.nice));
123-
result.emplace_back(Number::New(isolate, ci->cpu_times.sys));
124-
result.emplace_back(Number::New(isolate, ci->cpu_times.idle));
125-
result.emplace_back(Number::New(isolate, ci->cpu_times.irq));
121+
result.emplace_back(
122+
Number::New(isolate, static_cast<double>(ci->cpu_times.user)));
123+
result.emplace_back(
124+
Number::New(isolate, static_cast<double>(ci->cpu_times.nice)));
125+
result.emplace_back(
126+
Number::New(isolate, static_cast<double>(ci->cpu_times.sys)));
127+
result.emplace_back(
128+
Number::New(isolate, static_cast<double>(ci->cpu_times.idle)));
129+
result.emplace_back(
130+
Number::New(isolate, static_cast<double>(ci->cpu_times.irq)));
126131
}
127132

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

132137

133138
static void GetFreeMemory(const FunctionCallbackInfo<Value>& args) {
134-
double amount = uv_get_free_memory();
139+
double amount = static_cast<double>(uv_get_free_memory());
135140
args.GetReturnValue().Set(amount);
136141
}
137142

138143

139144
static void GetTotalMemory(const FunctionCallbackInfo<Value>& args) {
140-
double amount = uv_get_total_memory();
145+
double amount = static_cast<double>(uv_get_total_memory());
141146
args.GetReturnValue().Set(amount);
142147
}
143148

src/node_perf.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ std::ostream& operator<<(std::ostream& o,
8888

8989
void PerformanceState::Mark(enum PerformanceMilestone milestone,
9090
uint64_t ts) {
91-
this->milestones[milestone] = ts;
91+
this->milestones[milestone] = static_cast<double>(ts);
9292
TRACE_EVENT_INSTANT_WITH_TIMESTAMP0(
9393
TRACING_CATEGORY_NODE1(bootstrap),
9494
GetPerformanceMilestoneName(milestone),

0 commit comments

Comments
 (0)