Skip to content

Commit e622457

Browse files
committed
src: avoid implicit type conversions (take 2)
This fixes more C4244 MSVC warnings in the code base. Refs: #37149 PR-URL: #37334 Reviewed-By: James M Snell <[email protected]>
1 parent 11067fb commit e622457

12 files changed

+60
-58
lines changed

src/base64-inl.h

+15-19
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,17 @@ bool base64_decode_group_slow(char* const dst, const size_t dstlen,
3030
size_t* const i, size_t* const k) {
3131
uint8_t hi;
3232
uint8_t lo;
33-
#define V(expr) \
34-
for (;;) { \
35-
const uint8_t c = src[*i]; \
36-
lo = unbase64(c); \
37-
*i += 1; \
38-
if (lo < 64) \
39-
break; /* Legal character. */ \
40-
if (c == '=' || *i >= srclen) \
41-
return false; /* Stop decoding. */ \
42-
} \
43-
expr; \
44-
if (*i >= srclen) \
45-
return false; \
46-
if (*k >= dstlen) \
47-
return false; \
33+
#define V(expr) \
34+
for (;;) { \
35+
const uint8_t c = static_cast<uint8_t>(src[*i]); \
36+
lo = unbase64(c); \
37+
*i += 1; \
38+
if (lo < 64) break; /* Legal character. */ \
39+
if (c == '=' || *i >= srclen) return false; /* Stop decoding. */ \
40+
} \
41+
expr; \
42+
if (*i >= srclen) return false; \
43+
if (*k >= dstlen) return false; \
4844
hi = lo;
4945
V(/* Nothing. */);
5046
V(dst[(*k)++] = ((hi & 0x3F) << 2) | ((lo & 0x30) >> 4));
@@ -66,10 +62,10 @@ size_t base64_decode_fast(char* const dst, const size_t dstlen,
6662
size_t k = 0;
6763
while (i < max_i && k < max_k) {
6864
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])),
65+
static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 0]))),
66+
static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 1]))),
67+
static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 2]))),
68+
static_cast<unsigned char>(unbase64(static_cast<uint8_t>(src[i + 3]))),
7369
};
7470

7571
const uint32_t v = ReadUint32BE(txt);

src/inspector/worker_inspector.cc

+15-12
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,22 +42,25 @@ 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

56-
5756
ParentInspectorHandle::ParentInspectorHandle(
58-
int id, const std::string& url,
59-
std::shared_ptr<MainThreadHandle> parent_thread, bool wait_for_connect)
60-
: id_(id), url_(url), parent_thread_(parent_thread),
57+
uint64_t id,
58+
const std::string& url,
59+
std::shared_ptr<MainThreadHandle> parent_thread,
60+
bool wait_for_connect)
61+
: id_(id),
62+
url_(url),
63+
parent_thread_(parent_thread),
6164
wait_(wait_for_connect) {}
6265

6366
ParentInspectorHandle::~ParentInspectorHandle() {
@@ -78,11 +81,11 @@ std::unique_ptr<inspector::InspectorSession> ParentInspectorHandle::Connect(
7881
return parent_thread_->Connect(std::move(delegate), prevent_shutdown);
7982
}
8083

81-
void WorkerManager::WorkerFinished(int session_id) {
84+
void WorkerManager::WorkerFinished(uint64_t session_id) {
8285
children_.erase(session_id);
8386
}
8487

85-
void WorkerManager::WorkerStarted(int session_id,
88+
void WorkerManager::WorkerStarted(uint64_t session_id,
8689
const WorkerInfo& info,
8790
bool waiting) {
8891
if (info.worker_thread->Expired())
@@ -93,8 +96,8 @@ void WorkerManager::WorkerStarted(int session_id,
9396
}
9497
}
9598

96-
std::unique_ptr<ParentInspectorHandle>
97-
WorkerManager::NewParentHandle(int thread_id, const std::string& url) {
99+
std::unique_ptr<ParentInspectorHandle> WorkerManager::NewParentHandle(
100+
uint64_t thread_id, const std::string& url) {
98101
bool wait = !delegates_waiting_on_start_.empty();
99102
return std::make_unique<ParentInspectorHandle>(thread_id, url, thread_, wait);
100103
}

src/inspector/worker_inspector.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ struct WorkerInfo {
5353

5454
class ParentInspectorHandle {
5555
public:
56-
ParentInspectorHandle(int id, const std::string& url,
56+
ParentInspectorHandle(uint64_t id,
57+
const std::string& url,
5758
std::shared_ptr<MainThreadHandle> parent_thread,
5859
bool wait_for_connect);
5960
~ParentInspectorHandle();
6061
std::unique_ptr<ParentInspectorHandle> NewParentInspectorHandle(
61-
int thread_id, const std::string& url) {
62+
uint64_t thread_id, const std::string& url) {
6263
return std::make_unique<ParentInspectorHandle>(thread_id,
6364
url,
6465
parent_thread_,
@@ -75,7 +76,7 @@ class ParentInspectorHandle {
7576
bool prevent_shutdown);
7677

7778
private:
78-
int id_;
79+
uint64_t id_;
7980
std::string url_;
8081
std::shared_ptr<MainThreadHandle> parent_thread_;
8182
bool wait_;
@@ -87,9 +88,9 @@ class WorkerManager : public std::enable_shared_from_this<WorkerManager> {
8788
: thread_(thread) {}
8889

8990
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);
91+
uint64_t thread_id, const std::string& url);
92+
void WorkerStarted(uint64_t session_id, const WorkerInfo& info, bool waiting);
93+
void WorkerFinished(uint64_t session_id);
9394
std::unique_ptr<WorkerManagerEventHandle> SetAutoAttach(
9495
std::unique_ptr<WorkerDelegate> attach_delegate);
9596
void SetWaitOnStartForDelegate(int id, bool wait);
@@ -100,7 +101,7 @@ class WorkerManager : public std::enable_shared_from_this<WorkerManager> {
100101

101102
private:
102103
std::shared_ptr<MainThreadHandle> thread_;
103-
std::unordered_map<int, WorkerInfo> children_;
104+
std::unordered_map<uint64_t, WorkerInfo> children_;
104105
std::unordered_map<int, std::unique_ptr<WorkerDelegate>> delegates_;
105106
// If any one needs it, workers stop for all
106107
std::unordered_set<int> delegates_waiting_on_start_;

src/inspector_agent.cc

+2-2
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

@@ -919,7 +919,7 @@ void Agent::SetParentHandle(
919919
}
920920

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

src/inspector_agent.h

+1-1
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

+1-1
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

+5-4
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ void URLHost::ParseIPv6Host(const char* input, size_t length) {
893893
}
894894

895895
if (compress_pointer != nullptr) {
896-
unsigned swaps = piece_pointer - compress_pointer;
896+
int64_t swaps = piece_pointer - compress_pointer;
897897
piece_pointer = buffer_end - 1;
898898
while (piece_pointer != &value_.ipv6[0] && swaps > 0) {
899899
uint16_t temp = *piece_pointer;
@@ -960,7 +960,7 @@ void URLHost::ParseIPv4Host(const char* input, size_t length, bool* is_ipv4) {
960960

961961
while (pointer <= end) {
962962
const char ch = pointer < end ? pointer[0] : kEOL;
963-
int remaining = end - pointer - 1;
963+
int64_t remaining = end - pointer - 1;
964964
if (ch == '.' || ch == kEOL) {
965965
if (++parts > static_cast<int>(arraysize(numbers)))
966966
return;
@@ -993,10 +993,11 @@ void URLHost::ParseIPv4Host(const char* input, size_t length, bool* is_ipv4) {
993993
}
994994

995995
type_ = HostType::H_IPV4;
996-
val = numbers[parts - 1];
996+
val = static_cast<uint32_t>(numbers[parts - 1]);
997997
for (int n = 0; n < parts - 1; n++) {
998998
double b = 3 - n;
999-
val += numbers[n] * pow(256, b);
999+
val +=
1000+
static_cast<uint32_t>(numbers[n]) * static_cast<uint32_t>(pow(256, b));
10001001
}
10011002

10021003
value_.ipv4 = val;

src/node_wasi.cc

+4-2
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class SignalWrap : public HandleWrap {
159159

160160
void DecreaseSignalHandlerCount(int signum) {
161161
Mutex::ScopedLock lock(handled_signals_mutex);
162-
int new_handler_count = --handled_signals[signum];
162+
int64_t new_handler_count = --handled_signals[signum];
163163
CHECK_GE(new_handler_count, 0);
164164
if (new_handler_count == 0)
165165
handled_signals.erase(signum);

src/stream_base.cc

+1-1
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

+2-2
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

+5-6
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) {
541541
wrap->current_send_has_callback_ =
542542
sendto ? args[5]->IsTrue() : args[3]->IsTrue();
543543

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

546546
wrap->current_send_req_wrap_.Clear();
547547
wrap->current_send_has_callback_ = false;
@@ -715,11 +715,10 @@ void UDPWrap::OnRecv(ssize_t nread,
715715
Context::Scope context_scope(env->context());
716716

717717
Local<Value> argv[] = {
718-
Integer::New(env->isolate(), nread),
719-
object(),
720-
Undefined(env->isolate()),
721-
Undefined(env->isolate())
722-
};
718+
Integer::New(env->isolate(), static_cast<int32_t>(nread)),
719+
object(),
720+
Undefined(env->isolate()),
721+
Undefined(env->isolate())};
723722

724723
if (nread < 0) {
725724
MakeCallback(env->onmessage_string(), arraysize(argv), argv);

0 commit comments

Comments
 (0)