From af339a7a7a70a4f1c300eef70e1eb838ac93117a Mon Sep 17 00:00:00 2001 From: maksqwe Date: Fri, 23 Aug 2013 00:30:37 +0300 Subject: [PATCH 1/2] 1. "wcslen" function was called multiple times with "const wchar_t* Path" 2. fix mistake which led to undefined behavior "uint64(High<<32)" -> "uint64(High)<<32" 3. optimize std::vector<>::iterator's usage "iter++" -> "++iter". See preincrement and postincrement operator++() --- src/EbmlMaster.cpp | 6 +++--- src/platform/win32/WinIOCallback.cpp | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/EbmlMaster.cpp b/src/EbmlMaster.cpp index ea2d63a..5735ac9 100644 --- a/src/EbmlMaster.cpp +++ b/src/EbmlMaster.cpp @@ -65,7 +65,7 @@ EbmlMaster::EbmlMaster(const EbmlMaster & ElementToClone) while (Itr != ElementToClone.ElementList.end()) { *myItr = (*Itr)->Clone(); - Itr++; myItr++; + ++Itr; ++myItr; } } @@ -524,7 +524,7 @@ bool EbmlMaster::InsertElement(EbmlElement & element, size_t position) std::vector::iterator Itr = ElementList.begin(); while (Itr != ElementList.end() && position--) { - Itr++; + ++Itr; } if ((Itr == ElementList.end()) && position) return false; @@ -538,7 +538,7 @@ bool EbmlMaster::InsertElement(EbmlElement & element, const EbmlElement & before std::vector::iterator Itr = ElementList.begin(); while (Itr != ElementList.end() && *Itr != &before) { - Itr++; + ++Itr; } if (Itr == ElementList.end()) return false; diff --git a/src/platform/win32/WinIOCallback.cpp b/src/platform/win32/WinIOCallback.cpp index 9ac1190..bf592d6 100644 --- a/src/platform/win32/WinIOCallback.cpp +++ b/src/platform/win32/WinIOCallback.cpp @@ -153,10 +153,11 @@ bool WinIOCallback::open(const wchar_t* Path, const open_mode aMode, DWORD dwFla mFile = CreateFileW(Path, AccessMode, ShareMode, NULL, Disposition, dwFlags, NULL); } else { int errCode; - unsigned int bufferSize = wcslen(Path) + sizeof(wchar_t) * 2; + int pathSize = wcslen(Path); + unsigned int bufferSize = pathSize + sizeof(wchar_t) * 2; std::string PathA; PathA.resize(bufferSize); - errCode = WideCharToMultiByte(CP_ACP, 0, Path, wcslen(Path), (char *)PathA.c_str(), bufferSize, NULL, NULL); + errCode = WideCharToMultiByte(CP_ACP, 0, Path, pathSize, (char *)PathA.c_str(), bufferSize, NULL, NULL); if (errCode == 0) errCode = GetLastError(); #ifdef _DEBUG @@ -168,7 +169,7 @@ bool WinIOCallback::open(const wchar_t* Path, const open_mode aMode, DWORD dwFla // Increase the buffer size bufferSize += MAX_PATH; PathA.resize(bufferSize); - errCode = WideCharToMultiByte(CP_ACP, WC_SEPCHARS, Path, wcslen(Path), (char *)PathA.c_str(), bufferSize, NULL, NULL); + errCode = WideCharToMultiByte(CP_ACP, WC_SEPCHARS, Path, pathSize, (char *)PathA.c_str(), bufferSize, NULL, NULL); if (errCode == 0) errCode = GetLastError(); } @@ -251,7 +252,7 @@ void WinIOCallback::setFilePointer(int64 Offset, seek_mode Mode) } else { - mCurrentPosition |= uint64(High<<32); + mCurrentPosition |= uint64(High)<<32; } } From f0cf0dcbece5b412252b02a5dcdee76c8f58680f Mon Sep 17 00:00:00 2001 From: maksqwe Date: Fri, 23 Aug 2013 21:58:04 +0300 Subject: [PATCH 2/2] fix indentation --- src/platform/win32/WinIOCallback.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/win32/WinIOCallback.cpp b/src/platform/win32/WinIOCallback.cpp index bf592d6..9b7eda2 100644 --- a/src/platform/win32/WinIOCallback.cpp +++ b/src/platform/win32/WinIOCallback.cpp @@ -153,7 +153,7 @@ bool WinIOCallback::open(const wchar_t* Path, const open_mode aMode, DWORD dwFla mFile = CreateFileW(Path, AccessMode, ShareMode, NULL, Disposition, dwFlags, NULL); } else { int errCode; - int pathSize = wcslen(Path); + int pathSize = wcslen(Path); unsigned int bufferSize = pathSize + sizeof(wchar_t) * 2; std::string PathA; PathA.resize(bufferSize);