freetype-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ft-devel] new CFF engine


From: Werner LEMBERG
Subject: Re: [ft-devel] new CFF engine
Date: Fri, 03 May 2013 16:23:05 +0200 (CEST)

> I've build freetype2 and freetype2-demos with clang's 
> -fsanitize=undefined. Here's what I get when run ftview on an otf
> font:
>
>   ttcmap.c:908:18: runtime error: left shift of negative value -1
>   [...]

Thanks.  clang is *very* picky :-) Virtually all compilers do the
right thing for left shifting negative values in case the result fits
into the data type...

May I ask you to apply the attached patch and re-run the test?  It
should remove the errors in cffgload.c; I'll try to fix the other
errors in due course, after getting your confirmation that I'm doing
the right thing.


   Werner
diff --git a/src/cff/cffgload.c b/src/cff/cffgload.c
index fc01d98..86fa371 100644
--- a/src/cff/cffgload.c
+++ b/src/cff/cffgload.c
@@ -968,11 +968,14 @@
 
 
         /* this is an operand, push it on the stack */
+
+        /* if we use shifts, all computations are done with unsigned */
+        /* values; the conversion to a signed value is the last step */
         if ( v == 28 )
         {
           if ( ip + 1 >= limit )
             goto Syntax_Error;
-          val = (FT_Short)( ( (FT_Short)ip[0] << 8 ) | ip[1] );
+          val = (FT_Short)( ( (FT_UShort)ip[0] << 8 ) | ip[1] );
           ip += 2;
         }
         else if ( v < 247 )
@@ -993,10 +996,10 @@
         {
           if ( ip + 3 >= limit )
             goto Syntax_Error;
-          val = ( (FT_Int32)ip[0] << 24 ) |
-                ( (FT_Int32)ip[1] << 16 ) |
-                ( (FT_Int32)ip[2] <<  8 ) |
-                            ip[3];
+          val = (FT_Int32)( ( (FT_UInt32)ip[0] << 24 ) |
+                            ( (FT_UInt32)ip[1] << 16 ) |
+                            ( (FT_UInt32)ip[2] <<  8 ) |
+                              (FT_UInt32)ip[3] );
           ip    += 4;
           if ( charstring_type == 2 )
             shift = 0;
@@ -1004,12 +1007,12 @@
         if ( decoder->top - stack >= CFF_MAX_OPERANDS )
           goto Stack_Overflow;
 
-        val           <<= shift;
+        val             = (FT_Int32)( (FT_UInt32)val << shift );
         *decoder->top++ = val;
 
 #ifdef FT_DEBUG_LEVEL_TRACE
         if ( !( val & 0xFFFFL ) )
-          FT_TRACE4(( " %ld", (FT_Int32)( val >> 16 ) ));
+          FT_TRACE4(( " %ld", (FT_Int32)( (FT_UInt32)val >> 16 ) ));
         else
           FT_TRACE4(( " %.2f", val / 65536.0 ));
 #endif

reply via email to

[Prev in Thread] Current Thread [Next in Thread]