From eb087029329fab4b9d4d956b525eba752ee8d5e3 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sun, 28 Mar 2010 22:39:39 +0000
Subject: [PATCH] Fix bug in wxMBConv_cf::FromWChar() in OS X.

Apparently CFStringGetBytes() doesn't always behave as expected, work around
this by checking that the returned buffer size is not greater than the size we
passed in.

Closes #11859.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63772 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 src/osx/core/strconv_cf.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/osx/core/strconv_cf.cpp b/src/osx/core/strconv_cf.cpp
index aba2402411..c77825c23a 100644
--- a/src/osx/core/strconv_cf.cpp
+++ b/src/osx/core/strconv_cf.cpp
@@ -212,13 +212,15 @@ WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_cf(wxFontEncoding encoding)
                 m_encoding,
                 0, // FAIL on unconvertible characters
                 false, // not an external representation
-                // if dstSize is 0 then pass NULL to get required length in usedBufLen
-                (dstSize != 0)?(UInt8*)dst:NULL,
+                (UInt8*)dst,
                 dstSize,
                 &usedBufLen
             );
 
-        if(charsConverted < CFStringGetLength(theString) )
+        // when dst is non-NULL, we check usedBufLen against dstSize as
+        // CFStringGetBytes sometimes treats dst as being NULL when dstSize==0
+        if( (charsConverted < CFStringGetLength(theString)) ||
+                (dst && (size_t) usedBufLen > dstSize) )
             return wxCONV_FAILED;
 
         return usedBufLen;
-- 
2.47.2