From dd9bb892cb977b9c7767c646cacb64d7b8f80dc3 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 6 Nov 2015 12:18:44 -0500 Subject: [PATCH 1/3] [Common] MaxFileSize: Use size_t for memory sizes. --- Source/Common/Log Class.h | 12 ++++++------ Source/Common/Trace.cpp | 13 +++++-------- Source/Common/Trace.h | 2 +- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Source/Common/Log Class.h b/Source/Common/Log Class.h index 69d322cb1..1b155da5d 100644 --- a/Source/Common/Log Class.h +++ b/Source/Common/Log Class.h @@ -15,8 +15,8 @@ class CLog bool m_FlushOnWrite; stdstr m_FileName; bool m_TruncateFileLog; - uint32_t m_MaxFileSize; - uint32_t m_FileChangeSize; + size_t m_MaxFileSize; + size_t m_FileChangeSize; public: CLog ( void ); @@ -29,10 +29,10 @@ public: bool Empty ( void ); void Close ( void ); - inline void SetMaxFileSize ( uint32_t Size ) - { - m_MaxFileSize = Size; - m_FileChangeSize = (uint32_t)(Size * 0.1); + inline void SetMaxFileSize(size_t Size) + { + m_MaxFileSize = Size; + m_FileChangeSize = (size_t)(Size * 0.1); } inline void SetTruncateFile( bool Truncate ) { m_TruncateFileLog = Truncate; } inline void SetFlush ( bool Always ) { m_FlushOnWrite = Always; } diff --git a/Source/Common/Trace.cpp b/Source/Common/Trace.cpp index 313d660f2..e74519fe8 100644 --- a/Source/Common/Trace.cpp +++ b/Source/Common/Trace.cpp @@ -191,7 +191,7 @@ m_FlushFile(FlushFile) m_hLogFile.Open(FileName, Log_Append); } -CTraceFileLog::CTraceFileLog(LPCTSTR FileName, bool FlushFile, LOG_OPEN_MODE eMode, uint32_t dwMaxFileSize) : +CTraceFileLog::CTraceFileLog(LPCTSTR FileName, bool FlushFile, LOG_OPEN_MODE eMode, size_t dwMaxFileSize) : m_FlushFile(FlushFile) { enum { MB = 1024 * 1024 }; @@ -199,14 +199,11 @@ m_FlushFile(FlushFile) m_hLogFile.SetFlush(false); m_hLogFile.SetTruncateFile(true); - if (dwMaxFileSize < 2048 && dwMaxFileSize > 2) - { - m_hLogFile.SetMaxFileSize(dwMaxFileSize * MB); - } - else - { - m_hLogFile.SetMaxFileSize(5 * MB); + if (dwMaxFileSize < 3 || dwMaxFileSize > 2047) + { /* Clamp file size to 5 MB if it exceeds 2047 or falls short of 3. */ + dwMaxFileSize = 5; } + m_hLogFile.SetMaxFileSize(dwMaxFileSize * MB); m_hLogFile.Open(FileName, eMode); } diff --git a/Source/Common/Trace.h b/Source/Common/Trace.h index d3b170633..2e8e2f0cd 100644 --- a/Source/Common/Trace.h +++ b/Source/Common/Trace.h @@ -26,7 +26,7 @@ class CTraceFileLog : public CTraceModule public: CTraceFileLog (const char * FileName, bool FlushFile = true); - CTraceFileLog (const char * FileName, bool FlushFile, LOG_OPEN_MODE eMode, uint32_t dwMaxFileSize = 5); + CTraceFileLog(const char * FileName, bool FlushFile, LOG_OPEN_MODE eMode, size_t dwMaxFileSize = 5); virtual ~CTraceFileLog (); void Write ( const char * Message, bool EndOfLine ); From a3cac34c74c5e7dc9b6d89e823bfc41b294214ed Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 6 Nov 2015 13:02:45 -0500 Subject: [PATCH 2/3] [Common] CFile::Write(): Use standard memory size input type. --- Source/Common/File Class.cpp | 10 +++++++--- Source/Common/File Class.h | 4 ++-- Source/Common/Log Class.cpp | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Source/Common/File Class.cpp b/Source/Common/File Class.cpp index 2e7a25d69..39166f792 100644 --- a/Source/Common/File Class.cpp +++ b/Source/Common/File Class.cpp @@ -140,15 +140,19 @@ bool CFile::Flush() return ::FlushFileBuffers(m_hFile) != 0; } -bool CFile::Write(const void* lpBuf, uint32_t nCount) +bool CFile::Write(const void* lpBuf, size_t nCount) { if (nCount == 0) { return true; // avoid Win32 "null-write" option } + if (nCount > ULONG_MAX) + { + nCount = ULONG_MAX; /* Or should we loop WriteFile() every 2 GB? */ + } - ULONG nWritten = 0; - if (!::WriteFile(m_hFile, lpBuf, nCount, &nWritten, NULL)) + DWORD nWritten = 0; + if (!::WriteFile(m_hFile, lpBuf, (DWORD)nCount, &nWritten, NULL)) { return false; } diff --git a/Source/Common/File Class.h b/Source/Common/File Class.h index fc734f633..b47709da0 100644 --- a/Source/Common/File Class.h +++ b/Source/Common/File Class.h @@ -41,7 +41,7 @@ public: virtual uint32_t GetLength() const = 0; virtual uint32_t Read(void* lpBuf, uint32_t nCount) = 0; - virtual bool Write(const void* lpBuf, uint32_t nCount) = 0; + virtual bool Write(const void* lpBuf, size_t nCount) = 0; virtual bool Flush() = 0; virtual bool Close() = 0; @@ -79,7 +79,7 @@ public: virtual uint32_t GetLength() const; virtual uint32_t Read(void* lpBuf, uint32_t nCount); - virtual bool Write(const void* lpBuf, uint32_t nCount); + virtual bool Write(const void* lpBuf, size_t nCount); virtual bool Flush(); virtual bool Close(); diff --git a/Source/Common/Log Class.cpp b/Source/Common/Log Class.cpp index 95f001dba..4eebfc35a 100644 --- a/Source/Common/Log Class.cpp +++ b/Source/Common/Log Class.cpp @@ -120,7 +120,7 @@ void CLog::LogArgs(const char * Message, va_list & args ) void CLog::Log( const char * Message ) { if (!m_hLogFile.IsOpen()) { return; } - m_hLogFile.Write(Message,(uint32_t)strlen(Message)*sizeof(TCHAR)); + m_hLogFile.Write(Message, strlen(Message)*sizeof(TCHAR)); if (m_FlushOnWrite) { m_hLogFile.Flush(); From 258c540543d1de64cf25becc450ae514c54450d0 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 6 Nov 2015 13:23:51 -0500 Subject: [PATCH 3/3] [Common] CFile::Read(): Use standard memory size input type. --- Source/Common/File Class.cpp | 10 +++++++--- Source/Common/File Class.h | 4 ++-- Source/Common/Log Class.cpp | 15 ++++++++------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Source/Common/File Class.cpp b/Source/Common/File Class.cpp index 39166f792..48acd893a 100644 --- a/Source/Common/File Class.cpp +++ b/Source/Common/File Class.cpp @@ -165,19 +165,23 @@ bool CFile::Write(const void* lpBuf, size_t nCount) return true; } -uint32_t CFile::Read(void* lpBuf, uint32_t nCount) +size_t CFile::Read(void* lpBuf, size_t nCount) { if (nCount == 0) { return 0; // avoid Win32 "null-read" } + if (nCount > ULONG_MAX) + { + nCount = ULONG_MAX; /* Or should we loop ReadFile() every 2 GB? */ + } DWORD dwRead = 0; - if (!::ReadFile(m_hFile, lpBuf, nCount, &dwRead, NULL)) + if (!::ReadFile(m_hFile, lpBuf, (DWORD)nCount, &dwRead, NULL)) { return 0; } - return (uint32_t)dwRead; + return (dwRead); } long CFile::Seek(long lOff, SeekPosition nFrom) diff --git a/Source/Common/File Class.h b/Source/Common/File Class.h index b47709da0..c61730450 100644 --- a/Source/Common/File Class.h +++ b/Source/Common/File Class.h @@ -40,7 +40,7 @@ public: virtual bool SetLength(uint32_t dwNewLen) = 0; virtual uint32_t GetLength() const = 0; - virtual uint32_t Read(void* lpBuf, uint32_t nCount) = 0; + virtual size_t Read(void* lpBuf, size_t nCount) = 0; virtual bool Write(const void* lpBuf, size_t nCount) = 0; virtual bool Flush() = 0; @@ -78,7 +78,7 @@ public: virtual bool SetLength(uint32_t dwNewLen); virtual uint32_t GetLength() const; - virtual uint32_t Read(void* lpBuf, uint32_t nCount); + virtual size_t Read(void* lpBuf, size_t nCount); virtual bool Write(const void* lpBuf, size_t nCount); virtual bool Flush(); diff --git a/Source/Common/Log Class.cpp b/Source/Common/Log Class.cpp index 4eebfc35a..a0a9560fd 100644 --- a/Source/Common/Log Class.cpp +++ b/Source/Common/Log Class.cpp @@ -145,13 +145,13 @@ void CLog::Log( const char * Message ) m_hLogFile.Seek((end - m_MaxFileSize) + m_FileChangeSize,CFile::begin); // Find next end of line - uint32_t NextEnter = 0, dwRead = 0; + size_t NextEnter = 0, dwRead = 0; do { BYTE Data[300]; - uint32_t dwRead; + size_t dwRead; - dwRead = m_hLogFile.Read(Data,sizeof(Data)); + dwRead = m_hLogFile.Read(Data, sizeof(Data)); if (dwRead == 0) { break; @@ -170,8 +170,9 @@ void CLog::Log( const char * Message ) } while(dwRead != 0); // copy content of log to the new file - uint32_t ReadPos = (end - m_MaxFileSize) + m_FileChangeSize + NextEnter; - uint32_t SizeToRead, WritePos = 0; + size_t ReadPos = (end - m_MaxFileSize) + m_FileChangeSize + NextEnter; + uint32_t WritePos = 0; + size_t SizeToRead; do { enum { fIS_MvSize = 0x5000 }; @@ -182,8 +183,8 @@ void CLog::Log( const char * Message ) m_hLogFile.Seek(ReadPos,CFile::begin); - uint32_t dwRead; - dwRead = m_hLogFile.Read(Data,SizeToRead); + size_t dwRead; + dwRead = m_hLogFile.Read(Data, SizeToRead); m_hLogFile.Seek(WritePos,CFile::begin);