Skip to content

Commit ce503b3

Browse files
zbjornsonreconbot
authored andcommitted
fix: use correct casts to/from HANDLE/int (#1766)
Silences a ton of warnings with MSVC
1 parent 9b5dbdb commit ce503b3

1 file changed

Lines changed: 29 additions & 25 deletions

File tree

packages/bindings/src/serialport_win.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
// Declare type of pointer to CancelIoEx function
2222
typedef BOOL (WINAPI *CancelIoExType)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
2323

24+
static inline HANDLE int2handle(int ptr) {
25+
return reinterpret_cast<HANDLE>(static_cast<uintptr_t>(ptr));
26+
}
27+
2428
std::list<int> g_closingHandles;
2529

2630
void ErrorCodeToString(const char* prefix, int errorCode, char *errorStr) {
@@ -185,7 +189,7 @@ void EIO_Open(uv_work_t* req) {
185189
PurgeComm(file, PURGE_RXCLEAR);
186190
PurgeComm(file, PURGE_TXCLEAR);
187191

188-
data->result = (int)file; // NOLINT
192+
data->result = static_cast<int>(reinterpret_cast<uintptr_t>(file));
189193
}
190194

191195
void EIO_Update(uv_work_t* req) {
@@ -195,14 +199,14 @@ void EIO_Update(uv_work_t* req) {
195199
SecureZeroMemory(&dcb, sizeof(DCB));
196200
dcb.DCBlength = sizeof(DCB);
197201

198-
if (!GetCommState((HANDLE)data->fd, &dcb)) {
202+
if (!GetCommState(int2handle(data->fd), &dcb)) {
199203
ErrorCodeToString("Update (GetCommState)", GetLastError(), data->errorString);
200204
return;
201205
}
202206

203207
dcb.BaudRate = data->baudRate;
204208

205-
if (!SetCommState((HANDLE)data->fd, &dcb)) {
209+
if (!SetCommState(int2handle(data->fd), &dcb)) {
206210
ErrorCodeToString("Update (SetCommState)", GetLastError(), data->errorString);
207211
return;
208212
}
@@ -212,26 +216,26 @@ void EIO_Set(uv_work_t* req) {
212216
SetBaton* data = static_cast<SetBaton*>(req->data);
213217

214218
if (data->rts) {
215-
EscapeCommFunction((HANDLE)data->fd, SETRTS);
219+
EscapeCommFunction(int2handle(data->fd), SETRTS);
216220
} else {
217-
EscapeCommFunction((HANDLE)data->fd, CLRRTS);
221+
EscapeCommFunction(int2handle(data->fd), CLRRTS);
218222
}
219223

220224
if (data->dtr) {
221-
EscapeCommFunction((HANDLE)data->fd, SETDTR);
225+
EscapeCommFunction(int2handle(data->fd), SETDTR);
222226
} else {
223-
EscapeCommFunction((HANDLE)data->fd, CLRDTR);
227+
EscapeCommFunction(int2handle(data->fd), CLRDTR);
224228
}
225229

226230
if (data->brk) {
227-
EscapeCommFunction((HANDLE)data->fd, SETBREAK);
231+
EscapeCommFunction(int2handle(data->fd), SETBREAK);
228232
} else {
229-
EscapeCommFunction((HANDLE)data->fd, CLRBREAK);
233+
EscapeCommFunction(int2handle(data->fd), CLRBREAK);
230234
}
231235

232236
DWORD bits = 0;
233237

234-
GetCommMask((HANDLE)data->fd, &bits);
238+
GetCommMask(int2handle(data->fd), &bits);
235239

236240
bits &= ~(EV_CTS | EV_DSR);
237241

@@ -243,7 +247,7 @@ void EIO_Set(uv_work_t* req) {
243247
bits |= EV_DSR;
244248
}
245249

246-
if (!SetCommMask((HANDLE)data->fd, bits)) {
250+
if (!SetCommMask(int2handle(data->fd), bits)) {
247251
ErrorCodeToString("Setting options on COM port (SetCommMask)", GetLastError(), data->errorString);
248252
return;
249253
}
@@ -253,7 +257,7 @@ void EIO_Get(uv_work_t* req) {
253257
GetBaton* data = static_cast<GetBaton*>(req->data);
254258

255259
DWORD bits = 0;
256-
if (!GetCommModemStatus((HANDLE)data->fd, &bits)) {
260+
if (!GetCommModemStatus(int2handle(data->fd), &bits)) {
257261
ErrorCodeToString("Getting control settings on COM port (GetCommModemStatus)", GetLastError(), data->errorString);
258262
return;
259263
}
@@ -270,7 +274,7 @@ void EIO_GetBaudRate(uv_work_t* req) {
270274
SecureZeroMemory(&dcb, sizeof(DCB));
271275
dcb.DCBlength = sizeof(DCB);
272276

273-
if (!GetCommState((HANDLE)data->fd, &dcb)) {
277+
if (!GetCommState(int2handle(data->fd), &dcb)) {
274278
ErrorCodeToString("Getting baud rate (GetCommState)", GetLastError(), data->errorString);
275279
return;
276280
}
@@ -331,7 +335,7 @@ NAN_METHOD(Write) {
331335
void __stdcall WriteIOCompletion(DWORD errorCode, DWORD bytesTransferred, OVERLAPPED* ov) {
332336
WriteBaton* baton = static_cast<WriteBaton*>(ov->hEvent);
333337
DWORD bytesWritten;
334-
if (!GetOverlappedResult((HANDLE)baton->fd, ov, &bytesWritten, TRUE)) {
338+
if (!GetOverlappedResult(int2handle(baton->fd), ov, &bytesWritten, TRUE)) {
335339
errorCode = GetLastError();
336340
ErrorCodeToString("Writing to COM port (GetOverlappedResult)", errorCode, baton->errorString);
337341
baton->complete = true;
@@ -357,7 +361,7 @@ DWORD __stdcall WriteThread(LPVOID param) {
357361
char* offsetPtr = baton->bufferData + baton->offset;
358362
// WriteFileEx requires calling GetLastError even upon success. Clear the error beforehand.
359363
SetLastError(0);
360-
WriteFileEx((HANDLE)baton->fd, offsetPtr,
364+
WriteFileEx(int2handle(baton->fd), offsetPtr,
361365
static_cast<DWORD>(baton->bufferLength - baton->offset), ov, WriteIOCompletion);
362366
// Error codes when call is successful, such as ERROR_MORE_DATA.
363367
DWORD lastError = GetLastError();
@@ -460,7 +464,7 @@ void __stdcall ReadIOCompletion(DWORD errorCode, DWORD bytesTransferred, OVERLAP
460464
}
461465

462466
DWORD lastError;
463-
if (!GetOverlappedResult((HANDLE)baton->fd, ov, &bytesTransferred, TRUE)) {
467+
if (!GetOverlappedResult(int2handle(baton->fd), ov, &bytesTransferred, TRUE)) {
464468
lastError = GetLastError();
465469
ErrorCodeToString("Reading from COM port (GetOverlappedResult)", lastError, baton->errorString);
466470
baton->complete = true;
@@ -477,7 +481,7 @@ void __stdcall ReadIOCompletion(DWORD errorCode, DWORD bytesTransferred, OVERLAP
477481
// return immediately no matter how much data is available.
478482
COMMTIMEOUTS commTimeouts = {};
479483
commTimeouts.ReadIntervalTimeout = MAXDWORD;
480-
if (!SetCommTimeouts((HANDLE)baton->fd, &commTimeouts)) {
484+
if (!SetCommTimeouts(int2handle(baton->fd), &commTimeouts)) {
481485
lastError = GetLastError();
482486
ErrorCodeToString("Setting COM timeout (SetCommTimeouts)", lastError, baton->errorString);
483487
baton->complete = true;
@@ -490,7 +494,7 @@ void __stdcall ReadIOCompletion(DWORD errorCode, DWORD bytesTransferred, OVERLAP
490494
// ReadFile, unlike ReadFileEx, needs an event in the overlapped structure.
491495
memset(ov, 0, sizeof(OVERLAPPED));
492496
ov->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
493-
if (!ReadFile((HANDLE)baton->fd, offsetPtr, baton->bytesToRead, &bytesTransferred, ov)) {
497+
if (!ReadFile(int2handle(baton->fd), offsetPtr, baton->bytesToRead, &bytesTransferred, ov)) {
494498
errorCode = GetLastError();
495499

496500
if (errorCode != ERROR_IO_PENDING) {
@@ -500,7 +504,7 @@ void __stdcall ReadIOCompletion(DWORD errorCode, DWORD bytesTransferred, OVERLAP
500504
return;
501505
}
502506

503-
if (!GetOverlappedResult((HANDLE)baton->fd, ov, &bytesTransferred, TRUE)) {
507+
if (!GetOverlappedResult(int2handle(baton->fd), ov, &bytesTransferred, TRUE)) {
504508
lastError = GetLastError();
505509
ErrorCodeToString("Reading from COM port (GetOverlappedResult)", lastError, baton->errorString);
506510
baton->complete = true;
@@ -528,7 +532,7 @@ DWORD __stdcall ReadThread(LPVOID param) {
528532
// Reset the read timeout to 0, so that it will block until more data arrives.
529533
COMMTIMEOUTS commTimeouts = {};
530534
commTimeouts.ReadIntervalTimeout = 0;
531-
if (!SetCommTimeouts((HANDLE)baton->fd, &commTimeouts)) {
535+
if (!SetCommTimeouts(int2handle(baton->fd), &commTimeouts)) {
532536
lastError = GetLastError();
533537
ErrorCodeToString("Setting COM timeout (SetCommTimeouts)", lastError, baton->errorString);
534538
break;
@@ -539,7 +543,7 @@ DWORD __stdcall ReadThread(LPVOID param) {
539543
// ReadFileEx requires calling GetLastError even upon success. Clear the error beforehand.
540544
SetLastError(0);
541545
// Only read 1 byte, so that the callback will be triggered once any data arrives.
542-
ReadFileEx((HANDLE)baton->fd, offsetPtr, 1, ov, ReadIOCompletion);
546+
ReadFileEx(int2handle(baton->fd), offsetPtr, 1, ov, ReadIOCompletion);
543547
// Error codes when call is successful, such as ERROR_MORE_DATA.
544548
lastError = GetLastError();
545549
if (lastError != ERROR_SUCCESS) {
@@ -587,9 +591,9 @@ void EIO_Close(uv_work_t* req) {
587591
if (pCancelIoEx) {
588592
// Function exists so call it
589593
// Cancel all pending IO Requests for the current device
590-
pCancelIoEx((HANDLE)data->fd, NULL);
594+
pCancelIoEx(int2handle(data->fd), NULL);
591595
}
592-
if (!CloseHandle((HANDLE)data->fd)) {
596+
if (!CloseHandle(int2handle(data->fd))) {
593597
ErrorCodeToString("Closing connection (CloseHandle)", GetLastError(), data->errorString);
594598
return;
595599
}
@@ -932,7 +936,7 @@ void EIO_Flush(uv_work_t* req) {
932936
VoidBaton* data = static_cast<VoidBaton*>(req->data);
933937

934938
DWORD purge_all = PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR;
935-
if (!PurgeComm((HANDLE)data->fd, purge_all)) {
939+
if (!PurgeComm(int2handle(data->fd), purge_all)) {
936940
ErrorCodeToString("Flushing connection (PurgeComm)", GetLastError(), data->errorString);
937941
return;
938942
}
@@ -941,7 +945,7 @@ void EIO_Flush(uv_work_t* req) {
941945
void EIO_Drain(uv_work_t* req) {
942946
VoidBaton* data = static_cast<VoidBaton*>(req->data);
943947

944-
if (!FlushFileBuffers((HANDLE)data->fd)) {
948+
if (!FlushFileBuffers(int2handle(data->fd))) {
945949
ErrorCodeToString("Draining connection (FlushFileBuffers)", GetLastError(), data->errorString);
946950
return;
947951
}

0 commit comments

Comments
 (0)