Changeset 82772 in webkit


Ignore:
Timestamp:
Apr 2, 2011 12:22:07 PM (13 years ago)
Author:
commit-queue@webkit.org
Message:

2011-04-02 Ryuan Choi <ryuan.choi@samsung.com>

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):
Location:
trunk/Source/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r82769 r82772  
     12011-04-02  Ryuan Choi  <ryuan.choi@samsung.com>
     2
     3        Reviewed by Martin Robinson.
     4
     5        [GTK] Fix leaked pointer in FontGtk.cpp
     6        https://bugs.webkit.org/show_bug.cgi?id=57307
     7
     8        Fix a memory leak.
     9
     10        No new functionality, so no new tests.
     11
     12        * platform/graphics/gtk/FontGtk.cpp:
     13        (WebCore::utf16ToUtf8): Rename utf16_to_utf8 and fix indentation.
     14        (WebCore::convertUniCharToUTF8):
     15
    1162011-04-02  Ilya Tikhonovsky  <loislo@chromium.org>
    217
  • trunk/Source/WebCore/platform/graphics/gtk/FontGtk.cpp

    r82553 r82772  
    3636#include "CairoUtilities.h"
    3737#include "ContextShadow.h"
     38#include "GOwnPtr.h"
    3839#include "GraphicsContext.h"
    3940#include "NotImplemented.h"
     
    8485#define IS_LOW_SURROGATE(u)  ((UChar)(u) >= (UChar)0xdc00 && (UChar)(u) <= (UChar)0xdfff)
    8586
    86 static void utf16_to_utf8(const UChar* aText, gint aLength, char* &text, gint &length)
    87 {
    88   gboolean need_copy = FALSE;
    89   int i;
    90 
    91   for (i = 0; i < aLength; i++) {
    92     if (!aText[i] || IS_LOW_SURROGATE(aText[i])) {
    93       need_copy = TRUE;
    94       break;
    95     }
    96     else if (IS_HIGH_SURROGATE(aText[i])) {
    97       if (i < aLength - 1 && IS_LOW_SURROGATE(aText[i+1]))
    98         i++;
    99       else {
    100         need_copy = TRUE;
    101         break;
    102       }
    103     }
    104   }
    105 
    106   if (need_copy) {
    107 
    108     /* Pango doesn't correctly handle nuls.  We convert them to 0xff. */
    109     /* Also "validate" UTF-16 text to make sure conversion doesn't fail. */
    110 
    111     UChar* p = (UChar*)g_memdup(aText, aLength * sizeof(aText[0]));
    112 
    113     /* don't need to reset i */
    114     for (i = 0; i < aLength; i++) {
    115       if (!p[i] || IS_LOW_SURROGATE(p[i]))
    116         p[i] = 0xFFFD;
    117       else if (IS_HIGH_SURROGATE(p[i])) {
    118         if (i < aLength - 1 && IS_LOW_SURROGATE(aText[i+1]))
    119           i++;
    120         else
    121           p[i] = 0xFFFD;
    122       }
    123     }
    124 
    125     aText = p;
    126   }
    127 
    128   glong items_written;
    129   text = g_utf16_to_utf8(reinterpret_cast<const gunichar2*>(aText), aLength, NULL, &items_written, NULL);
    130   length = items_written;
    131 
    132   if (need_copy)
    133     g_free((gpointer)aText);
    134 
     87static gchar* utf16ToUtf8(const UChar* aText, gint aLength, gint &length)
     88{
     89    gboolean needCopy = FALSE;
     90
     91    for (int i = 0; i < aLength; i++) {
     92        if (!aText[i] || IS_LOW_SURROGATE(aText[i])) {
     93            needCopy = TRUE;
     94            break;
     95        }
     96
     97        if (IS_HIGH_SURROGATE(aText[i])) {
     98            if (i < aLength - 1 && IS_LOW_SURROGATE(aText[i+1]))
     99                i++;
     100            else {
     101                needCopy = TRUE;
     102                break;
     103            }
     104        }
     105    }
     106
     107    GOwnPtr<UChar> copiedString;
     108    if (needCopy) {
     109        /* Pango doesn't correctly handle nuls.  We convert them to 0xff. */
     110        /* Also "validate" UTF-16 text to make sure conversion doesn't fail. */
     111
     112        copiedString.set(static_cast<UChar*>(g_memdup(aText, aLength * sizeof(aText[0]))));
     113        UChar* p = copiedString.get();
     114
     115        /* don't need to reset i */
     116        for (int i = 0; i < aLength; i++) {
     117            if (!p[i] || IS_LOW_SURROGATE(p[i]))
     118                p[i] = 0xFFFD;
     119            else if (IS_HIGH_SURROGATE(p[i])) {
     120                if (i < aLength - 1 && IS_LOW_SURROGATE(aText[i+1]))
     121                    i++;
     122                else
     123                    p[i] = 0xFFFD;
     124            }
     125        }
     126
     127        aText = p;
     128    }
     129
     130    gchar* utf8Text;
     131    glong itemsWritten;
     132    utf8Text = g_utf16_to_utf8(static_cast<const gunichar2*>(aText), aLength, 0, &itemsWritten, 0);
     133    length = itemsWritten;
     134
     135    return utf8Text;
    135136}
    136137
    137138static gchar* convertUniCharToUTF8(const UChar* characters, gint length, int from, int to)
    138139{
    139     gchar* utf8 = 0;
    140     gint new_length = 0;
    141     utf16_to_utf8(characters, length, utf8, new_length);
    142     if (!utf8)
    143         return NULL;
    144 
     140    gint newLength = 0;
     141    GOwnPtr<gchar> utf8Text(utf16ToUtf8(characters, length, newLength));
     142    if (!utf8Text)
     143        return 0;
     144
     145    gchar* pos = utf8Text.get();
    145146    if (from > 0) {
    146147        // discard the first 'from' characters
    147148        // FIXME: we should do this before the conversion probably
    148         gchar* str_left = g_utf8_offset_to_pointer(utf8, from);
    149         gchar* tmp = g_strdup(str_left);
    150         g_free(utf8);
    151         utf8 = tmp;
    152     }
    153 
    154     gchar* pos = utf8;
     149        pos = g_utf8_offset_to_pointer(utf8Text.get(), from);
     150    }
     151
    155152    gint len = strlen(pos);
    156153    GString* ret = g_string_new_len(NULL, len);
Note: See TracChangeset for help on using the changeset viewer.