freetype-commit
[Top][All Lists]
Advanced

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

[freetype2-demos] master f21dd6a 5/6: Use new string functions to replac


From: Werner LEMBERG
Subject: [freetype2-demos] master f21dd6a 5/6: Use new string functions to replace remaining `sprintf' calls.
Date: Fri, 24 Jul 2020 10:16:50 -0400 (EDT)

branch: master
commit f21dd6ad3a3877cc8af1f37606e3d06032c7fc8a
Author: David Turner <david@freetype.org>
Commit: Werner Lemberg <wl@gnu.org>

    Use new string functions to replace remaining `sprintf' calls.
    
    This also removes calls to `strncat' and friends.
    
    * src/ftcommon.c: Include `strbuf.h'.
    (FTDemo_Version, FTDemo_DrawHeader): Do it.
    
    * src/ftmulti.c: Include `strbuf.h'.
    (main): Do it.
    
    * src/ttdebug.c: Include `strbuf.h'.
    (tempStr): Increase size to 256 bytes.
    (Cur_U_Line): Do it.
    (RunIns): Make `round_str' static.
---
 ChangeLog      |  17 +++++++++
 src/ftcommon.c |  71 +++++++++++++++++++++---------------
 src/ftmulti.c  |  58 +++++++++++++++---------------
 src/ttdebug.c  | 111 ++++++++++++++++++++++++++-------------------------------
 4 files changed, 139 insertions(+), 118 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e555cbe..a2a395c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,22 @@
 2020-07-24  David Turner  <david@freetype.org>
 
+       Use new string functions to replace remaining `sprintf' calls.
+
+       This also removes calls to `strncat' and friends.
+
+       * src/ftcommon.c: Include `strbuf.h'.
+       (FTDemo_Version, FTDemo_DrawHeader): Do it.
+
+       * src/ftmulti.c: Include `strbuf.h'.
+       (main): Do it.
+
+       * src/ttdebug.c: Include `strbuf.h'.
+       (tempStr): Increase size to 256 bytes.
+       (Cur_U_Line): Do it.
+       (RunIns): Make `round_str' static.
+
+2020-07-24  David Turner  <david@freetype.org>
+
        Add functions to safely append strings to fixed-size buffers.
 
        Not used yet.
diff --git a/src/ftcommon.c b/src/ftcommon.c
index 535cd7b..00cbd81 100644
--- a/src/ftcommon.c
+++ b/src/ftcommon.c
@@ -35,6 +35,7 @@
 #define FT_ERROR_END_LIST       default: str = "unknown error"; }
 
 #include "common.h"
+#include "strbuf.h"
 #include "ftcommon.h"
 
 #include <stdio.h>
@@ -556,6 +557,7 @@
   {
     FT_Int     major, minor, patch;
     FT_String  format[] = "%d.%d.%d";
+    StrBuf     sb;
 
 
     FT_Library_Version( handle->library, &major, &minor, &patch );
@@ -564,7 +566,8 @@
       format[5] = '\0';   /* terminate early */
 
     /* append the version string */
-    sprintf( str + strlen( str ), format, major, minor, patch );
+    strbuf_init( &sb, str, strlen( str ) );
+    strbuf_format( &sb, format, major, minor, patch );
   }
 
 
@@ -961,7 +964,8 @@
                       int              error_code )
   {
     FT_Face      face;
-    char         buf[256];
+    char         buffer[256];
+    StrBuf       buf[1];
     const char*  basename;
     int          ppem;
 
@@ -980,9 +984,11 @@
 
 
     /* font and file name */
-    x = sprintf( buf, "%.50s %.50s", face->family_name, face->style_name );
+    strbuf_init( buf, buffer, sizeof ( buffer ) );
+    x = strbuf_format( buf, "%.50s %.50s", face->family_name,
+                       face->style_name );
     grWriteCellString( display->bitmap, 0, line * HEADER_HEIGHT,
-                       buf, display->fore_color );
+                       strbuf_value( buf ), display->fore_color );
 
     basename = ft_basename( handle->current_font->filepathname );
     x = display->bitmap->width - 8 * (int)strlen( basename ) > 8 * x + 8 ?
@@ -995,49 +1001,53 @@
                                                face->size->metrics.y_scale )
                                   : face->size->metrics.y_ppem * 64;
 
+    strbuf_reset( buf );
     if ( res == 72 )
-      x  = sprintf( buf, "%.4g ppem", ppem / 64.0 );
+      strbuf_format( buf, "%.4g ppem", ppem / 64.0 );
     else
-      x  = sprintf( buf, "%g pt at %d dpi, %.4g ppem",
-                         ptsize / 64.0, res, ppem / 64.0 );
+      strbuf_format( buf, "%g pt at %d dpi, %.4g ppem",
+                     ptsize / 64.0, res, ppem / 64.0 );
 
     if ( face->face_index >> 16 )
-      x += sprintf( buf + x, ", instance %ld/%ld",
-                             face->face_index >> 16,
-                             face->style_flags >> 16 );
+      strbuf_format( buf, ", instance %ld/%ld",
+                     face->face_index >> 16,
+                     face->style_flags >> 16 );
 
     grWriteCellString( display->bitmap, 0, line * HEADER_HEIGHT,
-                       buf, display->fore_color );
+                       strbuf_value( buf ), display->fore_color );
 
     if ( abs( ptsize * res / 64 - face->size->metrics.y_ppem * 72 ) > 36 ||
          error_code                                                      )
     {
+      strbuf_reset( buf );
+
       switch ( error_code )
       {
       case FT_Err_Ok:
-        sprintf( buf, "Available size shown" );
+        strbuf_add( buf, "Available size shown" );
         break;
       case FT_Err_Invalid_Pixel_Size:
-        sprintf( buf, "Invalid pixel size" );
+        strbuf_add( buf, "Invalid pixel size" );
         break;
       case FT_Err_Invalid_PPem:
-        sprintf( buf, "Invalid ppem value" );
+        strbuf_add( buf, "Invalid ppem value" );
         break;
       default:
-        sprintf( buf, "Error 0x%04x", (FT_UShort)error_code );
+        strbuf_format( buf, "Error 0x%04x", (FT_UShort)error_code );
       }
       grWriteCellString( display->bitmap, 8 * x + 16, line * HEADER_HEIGHT,
-                         buf, display->warn_color );
+                         strbuf_value( buf ), display->warn_color );
     }
 
     /* gamma */
+    strbuf_reset( buf );
     if ( display->gamma == 0.0 )
-      sprintf( buf, "gamma: sRGB" );
+      strbuf_add( buf, "gamma: sRGB" );
     else
-      sprintf( buf, "gamma = %.1f", display->gamma );
+      strbuf_format( buf, "gamma = %.1f", display->gamma );
     grWriteCellString( display->bitmap,
                        display->bitmap->width - 8 * 11, line * HEADER_HEIGHT,
-                       buf, display->fore_color );
+                       strbuf_value( buf ), display->fore_color );
 
     line++;
 
@@ -1096,25 +1106,30 @@
         encoding = "Other";
       }
 
+      strbuf_reset( buf );
       if ( handle->encoding == FT_ENCODING_ORDER )
-        x = sprintf( buf, "%s idx: %d",
-                          encoding, idx );
+        x = strbuf_format( buf, "%s idx: %d",
+                           encoding, idx );
       else if ( handle->encoding == FT_ENCODING_UNICODE )
-        x = sprintf( buf, "%s charcode: U+%04X (glyph idx %d)",
-                          encoding, idx, glyph_idx );
+        x = strbuf_format( buf, "%s charcode: U+%04X (glyph idx %d)",
+                           encoding, idx, glyph_idx );
       else
-        x = sprintf( buf, "%s charcode: 0x%X (glyph idx %d)",
-                          encoding, idx, glyph_idx );
+        x = strbuf_format( buf, "%s charcode: 0x%X (glyph idx %d)",
+                           encoding, idx, glyph_idx );
 
       if ( FT_HAS_GLYPH_NAMES( face ) )
       {
-        x += sprintf( buf + x, ", name: " );
+        x += strbuf_add( buf, ", name: " );
 
-        FT_Get_Glyph_Name( face, glyph_idx, buf + x, (FT_UInt)( 256 - x ) );
+        /* NOTE: This relies on the fact that `FT_Get_Glyph_Name' */
+        /* always appends a terminating zero to the input.        */
+        FT_Get_Glyph_Name( face, glyph_idx,
+                           strbuf_end( buf ),
+                           (FT_UInt)( strbuf_available( buf ) + 1 ) );
       }
 
       grWriteCellString( display->bitmap, 0, (line++) * HEADER_HEIGHT,
-                         buf, display->fore_color );
+                         strbuf_value( buf ), display->fore_color );
     }
 
   }
diff --git a/src/ftmulti.c b/src/ftmulti.c
index 5a3cf38..37dd103 100644
--- a/src/ftmulti.c
+++ b/src/ftmulti.c
@@ -21,6 +21,7 @@
 
 #include "common.h"
 #include "mlgetopt.h"
+#include "strbuf.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -1147,7 +1148,8 @@
 
     for ( ;; )
     {
-      int  key;
+      int     key;
+      StrBuf  header[1];
 
 
       Clear_Display();
@@ -1164,10 +1166,11 @@
           Render_All( (unsigned int)Num, ptsize );
         }
 
-        sprintf( Header, "%.50s %.50s (file %.100s)",
-                         face->family_name,
-                         face->style_name,
-                         ft_basename( argv[file] ) );
+        strbuf_init( header, Header, sizeof ( Header ) );
+        strbuf_format( header, "%.50s %.50s (file %.100s)",
+                       face->family_name,
+                       face->style_name,
+                       ft_basename( argv[file] ) );
 
         if ( !new_header )
           new_header = Header;
@@ -1175,11 +1178,14 @@
         grWriteCellString( bit, 0, 0, new_header, fore_color );
         new_header = NULL;
 
-        sprintf( Header, "PS name: %s",
-                         FT_Get_Postscript_Name( face ) );
+        strbuf_reset( header );
+        strbuf_format( header, "PS name: %s",
+                       FT_Get_Postscript_Name( face ) );
         grWriteCellString( bit, 0, 16, Header, fore_color );
 
-        sprintf( Header, "axes:" );
+        strbuf_reset( header );
+        strbuf_add( header, "axes:" );
+
         {
           unsigned int  limit = num_shown_axes > MAX_MM_AXES / 2
                                   ? MAX_MM_AXES / 2
@@ -1188,18 +1194,13 @@
 
           for ( n = 0; n < limit; n++ )
           {
-            char  temp[100];
-            int   axis;
-
+            int  axis = shown_axes[n];
 
-            axis = shown_axes[n];
 
-            sprintf( temp, "  %.50s%s: %.02f",
+            strbuf_format( header, "  %.50s%s: %.02f",
                            multimaster->axis[axis].name,
                            hidden[axis] ? "*" : "",
                            design_pos[axis] / 65536.0 );
-            strncat( Header, temp,
-                     sizeof ( Header ) - strlen( Header ) - 1 );
           }
         }
         grWriteCellString( bit, 0, 24, Header, fore_color );
@@ -1209,22 +1210,18 @@
           unsigned int  limit = num_shown_axes;
 
 
-          sprintf( Header, "     " );
+          strbuf_reset( header );
+          strbuf_add( header, "     " );
 
           for ( n = MAX_MM_AXES / 2; n < limit; n++ )
           {
-            char  temp[100];
-            int   axis;
-
+            int  axis = shown_axes[n];
 
-            axis = shown_axes[n];
 
-            sprintf( temp, "  %.50s%s: %.02f",
+            strbuf_format( header, "  %.50s%s: %.02f",
                            multimaster->axis[axis].name,
                            hidden[axis] ? "*" : "",
                            design_pos[axis] / 65536.0 );
-            strncat( Header, temp,
-                     sizeof ( Header ) - strlen( Header ) - 1 );
           }
 
           grWriteCellString( bit, 0, 32, Header, fore_color );
@@ -1255,15 +1252,18 @@
                                        ? "TrueType (v38)"
                                        : "TrueType (v40)" ) );
 
-          sprintf( Header, "at %d points, first glyph = %d, format = %s",
-                           ptsize,
-                           Num,
-                           format_str );
+          strbuf_reset( header );
+          strbuf_format( header,
+                         "at %d points, first glyph = %d, format = %s",
+                         ptsize,
+                         Num,
+                         format_str );
         }
       }
       else
-        sprintf( Header, "%.100s: not an MM font file, or could not be opened",
-                         ft_basename( argv[file] ) );
+        strbuf_format( header,
+                       "%.100s: not an MM font file, or could not be opened",
+                       ft_basename( argv[file] ) );
 
       grWriteCellString( bit, 0, 8, Header, fore_color );
       grRefreshSurface( surface );
diff --git a/src/ttdebug.c b/src/ttdebug.c
index e73338b..cf7c93f 100644
--- a/src/ttdebug.c
+++ b/src/ttdebug.c
@@ -55,6 +55,7 @@
 #include FT_FREETYPE_H
 #include FT_MULTIPLE_MASTERS_H
 #include "common.h"
+#include "strbuf.h"
 #include "mlgetopt.h"
 
 #include FT_DRIVER_H
@@ -96,9 +97,8 @@
   typedef char  ByteStr[2];
   typedef char  WordStr[4];
   typedef char  LongStr[8];
-  typedef char  DebugStr[128];
 
-  static DebugStr  tempStr;
+  static char   tempStr[256];
 
 
   typedef struct  Storage_
@@ -1656,19 +1656,19 @@
   static const FT_String*
   Cur_U_Line( TT_ExecContext  exc )
   {
-    FT_String  s[32];
-    FT_Int     op, i, n;
+    FT_Int  op, i, n;
+    StrBuf  bs[1];
 
 
     op = CUR.code[CUR.IP];
 
-    sprintf( tempStr, "%s", OpStr[op] );
+    strbuf_init( bs, tempStr, sizeof ( tempStr ) );
+    strbuf_add( bs, OpStr[op] );
 
     if ( op == 0x40 )  /* NPUSHB */
     {
       n = CUR.code[CUR.IP + 1];
-      sprintf( s, "(%d)", n );
-      strncat( tempStr, s, 8 );
+      strbuf_format( bs, "(%d)", n );
 
       /* limit output */
       if ( n > 20 )
@@ -1676,19 +1676,14 @@
 
       for ( i = 0; i < n; i++ )
       {
-        const FT_String*  temp;
-
-
-        temp = use_hex ? " $%02x" : " %d";
-        sprintf( s, temp, CUR.code[CUR.IP + i + 2] );
-        strncat( tempStr, s, 8 );
+        strbuf_format( bs, ( use_hex ? " $%02x" : " %d" ),
+                       CUR.code[CUR.IP + i + 2] );
       }
     }
     else if ( op == 0x41 )  /* NPUSHW */
     {
       n = CUR.code[CUR.IP + 1];
-      sprintf( s, "(%d)", n );
-      strncat( tempStr, s, 8 );
+      strbuf_format( bs, "(%d)", n );
 
       /* limit output */
       if ( n > 20 )
@@ -1697,9 +1692,9 @@
       for ( i = 0; i < n; i++ )
       {
         if ( use_hex )
-          sprintf( s, " $%02x%02x",
-                      CUR.code[CUR.IP + i * 2 + 2],
-                      CUR.code[CUR.IP + i * 2 + 3] );
+          strbuf_format( bs, " $%02x%02x",
+                         CUR.code[CUR.IP + i * 2 + 2],
+                         CUR.code[CUR.IP + i * 2 + 3] );
         else
         {
           unsigned short  temp;
@@ -1707,10 +1702,8 @@
 
           temp = (unsigned short)( ( CUR.code[CUR.IP + i * 2 + 2] << 8 ) +
                                      CUR.code[CUR.IP + i * 2 + 3]        );
-          sprintf( s, " %d",
-                      (signed short)temp );
+          strbuf_format( bs, " %u", temp );
         }
-        strncat( tempStr, s, 8 );
       }
     }
     else if ( ( op & 0xF8 ) == 0xB0 )  /* PUSHB */
@@ -1719,12 +1712,8 @@
 
       for ( i = 0; i <= n; i++ )
       {
-        const FT_String*  temp;
-
-
-        temp = use_hex ? " $%02x" : " %d";
-        sprintf( s, temp, CUR.code[CUR.IP + i + 1] );
-        strncat( tempStr, s, 8 );
+        strbuf_format( bs, ( use_hex ? " $%02x" : " %d" ),
+                       CUR.code[CUR.IP + i + 1] );
       }
     }
     else if ( ( op & 0xF8 ) == 0xB8 )  /* PUSHW */
@@ -1734,9 +1723,9 @@
       for ( i = 0; i <= n; i++ )
       {
         if ( use_hex )
-          sprintf( s, " $%02x%02x",
-                      CUR.code[CUR.IP + i * 2 + 1],
-                      CUR.code[CUR.IP + i * 2 + 2] );
+          strbuf_format( bs, " $%02x%02x",
+                         CUR.code[CUR.IP + i * 2 + 1],
+                         CUR.code[CUR.IP + i * 2 + 2] );
         else
         {
           unsigned short  temp;
@@ -1744,19 +1733,16 @@
 
           temp = (unsigned short)( ( CUR.code[CUR.IP + i * 2 + 1] << 8 ) +
                                      CUR.code[CUR.IP + i * 2 + 2]        );
-          sprintf( s, " %d",
-                      (signed short)temp );
+          strbuf_format( bs, " %d", (signed short)temp );
         }
-        strncat( tempStr, s, 8 );
       }
     }
     else if ( op == 0x39 )  /* IP */
     {
-      sprintf( s, " rp1=%d, rp2=%d", CUR.GS.rp1, CUR.GS.rp2 );
-      strncat( tempStr, s, 31 );
+      strbuf_format( bs, " rp1=%d, rp2=%d", CUR.GS.rp1, CUR.GS.rp2 );
     }
 
-    return (FT_String*)tempStr;
+    return (FT_String*)strbuf_value( bs );
   }
 
 
@@ -1999,7 +1985,7 @@
 
     const FT_String*  code_range;
 
-    const FT_String*  round_str[8] =
+    static const FT_String*  round_str[8] =
     {
       "to half-grid",
       "to grid",
@@ -2079,35 +2065,35 @@
         /* [loc]:[addr] [opcode]  [disassembly]         [a][b]|[c][d]      */
 
         {
-          char  temp[90];
-          int   n, col, pop;
-          int   args;
+          StrBuf  temp[1];
+          int     n, col, pop;
+          int     args;
 
 
-          sprintf( temp, "%78c\n", ' ' );
+          strbuf_init( temp, tempStr, sizeof ( tempStr ) );
 
           /* first letter of location */
           switch ( CUR.curRange )
           {
           case tt_coderange_glyph:
-            temp[0] = 'g';
+            strbuf_addc( temp, 'g' );
             break;
 
           case tt_coderange_cvt:
-            temp[0] = 'c';
+            strbuf_addc( temp, 'c' );
             break;
 
           default:
-            temp[0] = 'f';
+            strbuf_addc( temp, 'f' );
           }
 
           /* current IP */
-          sprintf( temp + 1, "%04lx: %02x  %-36.36s",
-                             CUR.IP,
-                             CUR.opcode,
-                             Cur_U_Line( &CUR ) );
+          strbuf_format( temp, "%04lx: %02x  %-36.36s",
+                         CUR.IP,
+                         CUR.opcode,
+                         Cur_U_Line( &CUR ) );
 
-          strncpy( temp + 46, " (", 3 );
+          strbuf_add( temp, " (" );
 
           args = CUR.top - 1;
           pop  = Pop_Push_Count[CUR.opcode] >> 4;
@@ -2123,7 +2109,12 @@
 
 
             if ( pop == 0 )
-              temp[col - 1] = temp[col - 1] == '(' ? ' ' : ')';
+            {
+              char* last = strbuf_back( temp );
+
+
+              *last = ( *last == '(' ) ? ' ' : ')';
+            }
 
             if ( args >= 0 )
             {
@@ -2134,13 +2125,12 @@
               {
                 /* we display signed hexadecimal numbers, which */
                 /* is easier to read and needs less space       */
-                num_chars = sprintf( temp + col, "%s%04lx",
-                                                 val < 0 ? "-" : "",
-                                                 val < 0 ? -val : val );
+                num_chars = strbuf_format( temp, "%s%04lx",
+                                           val < 0 ? "-" : "",
+                                           val < 0 ? -val : val );
               }
               else
-                num_chars = sprintf( temp + col, "%ld",
-                                                 val );
+                num_chars = strbuf_format( temp, "%ld", val );
 
               if ( col + num_chars >= 78 )
                 break;
@@ -2148,19 +2138,18 @@
             else
               num_chars = 0;
 
-            temp[col + num_chars] = ' ';
-            col                  += num_chars + 1;
+            strbuf_addc( temp, ' ' );
+            col += num_chars + 1;
 
             pop--;
             args--;
           }
 
           for ( n = col; n < 78; n++ )
-            temp[n] = ' ';
+            strbuf_addc( temp, ' ' );
 
-          temp[78] = '\n';
-          temp[79] = '\0';
-          printf( "%s", temp );
+          strbuf_addc( temp, '\n' );
+          printf( "%s", strbuf_value( temp ) );
         }
 
         /* First, check for empty stack and overflow */



reply via email to

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