]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/core/strconv_cf.cpp
Fix crash in wxColour ctor from NSColor in wxOSX/Cocoa.
[wxWidgets.git] / src / osx / core / strconv_cf.cpp
index b4cde0c351bee28e264797685b30fec92068c043..c77825c23ac77f56008b2239b8ba84e57901234e 100644 (file)
@@ -33,6 +33,7 @@
  * assume ABI compatibility even within a given wxWidgets release.
  */
 
+#if wxUSE_FONTMAP
 WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_cf( const char* name)
 {
     wxMBConv_cf *result = new wxMBConv_cf(name);
@@ -44,6 +45,7 @@ WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_cf( const char* name)
     else
         return result;
 }
+#endif // wxUSE_FONTMAP
 
 WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_cf(wxFontEncoding encoding)
 {
@@ -115,8 +117,7 @@ WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_cf(wxFontEncoding encoding)
                     dstSize * sizeof(wchar_t),
                     &usedBufLen);
 
-            // charsConverted is > 0 iff conversion succeeded
-            if(charsConverted <= 0)
+            if(charsConverted < CFStringGetLength(theString))
                 return wxCONV_FAILED;
 
             /* usedBufLen is the number of bytes written, so we divide by
@@ -131,11 +132,11 @@ WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_cf(wxFontEncoding encoding)
         else
         {
             // NOTE: Includes NULL iff source did
-            /* NOTE: This is an approximation.  The eventual UTF-32 will    
+            /* NOTE: This is an approximation.  The eventual UTF-32 will
              * possibly have less elements but certainly not more.
              */
             size_t returnSize = CFStringGetLength(theString);
-    
+
             if (dstSize == 0 || dst == NULL)
             {
                 return returnSize;
@@ -145,13 +146,13 @@ WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_cf(wxFontEncoding encoding)
             // for an undersized UTF-32 destination buffer.
             CFRange fullStringRange = CFRangeMake(0, CFStringGetLength(theString));
             UniChar *szUniCharBuffer = new UniChar[fullStringRange.length];
-    
+
             CFStringGetCharacters(theString, fullStringRange, szUniCharBuffer);
-    
+
             wxMBConvUTF16 converter;
             returnSize = converter.ToWChar( dst, dstSize, (const char*)szUniCharBuffer, fullStringRange.length );
             delete [] szUniCharBuffer;
-    
+
             return returnSize;
         }
         // NOTREACHED
@@ -206,19 +207,20 @@ WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_cf(wxFontEncoding encoding)
         CFIndex usedBufLen;
 
         CFIndex charsConverted = CFStringGetBytes(
-                theString, 
+                theString,
                 CFRangeMake(0, CFStringGetLength(theString)),
                 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
             );
 
-        // charsConverted is > 0 iff conversion succeeded
-        if(charsConverted <= 0)
+        // 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;