Blame SOURCES/freetype-2.4.11-pcf-read-a.patch

43e195
From 74af85c4b62b35e55b0ce9dec55ee10cbc4962a2 Mon Sep 17 00:00:00 2001
43e195
From: Werner Lemberg <wl@gnu.org>
43e195
Date: Mon, 8 Dec 2014 16:01:50 +0100
43e195
Subject: [PATCH] [pcf] Fix Savannah bug #43774.
43e195
43e195
Work around `features' of X11's `pcfWriteFont' and `pcfReadFont'
43e195
functions.  Since the PCF format doesn't have an official
43e195
specification, we have to exactly follow these functions' behaviour.
43e195
43e195
The problem was unveiled with a patch from 2014-11-06, fixing issue #43547.
43e195
43e195
* src/pcf/pcfread.c (pcf_read_TOC): Don't check table size for last
43e195
element.  Instead, assign real size.
43e195
---
43e195
 ChangeLog         | 14 ++++++++++++++
43e195
 src/pcf/pcfread.c | 54 +++++++++++++++++++++++++++++++++++++++++++-----------
43e195
 2 files changed, 57 insertions(+), 11 deletions(-)
43e195
43e195
diff --git a/src/pcf/pcfread.c b/src/pcf/pcfread.c
43e195
index 998cbed..e3caf82 100644
43e195
--- a/src/pcf/pcfread.c
43e195
+++ b/src/pcf/pcfread.c
43e195
@@ -95,9 +95,11 @@ THE SOFTWARE.
43e195
     FT_Memory  memory = FT_FACE(face)->memory;
43e195
     FT_UInt    n;
43e195
 
43e195
+    FT_ULong   size;
43e195
 
43e195
-    if ( FT_STREAM_SEEK ( 0 )                          ||
43e195
-         FT_STREAM_READ_FIELDS ( pcf_toc_header, toc ) )
43e195
+
43e195
+    if ( FT_STREAM_SEEK( 0 )                          ||
43e195
+         FT_STREAM_READ_FIELDS( pcf_toc_header, toc ) )
43e195
       return PCF_Err_Cannot_Open_Resource;
43e195
 
43e195
     if ( toc->version != PCF_FILE_VERSION                 ||
43e195
@@ -151,14 +153,35 @@ THE SOFTWARE.
43e195
         break;
43e195
     }
43e195
 
43e195
-    /* we now check whether the `size' and `offset' values are reasonable: */
43e195
-    /* `offset' + `size' must not exceed the stream size                   */
43e195
+    /*
43e195
+     *  We now check whether the `size' and `offset' values are reasonable:
43e195
+     *  `offset' + `size' must not exceed the stream size.
43e195
+     *
43e195
+     *  Note, however, that X11's `pcfWriteFont' routine (used by the
43e195
+     *  `bdftopcf' program to create PDF font files) has two special
43e195
+     *  features.
43e195
+     *
43e195
+     *  - It always assigns the accelerator table a size of 100 bytes in the
43e195
+     *    TOC, regardless of its real size, which can vary between 34 and 72
43e195
+     *    bytes.
43e195
+     *
43e195
+     *  - Due to the way the routine is designed, it ships out the last font
43e195
+     *    table with its real size, ignoring the TOC's size value.  Since
43e195
+     *    the TOC size values are always rounded up to a multiple of 4, the
43e195
+     *    difference can be up to three bytes for all tables except the
43e195
+     *    accelerator table, for which the difference can be as large as 66
43e195
+     *    bytes.
43e195
+     *
43e195
+     */
43e195
+
43e195
     tables = face->toc.tables;
43e195
-    for ( n = 0; n < toc->count; n++ )
43e195
+    size   = stream->size;
43e195
+
43e195
+    for ( n = 0; n < toc->count - 1; n++ )
43e195
     {
43e195
       /* we need two checks to avoid overflow */
43e195
-      if ( ( tables->size   > stream->size                ) ||
43e195
-           ( tables->offset > stream->size - tables->size ) )
43e195
+      if ( ( tables->size   > size                ) ||
43e195
+           ( tables->offset > size - tables->size ) )
43e195
       {
43e195
         error = PCF_Err_Invalid_Table;
43e195
         goto Exit;
43e195
@@ -166,6 +189,15 @@ THE SOFTWARE.
43e195
       tables++;
43e195
     }
43e195
 
43e195
+    /* no check of `tables->size' for last table element ... */
43e195
+    if ( ( tables->offset > size ) )
43e195
+    {
43e195
+      error = PCF_Err_Invalid_Table;
43e195
+      goto Exit;
43e195
+    }
43e195
+    /* ... instead, we adjust `tables->size' to the real value */
43e195
+    tables->size = size - tables->offset;
43e195
+
43e195
 #ifdef FT_DEBUG_LEVEL_TRACE
43e195
 
43e195
     {
43e195
@@ -714,8 +746,8 @@ THE SOFTWARE.
43e195
 
43e195
     FT_TRACE4(( "  number of bitmaps: %d\n", nbitmaps ));
43e195
 
43e195
-    /* XXX: PCF_Face->nmetrics is singed FT_Long, see pcf.h */
43e195
-    if ( face->nmetrics < 0 || nbitmaps != ( FT_ULong )face->nmetrics )
43e195
+    /* XXX: PCF_Face->nmetrics is signed FT_Long, see pcf.h */
43e195
+    if ( face->nmetrics < 0 || nbitmaps != (FT_ULong)face->nmetrics )
43e195
       return PCF_Err_Invalid_File_Format;
43e195
 
43e195
     if ( FT_NEW_ARRAY( offsets, nbitmaps ) )
43e195
-- 
43e195
2.1.0
43e195