From 8688c7a9bcf101fd5675312144461179a619eec0 Mon Sep 17 00:00:00 2001 From: Thorsten Otto Date: Thu, 14 Mar 2024 14:53:00 +0100 Subject: [PATCH] Avoid use of ftime() and fix overflow of unsigned int ftime() is an obsolete interface, and may not be present on all platforms. Also, multiplying a time_t by 1000 will overflow the range of an unsigned 32-bit int. --- common/connect.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/common/connect.cpp b/common/connect.cpp index 13b3386d..191336b3 100644 --- a/common/connect.cpp +++ b/common/connect.cpp @@ -44,7 +44,7 @@ #include //#include -#include +#include #include #include "connect.h" @@ -227,6 +227,8 @@ int ConnectionClass::Send_Packet(void* buf, int buflen, int ack_req) SwapCommHeaderType((CommHeaderType*)PacketBuf); + SwapCommHeaderType((CommHeaderType*)PacketBuf); + /*------------------------------------------------------------------------ Now build the packet ------------------------------------------------------------------------*/ @@ -747,7 +749,6 @@ int ConnectionClass::Service_Receive_Queue(void) *=========================================================================*/ unsigned int ConnectionClass::Time(void) { - static struct timeb mytime; // DOS time unsigned int msec; #ifdef WWLIB32_H @@ -764,23 +765,19 @@ unsigned int ConnectionClass::Time(void) /*------------------------------------------------------------------------ Otherwise, use the DOS timer ------------------------------------------------------------------------*/ - else { - ftime(&mytime); - msec = (unsigned int)mytime.time * 1000 + (unsigned int)mytime.millitm; - return ((msec / 100) * 6); - } #else /*------------------------------------------------------------------------ If the Westwood library isn't being used, use the DOS timer. ------------------------------------------------------------------------*/ - ftime(&mytime); - msec = (unsigned int)mytime.time * 1000 + (unsigned int)mytime.millitm; - return ((msec / 100) * 6); - #endif + static auto epoch = std::chrono::steady_clock::now().time_since_epoch(); + auto now = std::chrono::steady_clock::now().time_since_epoch(); + msec = unsigned(std::chrono::duration_cast(now - epoch).count()); + + return ((msec / 100) * 6); } /* end of Time */ /***************************************************************************