DirectWrite font engine: don't leak the font table buffer
authorKonstantin Ritt <ritt.ks@gmail.com>
Mon, 2 Jul 2012 04:44:27 +0000 (07:44 +0300)
committerQt by Nokia <qt-info@nokia.com>
Mon, 2 Jul 2012 23:08:49 +0000 (01:08 +0200)
Ideally, each TryGetFontTable() call sequence should be followed
by the call to ReleaseFontTable() with the context value
taken from the first TryGetFontTable() call,
otherwise we'll leak the buffer allocated for the font table.

Change-Id: I627bf0133b7f61798e82929723ccfb780ce9ee69
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@nokia.com>
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>
src/gui/text/qfontenginedirectwrite.cpp
src/plugins/platforms/windows/qwindowsfontenginedirectwrite.cpp

index 4843d7f..0608368 100644 (file)
@@ -225,6 +225,8 @@ QFixed QFontEngineDirectWrite::lineThickness() const
 
 bool QFontEngineDirectWrite::getSfntTableData(uint tag, uchar *buffer, uint *length) const
 {
+    bool ret = false;
+
     if (m_directWriteFontFace) {
         DWORD t = qbswap<quint32>(tag);
 
@@ -237,26 +239,22 @@ bool QFontEngineDirectWrite::getSfntTableData(uint tag, uchar *buffer, uint *len
                     );
 
         if (SUCCEEDED(hr)) {
-            if (!exists)
-                return false;
-
-            if (buffer == 0) {
-                *length = tableSize;
-                return true;
-            } else if (*length < tableSize) {
-                return false;
+            if (exists) {
+                if (!buffer) {
+                    *length = tableSize;
+                    ret = true;
+                } else if (*length >= tableSize) {
+                    memcpy(buffer, tableData, tableSize);
+                    ret = true;
+                }
             }
-
-            memcpy(buffer, tableData, tableSize);
             m_directWriteFontFace->ReleaseFontTable(tableContext);
-
-            return true;
         } else {
             qErrnoWarning("QFontEngineDirectWrite::getSfntTableData: TryGetFontTable failed");
         }
     }
 
-    return false;
+    return ret;
 }
 
 QFixed QFontEngineDirectWrite::emSquareSize() const
index b02686c..568de48 100644 (file)
@@ -254,6 +254,8 @@ QFixed QWindowsFontEngineDirectWrite::lineThickness() const
 
 bool QWindowsFontEngineDirectWrite::getSfntTableData(uint tag, uchar *buffer, uint *length) const
 {
+    bool ret = false;
+
     if (m_directWriteFontFace) {
         DWORD t = qbswap<quint32>(tag);
 
@@ -266,26 +268,22 @@ bool QWindowsFontEngineDirectWrite::getSfntTableData(uint tag, uchar *buffer, ui
                     );
 
         if (SUCCEEDED(hr)) {
-            if (!exists)
-                return false;
-
-            if (buffer == 0) {
-                *length = tableSize;
-                return true;
-            } else if (*length < tableSize) {
-                return false;
+            if (exists) {
+                if (!buffer) {
+                    *length = tableSize;
+                    ret = true;
+                } else if (*length >= tableSize) {
+                    memcpy(buffer, tableData, tableSize);
+                    ret = true;
+                }
             }
-
-            memcpy(buffer, tableData, tableSize);
             m_directWriteFontFace->ReleaseFontTable(tableContext);
-
-            return true;
         } else {
             qErrnoWarning("%s: TryGetFontTable failed", __FUNCTION__);
         }
     }
 
-    return false;
+    return ret;
 }
 
 QFixed QWindowsFontEngineDirectWrite::emSquareSize() const