Commit 0a33a3984fd3243f0df86ccbda3f55b115e699a2

Werner Lemberg 2015-02-17T08:03:05

[truetype] Various signedness fixes. * include/internal/ftgloadr.h, src/truetype/ttpload.c: Apply. * src/truetype/ttgload.c: Apply. (TT_Get_VMetrics): Protect against invalid ascenders and descenders while constructing advance height.

diff --git a/ChangeLog b/ChangeLog
index 78b4db1..714288f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2015-02-17  Werner Lemberg  <wl@gnu.org>
+
+	[truetype] Various signedness fixes.
+
+	* include/internal/ftgloadr.h, src/truetype/ttpload.c: Apply.
+
+	* src/truetype/ttgload.c: Apply.
+	(TT_Get_VMetrics): Protect against invalid ascenders and descenders
+	while constructing advance height.
+
 2015-02-16  Werner Lemberg  <wl@gnu.org>
 
 	[base] Finish compiler warning fixes for signedness issues.
diff --git a/include/internal/ftgloadr.h b/include/internal/ftgloadr.h
index 2364937..970dd70 100644
--- a/include/internal/ftgloadr.h
+++ b/include/internal/ftgloadr.h
@@ -121,23 +121,25 @@ FT_BEGIN_HEADER
                               FT_UInt         n_contours );
 
 
-#define FT_GLYPHLOADER_CHECK_P( _loader, _count )           \
-  ( (_count) == 0                                        || \
-    ( (_loader)->base.outline.n_points    +                 \
-      (_loader)->current.outline.n_points +                 \
-      (unsigned long)(_count) ) <= (_loader)->max_points )
-
-#define FT_GLYPHLOADER_CHECK_C( _loader, _count )            \
-  ( (_count) == 0                                         || \
-    ( (_loader)->base.outline.n_contours    +                \
-      (_loader)->current.outline.n_contours +                \
-      (unsigned long)(_count)) <= (_loader)->max_contours )
-
-#define FT_GLYPHLOADER_CHECK_POINTS( _loader, _points, _contours )      \
-  ( ( FT_GLYPHLOADER_CHECK_P( _loader, _points )   &&                   \
-      FT_GLYPHLOADER_CHECK_C( _loader, _contours ) )                    \
-    ? 0                                                                 \
-    : FT_GlyphLoader_CheckPoints( (_loader), (_points), (_contours) ) )
+#define FT_GLYPHLOADER_CHECK_P( _loader, _count )       \
+  ( (_count) == 0                                    || \
+    ( (FT_UInt)(_loader)->base.outline.n_points    +    \
+      (FT_UInt)(_loader)->current.outline.n_points +    \
+      (FT_UInt)(_count) ) <= (_loader)->max_points   )
+
+#define FT_GLYPHLOADER_CHECK_C( _loader, _count )         \
+  ( (_count) == 0                                      || \
+    ( (FT_UInt)(_loader)->base.outline.n_contours    +    \
+      (FT_UInt)(_loader)->current.outline.n_contours +    \
+      (FT_UInt)(_count) ) <= (_loader)->max_contours   )
+
+#define FT_GLYPHLOADER_CHECK_POINTS( _loader, _points, _contours ) \
+  ( ( FT_GLYPHLOADER_CHECK_P( _loader, _points )   &&              \
+      FT_GLYPHLOADER_CHECK_C( _loader, _contours ) )               \
+    ? 0                                                            \
+    : FT_GlyphLoader_CheckPoints( (_loader),                       \
+                                  (FT_UInt)(_points),              \
+                                  (FT_UInt)(_contours) ) )
 
 
   /* check that there is enough space to add `n_subs' sub-glyphs to */
diff --git a/src/truetype/ttgload.c b/src/truetype/ttgload.c
index 0b189a5..bb0931e 100644
--- a/src/truetype/ttgload.c
+++ b/src/truetype/ttgload.c
@@ -100,13 +100,15 @@
     else if ( face->os2.version != 0xFFFFU )
     {
       *tsb = (FT_Short)( face->os2.sTypoAscender - yMax );
-      *ah  = face->os2.sTypoAscender - face->os2.sTypoDescender;
+      *ah  = (FT_UShort)FT_ABS( face->os2.sTypoAscender -
+                                face->os2.sTypoDescender );
     }
 
     else
     {
       *tsb = (FT_Short)( face->horizontal.Ascender - yMax );
-      *ah  = face->horizontal.Ascender - face->horizontal.Descender;
+      *ah  = (FT_UShort)FT_ABS( face->horizontal.Ascender -
+                                face->horizontal.Descender );
     }
 
     FT_TRACE5(( "  advance height (font units): %d\n", *ah ));
@@ -555,8 +557,8 @@
       *flag  = (FT_Byte)( f & FT_CURVE_TAG_ON );
     }
 
-    outline->n_points   = (FT_UShort)n_points;
-    outline->n_contours = (FT_Short) n_contours;
+    outline->n_points   = (FT_Short)n_points;
+    outline->n_contours = (FT_Short)n_contours;
 
     load->cursor = p;
 
@@ -707,9 +709,10 @@
                    FT_UInt       start_point,
                    FT_UInt       start_contour )
   {
-    zone->n_points    = (FT_UShort)( load->outline.n_points - start_point );
-    zone->n_contours  = (FT_Short) ( load->outline.n_contours -
-                                       start_contour );
+    zone->n_points    = (FT_UShort)load->outline.n_points -
+                          (FT_UShort)start_point;
+    zone->n_contours  = load->outline.n_contours -
+                          (FT_Short)start_contour;
     zone->org         = load->extra_points + start_point;
     zone->cur         = load->outline.points + start_point;
     zone->orus        = load->extra_points2 + start_point;
@@ -740,7 +743,7 @@
     TT_GlyphZone  zone = &loader->zone;
 
 #ifdef TT_USE_BYTECODE_INTERPRETER
-    FT_UInt       n_ins;
+    FT_Long       n_ins;
 #else
     FT_UNUSED( is_composite );
 #endif
@@ -753,7 +756,7 @@
       FT_TRACE1(( " (0x%lx byte) is truncated\n",
                  loader->glyph->control_len ));
     }
-    n_ins = (FT_UInt)( loader->glyph->control_len );
+    n_ins = loader->glyph->control_len;
 
     /* save original point position in org */
     if ( n_ins > 0 )
@@ -887,7 +890,7 @@
       error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
                                         loader->glyph_index,
                                         &deltas,
-                                        n_points );
+                                        (FT_UInt)n_points );
       if ( error )
         return error;
 
@@ -916,9 +919,9 @@
       TT_Driver  driver = (TT_Driver)FT_FACE_DRIVER( face );
 
       FT_String*  family         = face->root.family_name;
-      FT_Int      ppem           = loader->size->metrics.x_ppem;
+      FT_UInt     ppem           = loader->size->metrics.x_ppem;
       FT_String*  style          = face->root.style_name;
-      FT_Int      x_scale_factor = 1000;
+      FT_UInt     x_scale_factor = 1000;
 #endif
 
       FT_Vector*  vec   = outline->points;
@@ -946,7 +949,7 @@
              x_scale_factor != 1000                         )
         {
           x_scale = FT_MulDiv( ((TT_Size)loader->size)->metrics.x_scale,
-                               x_scale_factor, 1000 );
+                               (FT_Long)x_scale_factor, 1000 );
           y_scale = ((TT_Size)loader->size)->metrics.y_scale;
 
           /* compensate for any scaling by de/emboldening; */
@@ -1017,7 +1020,7 @@
   {
     FT_GlyphLoader  gloader    = loader->gloader;
     FT_Vector*      base_vec   = gloader->base.outline.points;
-    FT_UInt         num_points = gloader->base.outline.n_points;
+    FT_UInt         num_points = (FT_UInt)gloader->base.outline.n_points;
     FT_Bool         have_scale;
     FT_Pos          x, y;
 
@@ -1039,8 +1042,8 @@
     /* get offset */
     if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
     {
-      FT_UInt     k = subglyph->arg1;
-      FT_UInt     l = subglyph->arg2;
+      FT_UInt     k = (FT_UInt)subglyph->arg1;
+      FT_UInt     l = (FT_UInt)subglyph->arg2;
       FT_Vector*  p1;
       FT_Vector*  p2;
 
@@ -1470,7 +1473,8 @@
 
       FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
       FT_Stream_OpenMemory( &inc_stream,
-                            glyph_data.pointer, glyph_data.length );
+                            glyph_data.pointer,
+                            (FT_ULong)glyph_data.length );
 
       loader->stream = &inc_stream;
     }
@@ -1498,7 +1502,7 @@
 
       error = face->access_glyph_frame( loader, glyph_index,
                                         loader->glyf_offset + offset,
-                                        loader->byte_len );
+                                        (FT_UInt)loader->byte_len );
       if ( error )
         goto Exit;
 
@@ -1631,8 +1635,8 @@
       FT_ULong  ins_pos;  /* position of composite instructions, if any */
 
 
-      start_point   = gloader->base.outline.n_points;
-      start_contour = gloader->base.outline.n_contours;
+      start_point   = (FT_UInt)gloader->base.outline.n_points;
+      start_contour = (FT_UInt)gloader->base.outline.n_contours;
 
       /* for each subglyph, read composite header */
       error = face->read_composite_glyph( loader );
@@ -1650,7 +1654,7 @@
 
       if ( face->doblend )
       {
-        FT_Int       i, limit;
+        FT_UInt      i, limit;
         FT_SubGlyph  subglyph;
         FT_Memory    memory = face->root.memory;
 
@@ -1755,10 +1759,12 @@
           pp[2] = loader->pp3;
           pp[3] = loader->pp4;
 
-          num_base_points = gloader->base.outline.n_points;
+          num_base_points = (FT_UInt)gloader->base.outline.n_points;
 
-          error = load_truetype_glyph( loader, subglyph->index,
-                                       recurse_count + 1, FALSE );
+          error = load_truetype_glyph( loader,
+                                       (FT_UInt)subglyph->index,
+                                       recurse_count + 1,
+                                       FALSE );
           if ( error )
             goto Exit;
 
@@ -1774,7 +1780,7 @@
             loader->pp4 = pp[3];
           }
 
-          num_points = gloader->base.outline.n_points;
+          num_points = (FT_UInt)gloader->base.outline.n_points;
 
           if ( num_points == num_base_points )
             continue;
@@ -2037,7 +2043,7 @@
     error = sfnt->load_sbit_image( face,
                                    size->strike_index,
                                    glyph_index,
-                                   (FT_Int)load_flags,
+                                   (FT_UInt)load_flags,
                                    stream,
                                    &glyph->bitmap,
                                    &metrics );
@@ -2305,7 +2311,7 @@
       loader->gloader = gloader;
     }
 
-    loader->load_flags = load_flags;
+    loader->load_flags = (FT_ULong)load_flags;
 
     loader->face   = (FT_Face)face;
     loader->size   = (FT_Size)size;
diff --git a/src/truetype/ttpload.c b/src/truetype/ttpload.c
index 491fa39..fb338bd 100644
--- a/src/truetype/ttpload.c
+++ b/src/truetype/ttpload.c
@@ -118,8 +118,8 @@
       /* we only handle the case where `maxp' gives a larger value */
       if ( face->num_locations <= (FT_ULong)face->root.num_glyphs )
       {
-        FT_Long   new_loca_len =
-                    ( (FT_Long)( face->root.num_glyphs ) + 1 ) << shift;
+        FT_ULong  new_loca_len =
+                    ( (FT_ULong)face->root.num_glyphs + 1 ) << shift;
 
         TT_Table  entry = face->dir_tables;
         TT_Table  limit = entry + face->num_tables;
@@ -131,7 +131,7 @@
         /* compute the distance to next table in font file */
         for ( ; entry < limit; entry++ )
         {
-          FT_Long  diff = entry->Offset - pos;
+          FT_Long  diff = (FT_Long)entry->Offset - pos;
 
 
           if ( diff > 0 && diff < dist )
@@ -141,12 +141,12 @@
         if ( entry == limit )
         {
           /* `loca' is the last table */
-          dist = stream->size - pos;
+          dist = (FT_Long)stream->size - pos;
         }
 
-        if ( new_loca_len <= dist )
+        if ( new_loca_len <= (FT_ULong)dist )
         {
-          face->num_locations = face->root.num_glyphs + 1;
+          face->num_locations = (FT_ULong)face->root.num_glyphs + 1;
           table_len           = new_loca_len;
 
           FT_TRACE2(( "adjusting num_locations to %d\n",