[glib] Revert "[MacOS] Fallback to CFStringGetCSTring if CFStringGetCStringPtr fails."



commit 13c6d9fedfec139ffafb593425e2ee0fbc17c00d
Author: Philip Withnall <withnall endlessm com>
Date:   Sat Oct 14 09:04:43 2017 +0100

    Revert "[MacOS] Fallback to CFStringGetCSTring if CFStringGetCStringPtr fails."
    
    This reverts commit c60226e0a1cae40a96ed0bf95ca7d5a508548f58.
    
    Pushed without review. This should be reviewed on bug #788936 first.
    Several issues with the patch:
     • Takes the string length unnecessarily early.
     • Calls CFRelease(str) before g_strdup(cstr) which could lead to
       use-after-free.
     • Code style issues.
     • Don’t want to encourage pushing patches without review.

 gio/gosxappinfo.c     |   34 ++--------------------------------
 gio/gosxcontenttype.c |   34 ++--------------------------------
 2 files changed, 4 insertions(+), 64 deletions(-)
---
diff --git a/gio/gosxappinfo.c b/gio/gosxappinfo.c
index 7c2b402..b24b6ff 100644
--- a/gio/gosxappinfo.c
+++ b/gio/gosxappinfo.c
@@ -175,44 +175,14 @@ static gchar *
 create_cstr_from_cfstring (CFStringRef str)
 {
   const gchar *cstr;
-  CFIndex length = CFStringGetLength (str);
-  char *buffer = NULL;
 
   if (str == NULL)
     return NULL;
 
   cstr = CFStringGetCStringPtr (str, kCFStringEncodingUTF8);
-  /* CFStringGetCStringPtr returns "NULL if the internal storage of
-   * theString does not allow [a pointer] to be returned efficiently".
-   * (Apple's docs don't say what that means). In that case we must
-   * use CFStringGetCString as a fallback.
-   */
-  if (cstr != NULL)
-    {
-        CFRelease (str);
-        return g_strdup (cstr);
-    }
-  
-  buffer = g_malloc0 (length + 1);
-  /* Start off with a buffer size sufficient for the most likely case
-   * that the CFString is ASCII so the UTF8 representation will be 1
-   * byte per code point. Keep trying up to 4 bytes per code point,
-   * the max allowed by RFC3629.
-   */
-  for (int i = 1; i <= 4; ++i)
-    {
-      if (CFStringGetCString (str, buffer, length, kCFStringEncodingUTF8))
-      {
-        CFRelease (str);
-        return buffer;
-      }
-      length += length;
-      buffer = g_realloc (buffer, length + 1);
-    }
-  
-  g_free (buffer);
   CFRelease (str);
-  return NULL;
+
+  return g_strdup (cstr);
 }
 
 static char *
diff --git a/gio/gosxcontenttype.c b/gio/gosxcontenttype.c
index c046d9e..485f5bf 100644
--- a/gio/gosxcontenttype.c
+++ b/gio/gosxcontenttype.c
@@ -53,44 +53,14 @@ static gchar *
 create_cstr_from_cfstring (CFStringRef str)
 {
   const gchar *cstr;
-  CFIndex length = CFStringGetLength (str);
-  char *buffer = NULL;
 
   if (str == NULL)
     return NULL;
 
   cstr = CFStringGetCStringPtr (str, kCFStringEncodingUTF8);
-  /* CFStringGetCStringPtr returns "NULL if the internal storage of
-   * theString does not allow [a pointer] to be returned efficiently".
-   * (Apple's docs don't say what that means). In that case we must
-   * use CFStringGetCString as a fallback.
-   */
-  if (cstr != NULL)
-    {
-        CFRelease (str);
-        return g_strdup (cstr);
-    }
-  
-  buffer = g_malloc0 (length + 1);
-  /* Start off with a buffer size sufficient for the most likely case
-   * that the CFString is ASCII so the UTF8 representation will be 1
-   * byte per code point. Keep trying up to 4 bytes per code point,
-   * the max allowed by RFC3629.
-   */
-  for (int i = 1; i <= 4; ++i)
-    {
-      if (CFStringGetCString (str, buffer, length, kCFStringEncodingUTF8))
-        {
-          CFRelease (str);
-          return buffer;
-        }
-      length += length;
-      buffer = g_realloc (buffer, length + 1);
-    }
-  
-  g_free (buffer);
   CFRelease (str);
-  return NULL;
+
+  return g_strdup (cstr);
 }
 
 /*< internal >


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]