OSDN Git Service

* Add _tcscpy_safe() to coretools.h
authorsdottaka <sdottaka@users.sourceforge.net>
Sun, 24 May 2015 11:44:42 +0000 (20:44 +0900)
committersdottaka <sdottaka@users.sourceforge.net>
Sun, 24 May 2015 11:44:42 +0000 (20:44 +0900)
* Use _tcscpy_safe() and StrintCchCopy() instead of _tcscpy() and _tcsncpy()

--HG--
branch : stable

Src/Common/RegKey.cpp
Src/Common/coretools.h
Src/Common/lwdisp.c
Src/Common/version.cpp
Src/Exceptions.h
Src/Plugins.cpp
Src/SourceControl.cpp

index be65d30..3dd4729 100644 (file)
@@ -9,6 +9,7 @@
 #include <windows.h>
 #include <cassert>
 #include <algorithm>
+#include <strsafe.h>
 #include "UnicodeString.h"
 #include "coretypes.h"
 
@@ -343,8 +344,5 @@ void CRegKeyEx::ReadChars (LPCTSTR pszKey, LPTSTR pData, DWORD dwLen, LPCTSTR de
        LONG ret = RegQueryValueEx (m_hKey, (LPTSTR) pszKey, NULL,
                &dwType, (LPBYTE)pData, &len);
        if (ret != ERROR_SUCCESS)
-       {
-               _tcsncpy(pData, defval, std::min((DWORD)_tcslen(defval), dwLen));
-               pData[std::min((DWORD)_tcslen(defval), dwLen-1)] = _T('\0');
-       }
+               StringCchCopy(pData, dwLen, defval);
 }
index 0c865ad..57d708b 100644 (file)
@@ -6,7 +6,15 @@
 #pragma once
 
 #include "UnicodeString.h"
+#include <strsafe.h>
 
 void replace_char(TCHAR *s, int target, int repl);
 
 size_t linelen(const char *string, size_t maxlen);
+
+template <typename T, size_t N>
+T *_tcscpy_safe(T(&dst)[N], const T *src)
+{
+       StringCchCopy(reinterpret_cast<T *>(&dst), N, src);
+       return reinterpret_cast<T *>(&dst);
+}
index 4bec1db..3d5d8d6 100644 (file)
@@ -52,6 +52,7 @@ struct _RPC_ASYNC_STATE;      // avoid MSC warning C4115
 #include <shlwapi.h>
 #include <tchar.h>
 #include <stdarg.h>
+#include <strsafe.h>
 #include "lwdisp.h"
 #include "dllproxy.h"
 
@@ -119,23 +120,23 @@ static LPTSTR FormatMessageFromString(LPCTSTR format, ...)
 static void mycpyt2w(LPCTSTR tsz, wchar_t * wdest, size_t limit)
 {
 #ifdef _UNICODE
-       wcsncpy(wdest, tsz, limit);
+       StringCchCopyW(wdest, limit, tsz);
 #else
        MultiByteToWideChar(CP_ACP, 0, tsz, -1, wdest, (int)limit);
-#endif
        // always terminate the string
        wdest[limit-1] = 0;
+#endif
 }
 
 static void mycpyt2a(LPCTSTR tsz, char * adest, size_t limit)
 {
 #ifdef _UNICODE
        WideCharToMultiByte(CP_ACP, 0, tsz, -1, adest, (int)limit, 0, 0);
-#else
-       strncpy(adest, tsz, limit);
-#endif
        // always terminate the string
        adest[limit-1] = 0;
+#else
+       StringCchCopyA(adest, list, tsz);
+#endif
 }
 
 #ifdef _WIN64
index 5917a80..d7daeb1 100644 (file)
@@ -9,6 +9,7 @@
 #include <cstdio>
 #include <tchar.h>
 #include <assert.h>
+#include <strsafe.h>
 #include "coretypes.h"
 #include "UnicodeString.h"
 
@@ -281,7 +282,7 @@ void CVersionInfo::GetVersionInfo()
        if (m_strFileName.empty())
                GetModuleFileName(NULL, szFileName, MAX_PATH);
        else
-               _tcsncpy(szFileName, m_strFileName.c_str(), MAX_PATH - 1);
+               StringCchCopy(szFileName, MAX_PATH, m_strFileName.c_str());
        
        DWORD dwVerInfoSize = GetFileVersionInfoSize(szFileName, &dwVerHnd);
        if (dwVerInfoSize)
index a3a357d..ce811be 100644 (file)
@@ -30,6 +30,7 @@
 #include <cstdio>
 #include <windows.h>
 #include <tchar.h>
+#include <strsafe.h>
 
 #endif
 
@@ -93,8 +94,7 @@ public:
        {
                static TCHAR message[512];
                _sntprintf(message, sizeof(message)/sizeof(message[0]), _T("Exception %s (0x%.8x)"), getSeMessage(), getSeNumber());
-               _tcsncpy(lpszError, message, nMaxError-1);
-               lpszError[nMaxError-1] = 0;
+               StringCchCopy(lpszError, nMaxError, message);
                return true;
        }
 #else
index 682babe..0b5129e 100644 (file)
@@ -45,6 +45,7 @@
 #include "paths.h"
 #include "Environment.h"
 #include "FileFilter.h"
+#include "coretools.h"
 
 using std::vector;
 using Poco::RegularExpression;
@@ -991,13 +992,13 @@ static HRESULT safeInvokeA(LPDISPATCH pi, VARIANT *ret, DISPID id, LPCCH op, ...
                // structured exception are catched here thanks to class SE_Exception
                if (!(e.GetErrorMessage(errorText, 500, NULL)))
                        // don't localize this as we do not localize the known exceptions
-                       _tcscpy(errorText, _T("Unknown CException"));
+                       _tcscpy_safe(errorText, _T("Unknown CException"));
                bExceptionCatched = true;
        }
        catch(...) 
        {
                // don't localize this as we do not localize the known exceptions
-               _tcscpy(errorText, _T("Unknown C++ exception"));
+               _tcscpy_safe(errorText, _T("Unknown C++ exception"));
                bExceptionCatched = true;
        }
 
@@ -1044,13 +1045,13 @@ static HRESULT safeInvokeW(LPDISPATCH pi, VARIANT *ret, LPCOLESTR silent, LPCCH
                // structured exception are catched here thanks to class SE_Exception
                if (!(e.GetErrorMessage(errorText, 500, NULL)))
                        // don't localize this as we do not localize the known exceptions
-                       _tcscpy(errorText, _T("Unknown CException"));
+                       _tcscpy_safe(errorText, _T("Unknown CException"));
                bExceptionCatched = true;
        }
        catch(...) 
        {
                // don't localize this as we do not localize the known exceptions
-               _tcscpy(errorText, _T("Unknown C++ exception"));
+               _tcscpy_safe(errorText, _T("Unknown C++ exception"));
                bExceptionCatched = true;
        }
 
index 526dca5..dc3c8b4 100644 (file)
@@ -20,6 +20,7 @@
 #include "ssapi.h"      // BSP - Includes for Visual Source Safe COM interface
 #include "CCPromptDlg.h"
 #include "VSSHelper.h"
+#include "coretools.h"
 
 using Poco::format;
 using Poco::Process;
@@ -231,8 +232,8 @@ BOOL CMergeApp::SaveToVersionControl(const String& strSavePath)
                        static TCHAR buffer1[nBufferSize];
                        static TCHAR buffer2[nBufferSize];
 
-                       _tcscpy(buffer1, strSavePath.c_str());
-                       _tcscpy(buffer2, m_pVssHelper->GetProjectBase().c_str());
+                       _tcscpy_safe(buffer1, strSavePath.c_str());
+                       _tcscpy_safe(buffer2, m_pVssHelper->GetProjectBase().c_str());
                        _tcslwr(buffer1);
                        _tcslwr(buffer2);
 
@@ -250,14 +251,14 @@ BOOL CMergeApp::SaveToVersionControl(const String& strSavePath)
                        {
                                int index  = (int)(pdest - buffer1 + 1);
                        
-                               _tcscpy(buffer, buffer1);
+                               _tcscpy_safe(buffer, buffer1);
                                TCHAR * fp = &buffer[int(index + _tcslen(pbuf2))];
                                sname = fp;
 
                                if (sname[0] == ':')
                                {
-                                       _tcscpy(buffer2, sname);
-                                       _tcscpy(buffer, (TCHAR*)&buffer2[2]);
+                                       _tcscpy_safe(buffer2, (LPCTSTR)sname);
+                                       _tcscpy_safe(buffer, (TCHAR*)&buffer2[2]);
                                        sname = buffer;
                                }
                        }