]> git.saurik.com Git - wxWidgets.git/commitdiff
add safe wxStrlcpy() function and replaced all wxStrncpy() calls by it
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 29 Nov 2008 14:41:02 +0000 (14:41 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 29 Nov 2008 14:41:02 +0000 (14:41 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57023 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

27 files changed:
include/wx/wxcrt.h
interface/wx/wxcrt.h
src/common/datetime.cpp
src/common/log.cpp
src/common/utilscmn.cpp
src/motif/clipbrd.cpp
src/msdos/utilsdos.cpp
src/msw/app.cpp
src/msw/crashrpt.cpp
src/msw/dialup.cpp
src/msw/filedlg.cpp
src/msw/font.cpp
src/msw/fontenum.cpp
src/msw/fontutil.cpp
src/msw/listctrl.cpp
src/msw/printdlg.cpp
src/msw/taskbar.cpp
src/msw/textctrl.cpp
src/msw/utils.cpp
src/msw/utilsgui.cpp
src/msw/window.cpp
src/os2/font.cpp
src/os2/fontenum.cpp
src/os2/fontutil.cpp
src/os2/utils.cpp
src/palmos/utils.cpp
src/unix/utilsunx.cpp

index 1549dc436b918a69a666a567f5804db2bc86a711..a0887da4aeb52ef681e1b3dee41afe2ac3d15ebf 100644 (file)
@@ -231,6 +231,36 @@ inline char *wxStrncpy(char *dest, const wchar_t *src, size_t n)
 inline wchar_t *wxStrncpy(wchar_t *dest, const char *src, size_t n)
     { return wxCRT_StrncpyW(dest, wxConvLibc.cMB2WC(src), n); }
 
+// this is a new function so we don't care about backwards compatibility and
+// so don't need to support wchar_t/char overloads
+inline size_t wxStrlcpy(char *dest, const char *src, size_t n)
+{
+    const size_t len = wxCRT_StrlenA(src);
+
+    if ( n )
+    {
+        if ( n-- > len )
+            n = len;
+        wxCRT_StrncpyA(dest, src, n);
+        dest[n] = '\0';
+    }
+
+    return len;
+}
+inline size_t wxStrlcpy(wchar_t *dest, const wchar_t *src, size_t n)
+{
+    const size_t len = wxCRT_StrlenW(src);
+    if ( n )
+    {
+        if ( n-- > len )
+            n = len;
+        wxCRT_StrncpyW(dest, src, n);
+        dest[n] = L'\0';
+    }
+
+    return len;
+}
+
 inline char *wxStrcat(char *dest, const char *src)
     { return wxCRT_StrcatA(dest, src); }
 inline wchar_t *wxStrcat(wchar_t *dest, const wchar_t *src)
index f30ed8982c7291c89f8aa3fa0c70d1509e2fb9cb..56aa19a3df18ebf057dcc1c9e02bd855d9f1bd7d 100644 (file)
@@ -86,6 +86,56 @@ wxArrayString wxStringTokenize(const wxString& string,
                                const wxString& delims = wxDEFAULT_DELIMITERS,
                                wxStringTokenizerMode mode = wxTOKEN_DEFAULT);
 
+/**
+    Safe and more convenient replacement for strncpy().
+
+    This function copies the source string @a src to the destination buffer @a
+    dst of size @a n without overflowing the buffer and ensuring that it is
+    always @NUL-terminated.
+
+    Example of use:
+    @code
+        char buf[256];
+        if ( wxStrlcpy(buf, GetSomeString(), WXSIZEOF(buf)) > WXSIZEOF(buf) )
+            ... handle truncation ...
+    @endcode
+    Notice that using wxStrncpy() in similar way is wrong, the above is broadly
+    equivalent to
+    @code
+        char buf[256];
+        buf[WXSIZEOF(buf) - 1] = '\0';
+        wxStrncpy(buf, GetSomeString(), WXSIZEOF(buf) - 1);
+        if ( buf[WXSIZEOF(buf) - 1] != '\0' )
+        {
+            ... truncation occurred ...
+            // need to NUL-terminate string manually
+            buf[WXSIZEOF(buf) - 1] = '\0';
+        }
+    @endcode
+    which should explain the advantage of using wxStrlcpy().
+
+    Notice that this function is similar to the OpenBSD strlcpy() function.
+
+    The template parameter @a T can be either @c char or @c wchar_t.
+
+    @param dst
+        Destination buffer of size (greater or) equal to @a n.
+    @param src
+        @NUL-terminated source string.
+    @param n
+        The size of the destination buffer.
+    @return
+        The length of @a src, if the returned value is greater or equal to @a n
+        then there was not enough space in the destination buffer and the
+        string was truncated.
+
+    @since{2.9.0}
+
+    @header{wx/wxcrt.h}
+ */
+template <typename T>
+size_t wxStrlcpy(T *dst, const T *src, size_t n);
+
 /**
     This function replaces the dangerous standard function @e sprintf() and is
     like @e snprintf() available on some platforms. The only difference with
index 834680691349d77a2df65fab9e94d50e7cbfeadb..00d6b690e7f5fef46132f149d5ae5cdcbc14caa7 100644 (file)
@@ -381,7 +381,7 @@ extern const char *wxDumpDate(const wxDateTime* dt)
     static char buf[128];
 
     wxString fmt(dt->Format("%Y-%m-%d (%a) %H:%M:%S"));
-    wxStrncpy(buf, fmt + " (" + dt->GetValue().ToString() + " ticks)",
+    wxStrlcpy(buf, fmt + " (" + dt->GetValue().ToString() + " ticks)",
               WXSIZEOF(buf));
 
     return buf;
index d6041c195e3561519e43bb47a8af3f23ddc052fc..9e64eaa9b8e0ec232edc6aa49a8d5ad6cdc4acfa 100644 (file)
@@ -1068,8 +1068,7 @@ const wxChar *wxSysErrorMsg(unsigned long nErrCode)
 #if !defined(__SMARTPHONE__) /* of WinCE */
     if( lpMsgBuf != 0 )
     {
-        wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
-        s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxS('\0');
+        wxStrlcpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf));
 
         LocalFree(lpMsgBuf);
 
index deffb94e27633207c11f89d91cb71cb9979f131d..dc2a79628fbfe1a5e04adc740de8a19a93f47597 100644 (file)
@@ -436,8 +436,7 @@ bool wxGetEmailAddress(wxChar *address, int maxSize)
     if ( !email )
         return false;
 
-    wxStrncpy(address, email, maxSize - 1);
-    address[maxSize - 1] = wxT('\0');
+    wxStrlcpy(address, email, maxSize);
 
     return true;
 }
index 79b013563b77f5fe07a5fc95d9097343fcc74bd0..936328d7cf1f12aa418c45344309396cdd9fd92f 100644 (file)
@@ -149,7 +149,7 @@ wxDataFormat wxRegisterClipboardFormat(char *WXUNUSED(formatName))
 bool wxGetClipboardFormatName(const wxDataFormat& dataFormat, char *formatName,
                               int maxCount)
 {
-    wxStrncpy( formatName, dataFormat.GetId().c_str(), maxCount );
+    wxStrlcpy( formatName, dataFormat.GetId().c_str(), maxCount );
 
     return true;
 }
index 006603afa56a6c8c47161af400cba6134b1908b5..93d9b31006c9c4ea5dcb0a5ff0e5203defb09568 100644 (file)
@@ -233,7 +233,7 @@ bool wxGetUserId(wxChar *buf, int n)
     if (!user)
         user = _T("user");
 
-    wxStrncpy(buf, user, n);
+    wxStrlcpy(buf, user, n);
     return true;
 }
 
@@ -254,7 +254,7 @@ bool wxGetHostName(wxChar *buf, int n)
     if (!host)
         host = _T("host");
 
-    wxStrncpy(buf, host, n);
+    wxStrlcpy(buf, host, n);
     return true;
 }
 
index fccf2634094c2ba08d5e1004f5509fcf79960412..b13dd1904e4736e0e1b8dd284b53fb7a9d197d00 100644 (file)
@@ -664,8 +664,8 @@ static void RegisterAndStoreClassName(const wxString& uniqueClassName,
                                       WNDCLASS *lpWndClass)
 {
     const size_t length = uniqueClassName.length() + 1; // for trailing NUL
-    wxChar *newChars = new wxChar[length];
-    wxStrncpy(newChars, uniqueClassName, length);
+    wxChar * const newChars = new wxChar[length];
+    wxStrlcpy(newChars, uniqueClassName, length);
     *className = newChars;
     lpWndClass->lpszClassName = *className;
 
index 795e7f6f02f6b836c6f5a8b5069267236299617e..2b0e276cefb6b91ee3113cfc2b7b1b66c7360d16 100644 (file)
@@ -247,8 +247,7 @@ bool wxCrashReportImpl::Generate(int flags, EXCEPTION_POINTERS *ep)
 /* static */
 void wxCrashReport::SetFileName(const wxString& filename)
 {
-    wxStrncpy(gs_reportFilename, filename.c_str(), WXSIZEOF(gs_reportFilename) - 1);
-    gs_reportFilename[WXSIZEOF(gs_reportFilename) - 1] = _T('\0');
+    wxStrlcpy(gs_reportFilename, filename.wx_str(), WXSIZEOF(gs_reportFilename));
 }
 
 /* static */
index 566c31c3353eb4d008f747e47e93dbb8d3a844ff..7daa30492e09085ca27a93e2e2f1b3647a727cef 100644 (file)
@@ -807,7 +807,7 @@ bool wxDialUpManagerMSW::Dial(const wxString& nameOfISP,
 
     RASDIALPARAMS rasDialParams;
     rasDialParams.dwSize = sizeof(rasDialParams);
-    wxStrncpy(rasDialParams.szEntryName, entryName, RAS_MaxEntryName);
+    wxStrlcpy(rasDialParams.szEntryName, entryName, RAS_MaxEntryName);
 
     // do we have the username and password?
     if ( !username || !password )
@@ -829,8 +829,8 @@ bool wxDialUpManagerMSW::Dial(const wxString& nameOfISP,
     }
     else
     {
-        wxStrncpy(rasDialParams.szUserName, username, UNLEN);
-        wxStrncpy(rasDialParams.szPassword, password, PWLEN);
+        wxStrlcpy(rasDialParams.szUserName, username, UNLEN);
+        wxStrlcpy(rasDialParams.szPassword, password, PWLEN);
     }
 
     // default values for other fields
index c618fe3629e93d0fe2910f4581c81810be99c4d3..546b6c7ab05f48d5e92a600cf9b5e7ab06fa9f3d 100644 (file)
@@ -494,8 +494,7 @@ int wxFileDialog::ShowModal()
 
     //=== Setting defaultFileName >>=========================================
 
-    wxStrncpy(fileNameBuffer, m_fileName, wxMAXPATH-1);
-    fileNameBuffer[ wxMAXPATH-1 ] = wxT('\0');
+    wxStrlcpy(fileNameBuffer, m_fileName, WXSIZEOF(fileNameBuffer));
 
     of.lpstrFile = fileNameBuffer;  // holds returned filename
     of.nMaxFile  = wxMAXPATH;
@@ -596,8 +595,7 @@ int wxFileDialog::ShowModal()
                 extension = extension + wxStrlen( extension ) + 1;
 
             m_fileName = AppendExtension(fileNameBuffer, extension);
-            wxStrncpy(fileNameBuffer, m_fileName.c_str(), wxMin(m_fileName.length(), wxMAXPATH-1));
-            fileNameBuffer[wxMin(m_fileName.length(), wxMAXPATH-1)] = wxT('\0');
+            wxStrlcpy(fileNameBuffer, m_fileName.c_str(), WXSIZEOF(fileNameBuffer));
         }
 
         m_path = fileNameBuffer;
index 911cfd84b5dadec0cd0effb281ad9e321b7f8ea4..a0403335204fb037c61074cbf9fcb00e48078bf3 100644 (file)
@@ -618,9 +618,7 @@ void wxNativeFontInfo::SetUnderlined(bool underlined)
 
 bool wxNativeFontInfo::SetFaceName(const wxString& facename)
 {
-    size_t len = WXSIZEOF(lf.lfFaceName);
-    wxStrncpy(lf.lfFaceName, facename, len);
-    lf.lfFaceName[len - 1] = '\0';    // truncate the face name
+    wxStrlcpy(lf.lfFaceName, facename, WXSIZEOF(lf.lfFaceName));
     return true;
 }
 
index 8ec371ae8ca85bf6e17eda425aeab1ab1c3722fd..03e53b58ad457f8ffecdf79bd63897be192753ce 100644 (file)
@@ -166,7 +166,7 @@ void wxFontEnumeratorHelper::DoEnumerate()
 #else // __WIN32__
     LOGFONT lf;
     lf.lfCharSet = (BYTE)m_charset;
-    wxStrncpy(lf.lfFaceName, m_facename, WXSIZEOF(lf.lfFaceName));
+    wxStrlcpy(lf.lfFaceName, m_facename, WXSIZEOF(lf.lfFaceName));
     lf.lfPitchAndFamily = 0;
     ::EnumFontFamiliesEx(hDC, &lf, (wxFONTENUMPROC)wxFontEnumeratorProc,
                          (LPARAM)this, 0 /* reserved */) ;
index 6e1d1cb031dfd67da46f7232e221998aa395770a..6dfb9fa207f56823bb888f58f0a75ccf0e5a8eca 100644 (file)
@@ -162,7 +162,7 @@ bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
     wxZeroMemory(lf);       // all default values
 
     lf.lfCharSet = (BYTE)info.charset;
-    wxStrncpy(lf.lfFaceName, info.facename, WXSIZEOF(lf.lfFaceName));
+    wxStrlcpy(lf.lfFaceName, info.facename, WXSIZEOF(lf.lfFaceName));
 
     HFONT hfont = ::CreateFontIndirect(&lf);
     if ( !hfont )
index 63c158daeb139caad10b4a8768c059f5f4d0acec..504899d2ee5aef071db9caaf1f9e9e158a2c317f 100644 (file)
@@ -2485,8 +2485,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                     if ( lvi.mask & LVIF_TEXT )
                     {
                         wxString text = OnGetItemText(item, lvi.iSubItem);
-                        wxStrncpy(lvi.pszText, text, lvi.cchTextMax - 1);
-                        lvi.pszText[lvi.cchTextMax - 1] = _T('\0');
+                        wxStrlcpy(lvi.pszText, text, lvi.cchTextMax);
                     }
 
                     // see comment at the end of wxListCtrl::GetColumn()
index 386be3f50b6d07f0964235d20cf6d23e9042a9f2..c7bc5edb3e4013b5b397c4b53703d204ca4241f9 100644 (file)
@@ -412,10 +412,9 @@ bool wxWindowsPrintNativeData::TransferFrom( const wxPrintData &data )
         {
             // NB: the cast is needed in the ANSI build, strangely enough
             //     dmDeviceName is BYTE[] and not char[] there
-            wxStrncpy(reinterpret_cast<wxChar *>(devMode->dmDeviceName),
+            wxStrlcpy(reinterpret_cast<wxChar *>(devMode->dmDeviceName),
                       name.wx_str(),
-                      WXSIZEOF(devMode->dmDeviceName) - 1);
-            devMode->dmDeviceName[WXSIZEOF(devMode->dmDeviceName) - 1] = wxT('\0');
+                      WXSIZEOF(devMode->dmDeviceName));
         }
 
         //// Colour
index cc52113434070e94a6a1e188e3c1e4f5dd76e1d3..856b6c1911bec6c5d1a2fb35ef1109586bfb52a2 100644 (file)
@@ -197,7 +197,7 @@ bool wxTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& tooltip)
     notifyData.uFlags |= NIF_TIP;
     if ( !tooltip.empty() )
     {
-        wxStrncpy(notifyData.szTip, tooltip.wx_str(), WXSIZEOF(notifyData.szTip));
+        wxStrlcpy(notifyData.szTip, tooltip.wx_str(), WXSIZEOF(notifyData.szTip));
     }
 
     bool ok = wxShellNotifyIcon(m_iconAdded ? NIM_MODIFY
@@ -235,8 +235,8 @@ wxTaskBarIcon::ShowBalloon(const wxString& title,
     notifyData = NotifyIconData(hwnd);
     notifyData.uFlags |= NIF_INFO;
     notifyData.uTimeout = msec;
-    wxStrncpy(notifyData.szInfo, text.wx_str(), WXSIZEOF(notifyData.szInfo));
-    wxStrncpy(notifyData.szInfoTitle, title.wx_str(),
+    wxStrlcpy(notifyData.szInfo, text.wx_str(), WXSIZEOF(notifyData.szInfo));
+    wxStrlcpy(notifyData.szInfoTitle, title.wx_str(),
                 WXSIZEOF(notifyData.szInfoTitle));
 
     if ( flags & wxICON_INFORMATION )
index 0f4ac931a7d9d01027852a1a09dbc60c14e12812..6a4c1baffe5c050fa4194d28b2ba78040dd48240 100644 (file)
@@ -2447,7 +2447,7 @@ bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
         cf.yHeight = 20*font.GetPointSize(); // 1 pt = 20 twips
         cf.bCharSet = lf.lfCharSet;
         cf.bPitchAndFamily = lf.lfPitchAndFamily;
-        wxStrncpy( cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName) );
+        wxStrlcpy(cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName));
 
         // also deal with underline/italic/bold attributes: note that we must
         // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
index 1822254f1f3a80cde80289f7ada802b9873067ad..7088abfabaceaff441e35edca8769d0eafdbe17e 100644 (file)
@@ -132,7 +132,7 @@ bool wxGetHostName(wxChar *WXUNUSED_IN_WINCE(buf),
 #if defined(__WXWINCE__)
     // TODO-CE
     return false;
-#elif defined(__WIN32__) && !defined(__WXMICROWIN__)
+#else
     DWORD nSize = maxSize;
     if ( !::GetComputerName(buf, &nSize) )
     {
@@ -142,16 +142,6 @@ bool wxGetHostName(wxChar *WXUNUSED_IN_WINCE(buf),
     }
 
     return true;
-#else
-    wxChar *sysname;
-    const wxChar *default_host = wxT("noname");
-
-    if ((sysname = wxGetenv(wxT("SYSTEM_NAME"))) == NULL) {
-        GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1);
-    } else
-        wxStrncpy(buf, sysname, maxSize - 1);
-    buf[maxSize] = wxT('\0');
-    return *buf ? true : false;
 #endif
 }
 
@@ -228,7 +218,7 @@ bool wxGetFullHostName(wxChar *buf, int maxSize)
 
             if ( !host.empty() )
             {
-                wxStrncpy(buf, host, maxSize);
+                wxStrlcpy(buf, host, maxSize);
 
                 return true;
             }
@@ -246,7 +236,7 @@ bool wxGetUserId(wxChar *WXUNUSED_IN_WINCE(buf),
 #if defined(__WXWINCE__)
     // TODO-CE
     return false;
-#elif defined(__WIN32__) && !defined(__WXMICROWIN__)
+#else
     DWORD nSize = maxSize;
     if ( ::GetUserName(buf, &nSize) == 0 )
     {
@@ -260,24 +250,6 @@ bool wxGetUserId(wxChar *WXUNUSED_IN_WINCE(buf),
     }
 
     return true;
-#else   // __WXMICROWIN__
-    wxChar *user;
-    const wxChar *default_id = wxT("anonymous");
-
-    // Can't assume we have NIS (PC-NFS) or some other ID daemon
-    // So we ...
-    if ( (user = wxGetenv(wxT("USER"))) == NULL &&
-         (user = wxGetenv(wxT("LOGNAME"))) == NULL )
-    {
-        // Use wxWidgets configuration data (comming soon)
-        GetProfileString(WX_SECTION, eUSERID, default_id, buf, maxSize - 1);
-    }
-    else
-    {
-        wxStrncpy(buf, user, maxSize - 1);
-    }
-
-    return *buf ? true : false;
 #endif
 }
 
@@ -294,8 +266,7 @@ bool wxGetUserName(wxChar *buf, int maxSize)
     wxString name;
     if(!key.QueryValue(wxT("Owner"),name))
         return false;
-    wxStrncpy(buf, name.c_str(), maxSize-1);
-    buf[maxSize-1] = _T('\0');
+    wxStrlcpy(buf, name.c_str(), maxSize);
     return true;
 #elif defined(USE_NET_API)
     CHAR szUserName[256];
@@ -374,7 +345,7 @@ error:
 
     if ( !ok )
     {
-        wxStrncpy(buf, wxT("Unknown User"), maxSize);
+        wxStrlcpy(buf, wxT("Unknown User"), maxSize);
     }
 
     return true;
index e3c65df0812381d3185999e874f6c0e98d32f94f..39dbba53d9adad37581f92fe27e899546dee29f8 100644 (file)
@@ -123,10 +123,9 @@ wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourc
 
     // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
     // so we need to find the length of the resource.
-    int len = ::SizeofResource(wxGetInstance(), hResource);
-    wxChar  *s = new wxChar[len+1];
-    wxStrncpy(s,theText,len);
-    s[len]=0;
+    int len = ::SizeofResource(wxGetInstance(), hResource) + 1;
+    wxChar *s = new wxChar[len];
+    wxStrlcpy(s, theText, len);
 
     // Obsolete in WIN32
 #ifndef __WIN32__
index b5004af9d21088066ef4aa0302bb9c5c39544421..fb93239f57da8f3fbf13948bf52e55f08fc6c991 100644 (file)
@@ -3850,8 +3850,7 @@ bool wxWindowMSW::HandleTooltipNotify(WXUINT code,
         // if we got TTN_NEEDTEXTW in Unicode build: in this case we just have
         // to copy the string we have into the buffer
         static wxChar buf[513];
-        wxStrncpy(buf, ttip.c_str(), WXSIZEOF(buf) - 1);
-        buf[WXSIZEOF(buf) - 1] = _T('\0');
+        wxStrlcpy(buf, ttip.c_str(), WXSIZEOF(buf));
         ttText->lpszText = buf;
     }
 
index 8fd33e10b03fee9409dc271f45624204bd2e3234..ca09caefc38dacfb321eaf1be87a510efeff05af 100644 (file)
@@ -697,7 +697,7 @@ bool wxNativeFontInfo::SetFaceName(
   const wxString&                   sFacename
 )
 {
-    wxStrncpy((wxChar*)fa.szFacename, sFacename, WXSIZEOF(fa.szFacename));
+    wxStrlcpy((wxChar*)fa.szFacename, sFacename, WXSIZEOF(fa.szFacename));
     return true;
 } // end of wxNativeFontInfo::SetFaceName
 
index 1af60107aafd055a26f2bb3d6c36c6fa5f9ac03b..fdb1f4af112f65411f7841fa77224c222d66913a 100644 (file)
@@ -120,7 +120,7 @@ void wxFontEnumeratorHelper::DoEnumerate()
 #ifdef __WIN32__
     LOGFONT lf;
     lf.lfCharSet = m_charset;
-    wxStrncpy(lf.lfFaceName, m_facename, WXSIZEOF(lf.lfFaceName));
+    wxStrlcpy(lf.lfFaceName, m_facename, WXSIZEOF(lf.lfFaceName));
     lf.lfPitchAndFamily = 0;
     ::EnumFontFamiliesEx(hDC, &lf, (wxFONTENUMPROC)wxFontEnumeratorProc,
                          (LPARAM)this, 0) ;
index e6d9b45d7f2c9fc296a41228f5e4eecddd0dde08..8883b7305cdcf25a227b4f9b44273a381a73b1be 100644 (file)
@@ -233,7 +233,7 @@ bool wxTestFontEncoding( const wxNativeEncodingInfo& rInfo )
     vLogFont.fsFontUse = FATTR_FONTUSE_OUTLINE |      // only outline fonts allowed
                          FATTR_FONTUSE_TRANSFORMABLE; // may be transformed
 
-    wxStrncpy((wxChar*)vLogFont.szFacename, rInfo.facename.c_str(), WXSIZEOF(vLogFont.szFacename));
+    wxStrlcpy((wxChar*)vLogFont.szFacename, rInfo.facename.c_str(), WXSIZEOF(vLogFont.szFacename));
 
     if (!::GpiCreateLogFont( hPS
                             ,NULL
@@ -549,7 +549,7 @@ void wxOS2SelectMatchingFontByName(
             break;
     }
 
-    wxStrncpy(zFontFaceName, sFaceName.c_str(), WXSIZEOF(zFontFaceName));
+    wxStrlcpy(zFontFaceName, sFaceName.c_str(), WXSIZEOF(zFontFaceName));
     nPointSize = pFont->GetPointSize();
 
     //
index 14d2c6b3e19d13da73d22591ecb62aea4485490f..5a03ba30f3070e1c6d2aba8f569a2a59d4d04a37 100644 (file)
@@ -93,13 +93,12 @@ bool wxGetHostName( wxChar* zBuf, int nMaxSize )
                                 ,(void*)zBuf
                                 ,(ULONG)nMaxSize - 1
                                );
+        zBuf[nMaxSize] = _T('\0');
     }
     else
     {
-        wxStrncpy(zBuf, zSysname, nMaxSize - 1);
+        wxStrlcpy(zBuf, zSysname, nMaxSize);
     }
-
-    zBuf[nMaxSize] = _T('\0');
 #endif
 
     return *zBuf ? true : false;
@@ -121,7 +120,7 @@ bool wxGetUserName( wxChar* zBuf, int nMaxSize )
 #ifdef USE_NET_API
     wxGetUserId( zBuf, nMaxSize );
 #else
-    wxStrncpy(zBuf, _T("Unknown User"), nMaxSize);
+    wxStrlcpy(zBuf, _T("Unknown User"), nMaxSize);
 #endif
     return true;
 }
index 66df86593625a8d752fe90efb21142e54ab0ddae..a03694b0dc755599a27d0470cc31ff6f921c48d3 100644 (file)
@@ -97,7 +97,7 @@ bool wxGetUserName(wxChar *buf, int maxSize)
         return false;
     }
 
-    wxStrncpy (buf, wxSafeConvertMB2WX(id), maxSize - 1);
+    wxStrlcpy(buf, wxSafeConvertMB2WX(id), maxSize);
 
     // free the buffer
     MemPtrUnlock(id);
index 9a1243beb74981db64b985162731528d8f357da5..f45510ae425adada6208cf86bfbbbccf56e7f665 100644 (file)
@@ -777,16 +777,14 @@ static bool wxGetHostNameInternal(wxChar *buf, int sz)
     bool ok = uname(&uts) != -1;
     if ( ok )
     {
-        wxStrncpy(buf, wxSafeConvertMB2WX(uts.nodename), sz - 1);
-        buf[sz] = wxT('\0');
+        wxStrlcpy(buf, wxSafeConvertMB2WX(uts.nodename), sz);
     }
 #elif defined(HAVE_GETHOSTNAME)
     char cbuf[sz];
     bool ok = gethostname(cbuf, sz) != -1;
     if ( ok )
     {
-        wxStrncpy(buf, wxSafeConvertMB2WX(cbuf), sz - 1);
-        buf[sz] = wxT('\0');
+        wxStrlcpy(buf, wxSafeConvertMB2WX(cbuf), sz);
     }
 #else // no uname, no gethostname
     wxFAIL_MSG(wxT("don't know host name for this machine"));
@@ -839,7 +837,7 @@ bool wxGetFullHostName(wxChar *buf, int sz)
             else
             {
                 // the canonical name
-                wxStrncpy(buf, wxSafeConvertMB2WX(host->h_name), sz);
+                wxStrlcpy(buf, wxSafeConvertMB2WX(host->h_name), sz);
             }
         }
         //else: it's already a FQDN (BSD behaves this way)
@@ -855,7 +853,7 @@ bool wxGetUserId(wxChar *buf, int sz)
     *buf = wxT('\0');
     if ((who = getpwuid(getuid ())) != NULL)
     {
-        wxStrncpy (buf, wxSafeConvertMB2WX(who->pw_name), sz - 1);
+        wxStrlcpy (buf, wxSafeConvertMB2WX(who->pw_name), sz);
         return true;
     }
 
@@ -873,7 +871,7 @@ bool wxGetUserName(wxChar *buf, int sz)
        char *comma = strchr(who->pw_gecos, ',');
        if (comma)
            *comma = '\0'; // cut off non-name comment fields
-       wxStrncpy (buf, wxSafeConvertMB2WX(who->pw_gecos), sz - 1);
+       wxStrlcpy(buf, wxSafeConvertMB2WX(who->pw_gecos), sz);
        return true;
     }