From: commit-queue@webkit.org Date: Thu, 31 Mar 2011 03:46:54 +0000 (+0000) Subject: 2011-03-30 Ryuan Choi X-Git-Url: http://git.webkit.org/?p=WebKit-https.git;a=commitdiff_plain;h=8037ff032b418c9166666dd4a911b612383bd4e9 2011-03-30 Ryuan Choi Reviewed by Martin Robinson. [GTK] Fix leaked pointer in FontGtk.cpp https://bugs.webkit.org/show_bug.cgi?id=57307 Fix a memory leak. No new functionality, so no new tests. * platform/graphics/gtk/FontGtk.cpp: (WebCore::utf16ToUtf8): Rename utf16_to_utf8 and fix indentation. (WebCore::convertUniCharToUTF8): git-svn-id: https://svn.webkit.org/repository/webkit/trunk@82541 268f45cc-cd09-0410-ab3c-d52691b4dbfc --- diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog index 6fa2eed..67f199c 100644 --- a/Source/WebCore/ChangeLog +++ b/Source/WebCore/ChangeLog @@ -1,3 +1,18 @@ +2011-03-30 Ryuan Choi + + Reviewed by Martin Robinson. + + [GTK] Fix leaked pointer in FontGtk.cpp + https://bugs.webkit.org/show_bug.cgi?id=57307 + + Fix a memory leak. + + No new functionality, so no new tests. + + * platform/graphics/gtk/FontGtk.cpp: + (WebCore::utf16ToUtf8): Rename utf16_to_utf8 and fix indentation. + (WebCore::convertUniCharToUTF8): + 2011-03-30 Dominic Cooney Reviewed by Dimitri Glazkov. diff --git a/Source/WebCore/platform/graphics/gtk/FontGtk.cpp b/Source/WebCore/platform/graphics/gtk/FontGtk.cpp index d14b052..977aa62 100644 --- a/Source/WebCore/platform/graphics/gtk/FontGtk.cpp +++ b/Source/WebCore/platform/graphics/gtk/FontGtk.cpp @@ -35,9 +35,10 @@ #include "CairoUtilities.h" #include "ContextShadow.h" -#include "PlatformContextCairo.h" +#include "GOwnPtr.h" #include "GraphicsContext.h" #include "NotImplemented.h" +#include "PlatformContextCairo.h" #include "SimpleFontData.h" #include "TextRun.h" #include @@ -84,75 +85,71 @@ IntRect getPangoRegionExtents(PangoRegionType region) #define IS_HIGH_SURROGATE(u) ((UChar)(u) >= (UChar)0xd800 && (UChar)(u) <= (UChar)0xdbff) #define IS_LOW_SURROGATE(u) ((UChar)(u) >= (UChar)0xdc00 && (UChar)(u) <= (UChar)0xdfff) -static void utf16_to_utf8(const UChar* aText, gint aLength, char* &text, gint &length) +static gchar* utf16ToUtf8(const UChar* aText, gint aLength, gint &length) { - gboolean need_copy = FALSE; - int i; + gboolean needCopy = FALSE; - for (i = 0; i < aLength; i++) { - if (!aText[i] || IS_LOW_SURROGATE(aText[i])) { - need_copy = TRUE; - break; - } - else if (IS_HIGH_SURROGATE(aText[i])) { - if (i < aLength - 1 && IS_LOW_SURROGATE(aText[i+1])) - i++; - else { - need_copy = TRUE; - break; - } + for (int i = 0; i < aLength; i++) { + if (!aText[i] || IS_LOW_SURROGATE(aText[i])) { + needCopy = TRUE; + break; + } + + if (IS_HIGH_SURROGATE(aText[i])) { + if (i < aLength - 1 && IS_LOW_SURROGATE(aText[i+1])) + i++; + else { + needCopy = TRUE; + break; + } + } } - } - - if (need_copy) { - /* Pango doesn't correctly handle nuls. We convert them to 0xff. */ - /* Also "validate" UTF-16 text to make sure conversion doesn't fail. */ - - UChar* p = (UChar*)g_memdup(aText, aLength * sizeof(aText[0])); - - /* don't need to reset i */ - for (i = 0; i < aLength; i++) { - if (!p[i] || IS_LOW_SURROGATE(p[i])) - p[i] = 0xFFFD; - else if (IS_HIGH_SURROGATE(p[i])) { - if (i < aLength - 1 && IS_LOW_SURROGATE(aText[i+1])) - i++; - else - p[i] = 0xFFFD; - } + GOwnPtr copiedString; + if (needCopy) { + /* Pango doesn't correctly handle nuls. We convert them to 0xff. */ + /* Also "validate" UTF-16 text to make sure conversion doesn't fail. */ + + copiedString.set(static_cast(g_memdup(aText, aLength * sizeof(aText[0])))); + UChar* p = copiedString.get(); + + /* don't need to reset i */ + for (int i = 0; i < aLength; i++) { + if (!p[i] || IS_LOW_SURROGATE(p[i])) + p[i] = 0xFFFD; + else if (IS_HIGH_SURROGATE(p[i])) { + if (i < aLength - 1 && IS_LOW_SURROGATE(aText[i+1])) + i++; + else + p[i] = 0xFFFD; + } + } + + aText = p; } - aText = p; - } - - glong items_written; - text = g_utf16_to_utf8(reinterpret_cast(aText), aLength, NULL, &items_written, NULL); - length = items_written; - - if (need_copy) - g_free((gpointer)aText); + gchar* utf8Text; + glong itemsWritten; + utf8Text = g_utf16_to_utf8(static_cast(aText), aLength, 0, &itemsWritten, 0); + length = itemsWritten; + return utf8Text; } static gchar* convertUniCharToUTF8(const UChar* characters, gint length, int from, int to) { - gchar* utf8 = 0; - gint new_length = 0; - utf16_to_utf8(characters, length, utf8, new_length); - if (!utf8) - return NULL; + gint newLength = 0; + GOwnPtr utf8Text(utf16ToUtf8(characters, length, newLength)); + if (!utf8Text) + return 0; + gchar* pos = utf8Text.get(); if (from > 0) { // discard the first 'from' characters // FIXME: we should do this before the conversion probably - gchar* str_left = g_utf8_offset_to_pointer(utf8, from); - gchar* tmp = g_strdup(str_left); - g_free(utf8); - utf8 = tmp; + pos = g_utf8_offset_to_pointer(utf8Text.get(), from); } - gchar* pos = utf8; gint len = strlen(pos); GString* ret = g_string_new_len(NULL, len);