From d62c98527a8634774ae49fbe3ff8f3776d5d7cd0 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Mon, 19 Aug 2013 01:25:53 -0700 Subject: [PATCH] Invalid usage of cv::Ptr for arrays fixed. --- modules/contrib/src/inputoutput.cpp | 37 ++++++++++++++--------------- modules/core/src/glob.cpp | 15 ++++++------ modules/core/src/system.cpp | 11 ++++----- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/modules/contrib/src/inputoutput.cpp b/modules/contrib/src/inputoutput.cpp index e04740faee..d6d514f5b8 100644 --- a/modules/contrib/src/inputoutput.cpp +++ b/modules/contrib/src/inputoutput.cpp @@ -11,7 +11,7 @@ namespace cv { - std::vector Directory::GetListFiles( const std::string& path, const std::string & exten, bool addPath ) + std::vector Directory::GetListFiles( const std::string& path, const std::string & exten, bool addPath ) { std::vector list; list.clear(); @@ -25,10 +25,9 @@ namespace cv HANDLE hFind; #ifdef HAVE_WINRT - size_t size = mbstowcs(NULL, path_f.c_str(), path_f.size()); - Ptr wpath = new wchar_t[size+1]; - wpath[size] = 0; - mbstowcs(wpath, path_f.c_str(), path_f.size()); + wchar_t wpath[MAX_PATH]; + size_t copied = mbstowcs(wpath, path_f.c_str(), MAX_PATH); + CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1)); hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0); #else hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData); @@ -47,12 +46,12 @@ namespace cv FindFileData.dwFileAttributes == FILE_ATTRIBUTE_SYSTEM || FindFileData.dwFileAttributes == FILE_ATTRIBUTE_READONLY) { - cv::Ptr fname; + char* fname; #ifdef HAVE_WINRT - size_t asize = wcstombs(NULL, FindFileData.cFileName, 0); - fname = new char[asize+1]; - fname[asize] = 0; - wcstombs(fname, FindFileData.cFileName, asize); + char fname_tmp[MAX_PATH] = {0}; + size_t copied = wcstombs(fname_tmp, FindFileData.cFileName, MAX_PATH); + CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1)); + fname = fname_tmp; #else fname = FindFileData.cFileName; #endif @@ -109,10 +108,10 @@ namespace cv HANDLE hFind; #ifdef HAVE_WINRT - size_t size = mbstowcs(NULL, path_f.c_str(), path_f.size()); - Ptr wpath = new wchar_t[size+1]; - wpath[size] = 0; - mbstowcs(wpath, path_f.c_str(), path_f.size()); + wchar_t wpath [MAX_PATH]; + size_t copied = mbstowcs(wpath, path_f.c_str(), path_f.size()); + CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1)); + hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0); #else hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData); @@ -135,12 +134,12 @@ namespace cv strcmp(FindFileData.cFileName, "..") != 0) #endif { - cv::Ptr fname; + char* fname; #ifdef HAVE_WINRT - size_t asize = wcstombs(NULL, FindFileData.cFileName, 0); - fname = new char[asize+1]; - fname[asize] = 0; - wcstombs(fname, FindFileData.cFileName, asize); + char fname_tmp[MAX_PATH]; + size_t copied = wcstombs(fname, FindFileData.cFileName, MAX_PATH); + CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1)); + fname = fname_tmp; #else fname = FindFileData.cFileName; #endif diff --git a/modules/core/src/glob.cpp b/modules/core/src/glob.cpp index e39cba0163..208b4e05cf 100644 --- a/modules/core/src/glob.cpp +++ b/modules/core/src/glob.cpp @@ -79,10 +79,9 @@ namespace dir->ent.d_name = 0; #ifdef HAVE_WINRT cv::String full_path = cv::String(path) + "\\*"; - size_t size = mbstowcs(NULL, full_path.c_str(), full_path.size()); - cv::Ptr wfull_path = new wchar_t[size+1]; - wfull_path[size] = 0; - mbstowcs(wfull_path, full_path.c_str(), full_path.size()); + wchar_t wfull_path[MAX_PATH]; + size_t copied = mbstowcs(wfull_path, full_path.c_str(), MAX_PATH); + CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1)); dir->handle = ::FindFirstFileExW(wfull_path, FindExInfoStandard, &dir->data, FindExSearchNameMatch, NULL, 0); #else @@ -106,6 +105,7 @@ namespace return 0; } size_t asize = wcstombs(NULL, dir->data.cFileName, 0); + CV_Assert((asize != 0) && (asize != (size_t)-1)); char* aname = new char[asize+1]; aname[asize] = 0; wcstombs(aname, dir->data.cFileName, asize); @@ -146,10 +146,9 @@ static bool isDir(const cv::String& path, DIR* dir) { WIN32_FILE_ATTRIBUTE_DATA all_attrs; #ifdef HAVE_WINRT - size_t size = mbstowcs(NULL, path.c_str(), path.size()); - cv::Ptr wpath = new wchar_t[size+1]; - wpath[size] = 0; - mbstowcs(wpath, path.c_str(), path.size()); + wchar_t wpath[MAX_PATH]; + size_t copied = mbstowcs(wpath, path.c_str(), MAX_PATH); + CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1)); ::GetFileAttributesExW(wpath, GetFileExInfoStandard, &all_attrs); #else ::GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &all_attrs); diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index 7e01ca5ea4..685b5b756d 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -411,15 +411,14 @@ string tempfile( const char* suffix ) temp_file = temp_dir + std::wstring(L"\\") + temp_file; DeleteFileW(temp_file.c_str()); - size_t asize = wcstombs(NULL, temp_file.c_str(), 0); - Ptr aname = new char[asize+1]; - aname[asize] = 0; - wcstombs(aname, temp_file.c_str(), asize); + char aname[MAX_PATH]; + size_t copied = wcstombs(aname, temp_file.c_str(), MAX_PATH); + CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1)); fname = std::string(aname); RoUninitialize(); #else - char temp_dir2[MAX_PATH + 1] = { 0 }; - char temp_file[MAX_PATH + 1] = { 0 }; + char temp_dir2[MAX_PATH] = { 0 }; + char temp_file[MAX_PATH] = { 0 }; if (temp_dir == 0 || temp_dir[0] == 0) {