Skip to content

Commit c405c2e

Browse files
committed
src: avoid implicit type conversions (take 2)
This fixes more C4244 MSVC warnings in the code base. Refs: nodejs#37149
1 parent 521c08d commit c405c2e

16 files changed

+44
-41
lines changed

src/base64-inl.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ bool base64_decode_group_slow(char* const dst, const size_t dstlen,
3232
uint8_t lo;
3333
#define V(expr) \
3434
for (;;) { \
35-
const uint8_t c = src[*i]; \
35+
const uint8_t c = static_cast<uint8_t>(src[*i]); \
3636
lo = unbase64(c); \
3737
*i += 1; \
3838
if (lo < 64) \
@@ -66,10 +66,10 @@ size_t base64_decode_fast(char* const dst, const size_t dstlen,
6666
size_t k = 0;
6767
while (i < max_i && k < max_k) {
6868
const unsigned char txt[] = {
69-
static_cast<unsigned char>(unbase64(src[i + 0])),
70-
static_cast<unsigned char>(unbase64(src[i + 1])),
71-
static_cast<unsigned char>(unbase64(src[i + 2])),
72-
static_cast<unsigned char>(unbase64(src[i + 3])),
69+
static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 0]))),
70+
static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 1]))),
71+
static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 2]))),
72+
static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 3]))),
7373
};
7474

7575
const uint32_t v = ReadUint32BE(txt);

src/crypto/crypto_dsa.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ Maybe<bool> GetDsaKeyDetail(
149149
if (target->Set(
150150
env->context(),
151151
env->modulus_length_string(),
152-
Number::New(env->isolate(), modulus_length)).IsNothing() ||
152+
Number::New(env->isolate(), static_cast<double>(modulus_length))).IsNothing() ||
153153
target->Set(
154154
env->context(),
155155
env->divisor_length_string(),
156-
Number::New(env->isolate(), divisor_length)).IsNothing()) {
156+
Number::New(env->isolate(), static_cast<double>(divisor_length))).IsNothing()) {
157157
return Nothing<bool>();
158158
}
159159

src/crypto/crypto_keygen.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Maybe<bool> SecretKeyGenTraits::AdditionalConfig(
6666
SecretKeyGenConfig* params) {
6767
Environment* env = Environment::GetCurrent(args);
6868
CHECK(args[*offset]->IsUint32());
69-
params->length = std::trunc(args[*offset].As<Uint32>()->Value() / CHAR_BIT);
69+
params->length = static_cast<size_t>(std::trunc(args[*offset].As<Uint32>()->Value() / CHAR_BIT));
7070
if (params->length > INT_MAX) {
7171
const std::string msg{
7272
SPrintF("length must be less than or equal to %s bits",

src/crypto/crypto_keys.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ Maybe<bool> GetSecretKeyDetail(
532532
return target->Set(
533533
env->context(),
534534
env->length_string(),
535-
Number::New(env->isolate(), length));
535+
Number::New(env->isolate(), static_cast<double>(length)));
536536
}
537537

538538
Maybe<bool> GetAsymmetricKeyDetail(

src/crypto/crypto_rsa.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ Maybe<bool> GetRsaKeyDetail(
535535
if (target->Set(
536536
env->context(),
537537
env->modulus_length_string(),
538-
Number::New(env->isolate(), modulus_length)).IsNothing()) {
538+
Number::New(env->isolate(), static_cast<double>(modulus_length))).IsNothing()) {
539539
return Nothing<bool>();
540540
}
541541

src/inspector/worker_inspector.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace {
1111
class WorkerStartedRequest : public Request {
1212
public:
1313
WorkerStartedRequest(
14-
int id,
14+
uint64_t id,
1515
const std::string& url,
1616
std::shared_ptr<node::inspector::MainThreadHandle> worker_thread,
1717
bool waiting)
@@ -28,7 +28,7 @@ class WorkerStartedRequest : public Request {
2828
return "Worker " + std::to_string(id);
2929
}
3030

31-
int id_;
31+
uint64_t id_;
3232
WorkerInfo info_;
3333
bool waiting_;
3434
};
@@ -42,20 +42,20 @@ void Report(const std::unique_ptr<WorkerDelegate>& delegate,
4242

4343
class WorkerFinishedRequest : public Request {
4444
public:
45-
explicit WorkerFinishedRequest(int worker_id) : worker_id_(worker_id) {}
45+
explicit WorkerFinishedRequest(uint64_t worker_id) : worker_id_(worker_id) {}
4646

4747
void Call(MainThreadInterface* thread) override {
4848
thread->inspector_agent()->GetWorkerManager()->WorkerFinished(worker_id_);
4949
}
5050

5151
private:
52-
int worker_id_;
52+
uint64_t worker_id_;
5353
};
5454
} // namespace
5555

5656

5757
ParentInspectorHandle::ParentInspectorHandle(
58-
int id, const std::string& url,
58+
uint64_t id, const std::string& url,
5959
std::shared_ptr<MainThreadHandle> parent_thread, bool wait_for_connect)
6060
: id_(id), url_(url), parent_thread_(parent_thread),
6161
wait_(wait_for_connect) {}
@@ -78,11 +78,11 @@ std::unique_ptr<inspector::InspectorSession> ParentInspectorHandle::Connect(
7878
return parent_thread_->Connect(std::move(delegate), prevent_shutdown);
7979
}
8080

81-
void WorkerManager::WorkerFinished(int session_id) {
81+
void WorkerManager::WorkerFinished(uint64_t session_id) {
8282
children_.erase(session_id);
8383
}
8484

85-
void WorkerManager::WorkerStarted(int session_id,
85+
void WorkerManager::WorkerStarted(uint64_t session_id,
8686
const WorkerInfo& info,
8787
bool waiting) {
8888
if (info.worker_thread->Expired())
@@ -94,7 +94,7 @@ void WorkerManager::WorkerStarted(int session_id,
9494
}
9595

9696
std::unique_ptr<ParentInspectorHandle>
97-
WorkerManager::NewParentHandle(int thread_id, const std::string& url) {
97+
WorkerManager::NewParentHandle(uint64_t thread_id, const std::string& url) {
9898
bool wait = !delegates_waiting_on_start_.empty();
9999
return std::make_unique<ParentInspectorHandle>(thread_id, url, thread_, wait);
100100
}

src/inspector/worker_inspector.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ struct WorkerInfo {
5353

5454
class ParentInspectorHandle {
5555
public:
56-
ParentInspectorHandle(int id, const std::string& url,
56+
ParentInspectorHandle(uint64_t id, const std::string& url,
5757
std::shared_ptr<MainThreadHandle> parent_thread,
5858
bool wait_for_connect);
5959
~ParentInspectorHandle();
6060
std::unique_ptr<ParentInspectorHandle> NewParentInspectorHandle(
61-
int thread_id, const std::string& url) {
61+
uint64_t thread_id, const std::string& url) {
6262
return std::make_unique<ParentInspectorHandle>(thread_id,
6363
url,
6464
parent_thread_,
@@ -75,7 +75,7 @@ class ParentInspectorHandle {
7575
bool prevent_shutdown);
7676

7777
private:
78-
int id_;
78+
uint64_t id_;
7979
std::string url_;
8080
std::shared_ptr<MainThreadHandle> parent_thread_;
8181
bool wait_;
@@ -87,9 +87,9 @@ class WorkerManager : public std::enable_shared_from_this<WorkerManager> {
8787
: thread_(thread) {}
8888

8989
std::unique_ptr<ParentInspectorHandle> NewParentHandle(
90-
int thread_id, const std::string& url);
91-
void WorkerStarted(int session_id, const WorkerInfo& info, bool waiting);
92-
void WorkerFinished(int session_id);
90+
uint64_t thread_id, const std::string& url);
91+
void WorkerStarted(uint64_t session_id, const WorkerInfo& info, bool waiting);
92+
void WorkerFinished(uint64_t session_id);
9393
std::unique_ptr<WorkerManagerEventHandle> SetAutoAttach(
9494
std::unique_ptr<WorkerDelegate> attach_delegate);
9595
void SetWaitOnStartForDelegate(int id, bool wait);
@@ -100,7 +100,7 @@ class WorkerManager : public std::enable_shared_from_this<WorkerManager> {
100100

101101
private:
102102
std::shared_ptr<MainThreadHandle> thread_;
103-
std::unordered_map<int, WorkerInfo> children_;
103+
std::unordered_map<uint64_t, WorkerInfo> children_;
104104
std::unordered_map<int, std::unique_ptr<WorkerDelegate>> delegates_;
105105
// If any one needs it, workers stop for all
106106
std::unordered_set<int> delegates_waiting_on_start_;

src/inspector_agent.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ class NodeInspectorClient : public V8InspectorClient {
527527
timers_.emplace(std::piecewise_construct, std::make_tuple(data),
528528
std::make_tuple(env_, [=]() { callback(data); }));
529529
CHECK(result.second);
530-
uint64_t interval = 1000 * interval_s;
530+
uint64_t interval = static_cast<uint64_t>(1000 * interval_s);
531531
result.first->second.Update(interval, interval);
532532
}
533533

@@ -917,7 +917,7 @@ void Agent::SetParentHandle(
917917
}
918918

919919
std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
920-
int thread_id, const std::string& url) {
920+
uint64_t thread_id, const std::string& url) {
921921
if (!parent_handle_) {
922922
return client_->getWorkerManager()->NewParentHandle(thread_id, url);
923923
} else {

src/inspector_agent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Agent {
8484

8585
void SetParentHandle(std::unique_ptr<ParentInspectorHandle> parent_handle);
8686
std::unique_ptr<ParentInspectorHandle> GetParentHandle(
87-
int thread_id, const std::string& url);
87+
uint64_t thread_id, const std::string& url);
8888

8989
// Called to create inspector sessions that can be used from the same thread.
9090
// The inspector responds by using the delegate to send messages back.

src/node_serdes.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ void DeserializerContext::ReadRawBytes(
443443
CHECK_GE(position, ctx->data_);
444444
CHECK_LE(position + length, ctx->data_ + ctx->length_);
445445

446-
const uint32_t offset = position - ctx->data_;
446+
const uint32_t offset = static_cast<uint32_t>(position - ctx->data_);
447447
CHECK_EQ(ctx->data_ + offset, position);
448448

449449
args.GetReturnValue().Set(offset);

src/node_url.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ void URLHost::ParseIPv6Host(const char* input, size_t length) {
896896
}
897897

898898
if (compress_pointer != nullptr) {
899-
unsigned swaps = piece_pointer - compress_pointer;
899+
int64_t swaps = piece_pointer - compress_pointer;
900900
piece_pointer = buffer_end - 1;
901901
while (piece_pointer != &value_.ipv6[0] && swaps > 0) {
902902
uint16_t temp = *piece_pointer;
@@ -963,7 +963,7 @@ void URLHost::ParseIPv4Host(const char* input, size_t length, bool* is_ipv4) {
963963

964964
while (pointer <= end) {
965965
const char ch = pointer < end ? pointer[0] : kEOL;
966-
int remaining = end - pointer - 1;
966+
int64_t remaining = end - pointer - 1;
967967
if (ch == '.' || ch == kEOL) {
968968
if (++parts > static_cast<int>(arraysize(numbers)))
969969
return;
@@ -996,10 +996,11 @@ void URLHost::ParseIPv4Host(const char* input, size_t length, bool* is_ipv4) {
996996
}
997997

998998
type_ = HostType::H_IPV4;
999-
val = numbers[parts - 1];
999+
val = static_cast<uint32_t>(numbers[parts - 1]);
10001000
for (int n = 0; n < parts - 1; n++) {
10011001
double b = 3 - n;
1002-
val += numbers[n] * pow(256, b);
1002+
val +=
1003+
static_cast<uint32_t>(numbers[n]) * static_cast<uint32_t>(pow(256, b));
10031004
}
10041005

10051006
value_.ipv4 = val;

src/node_wasi.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ void WASI::ArgsGet(const FunctionCallbackInfo<Value>& args) {
279279

280280
if (err == UVWASI_ESUCCESS) {
281281
for (size_t i = 0; i < wasi->uvw_.argc; i++) {
282-
uint32_t offset = argv_buf_offset + (argv[i] - argv[0]);
282+
uint32_t offset =
283+
static_cast<uint32_t>(argv_buf_offset + (argv[i] - argv[0]));
283284
uvwasi_serdes_write_uint32_t(memory,
284285
argv_offset +
285286
(i * UVWASI_SERDES_SIZE_uint32_t),
@@ -410,7 +411,8 @@ void WASI::EnvironGet(const FunctionCallbackInfo<Value>& args) {
410411

411412
if (err == UVWASI_ESUCCESS) {
412413
for (size_t i = 0; i < wasi->uvw_.envc; i++) {
413-
uint32_t offset = environ_buf_offset + (environment[i] - environment[0]);
414+
uint32_t offset = static_cast<uint32_t>(
415+
environ_buf_offset + (environment[i] - environment[0]));
414416

415417
uvwasi_serdes_write_uint32_t(memory,
416418
environ_offset +

src/signal_wrap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class SignalWrap : public HandleWrap {
153153

154154
void DecreaseSignalHandlerCount(int signum) {
155155
Mutex::ScopedLock lock(handled_signals_mutex);
156-
int new_handler_count = --handled_signals[signum];
156+
int64_t new_handler_count = --handled_signals[signum];
157157
CHECK_GE(new_handler_count, 0);
158158
if (new_handler_count == 0)
159159
handled_signals.erase(signum);

src/stream_base.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ MaybeLocal<Value> StreamBase::CallJSOnreadMethod(ssize_t nread,
337337
}
338338
}
339339

340-
env->stream_base_state()[kReadBytesOrError] = nread;
340+
env->stream_base_state()[kReadBytesOrError] = static_cast<int32_t>(nread);
341341
env->stream_base_state()[kArrayBufferOffset] = offset;
342342

343343
Local<Value> argv[] = {

src/string_bytes.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ static size_t hex_decode(char* buf,
250250
const size_t srcLen) {
251251
size_t i;
252252
for (i = 0; i < len && i * 2 + 1 < srcLen; ++i) {
253-
unsigned a = unhex(src[i * 2 + 0]);
254-
unsigned b = unhex(src[i * 2 + 1]);
253+
unsigned a = unhex(static_cast<uint8_t>(src[i * 2 + 0]));
254+
unsigned b = unhex(static_cast<uint8_t>(src[i * 2 + 1]));
255255
if (!~a || !~b)
256256
return i;
257257
buf[i] = (a << 4) | b;

src/udp_wrap.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) {
531531
wrap->current_send_has_callback_ =
532532
sendto ? args[5]->IsTrue() : args[3]->IsTrue();
533533

534-
err = wrap->Send(*bufs, count, addr);
534+
err = static_cast<int>(wrap->Send(*bufs, count, addr));
535535

536536
wrap->current_send_req_wrap_.Clear();
537537
wrap->current_send_has_callback_ = false;
@@ -705,7 +705,7 @@ void UDPWrap::OnRecv(ssize_t nread,
705705
Context::Scope context_scope(env->context());
706706

707707
Local<Value> argv[] = {
708-
Integer::New(env->isolate(), nread),
708+
Integer::New(env->isolate(), static_cast<int32_t>(nread)),
709709
object(),
710710
Undefined(env->isolate()),
711711
Undefined(env->isolate())

0 commit comments

Comments
 (0)