Commit c0f9c4aaddcf0a4078e8ce87808ff94b7dba377d

David Turner 2007-02-12T14:55:03

introduce ft_mem_dup, ft_mem_strdup and ft_mem_strcpyn, and the corresponding macros to use them (e.g. FT_STRDUP, FT_DUP and FT_STRCPYN) modify the code to use them instead of raw mallocs/strcpy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
diff --git a/ChangeLog b/ChangeLog
index a84e9f9..5664b25 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2007-02-12  David Turner  <david@freetype.org>
+
+	* include/freetype/internal/ftmemory.h, src/base/ftutils.c,
+	src/bfd/bfddrivr.c, src/bdf/bdflib.c, src/pcf/pcfread.c,
+	src/cff/cffdrivr.c, src/cff/cffload.c, src/cff/cffobjs.c,
+	src/sfnt/sfdriver.c, src/type1/t1driver.c, src/type42/t42drivr.c:
+	introduce ft_mem_strdup, ft_mem_dup, ft_mem_strcpyn and the
+	corresponding macros, and modify code to use them. This is to
+	get rid of various uses of strcpy and other "evil" functions,
+	as well as simplify a few things
+
+	
 2007-02-11  Werner Lemberg  <wl@gnu.org>
 
 	* src/autofit/afloader.c (af_loader_load_g): Don't change width for
@@ -66,6 +78,7 @@
 	(gxv_mort_subtable_type1_substTable_validate): Fix debugging
 	message.
 
+>>>>>>> 1.1514
 2007-01-31  Werner Lemberg  <wl@gnu.org>
 
 
diff --git a/builds/mac/ftmac.c b/builds/mac/ftmac.c
index 5a7c0cd..0f2c27d 100644
--- a/builds/mac/ftmac.c
+++ b/builds/mac/ftmac.c
@@ -441,7 +441,12 @@
 
     while ( 1 )
     {
-      q = p + FT_MIN( 255, ft_strlen( p ) );
+      int  len = ft_strlen( p );
+
+      if (len > 255)
+        len = 255;
+
+      q = p + len;
 
       if ( q == p )
         return 0;
diff --git a/include/freetype/internal/ftmemory.h b/include/freetype/internal/ftmemory.h
index 6bfeb9e..f52c9d8 100644
--- a/include/freetype/internal/ftmemory.h
+++ b/include/freetype/internal/ftmemory.h
@@ -322,6 +322,38 @@ FT_BEGIN_HEADER
 #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
 
 
+  FT_BASE( FT_Pointer )
+  ft_mem_strdup( FT_Memory    memory,
+                 const char*  str,
+                 FT_Error    *p_error );
+
+  FT_BASE( FT_Pointer )
+  ft_mem_dup( FT_Memory    memory,
+              const void*  address,
+              FT_ULong     size,
+              FT_Error    *p_error );
+
+#define  FT_MEM_STRDUP(dst,str)    \
+    (dst) = ft_mem_strdup( memory, (const char*)(str), &error )
+
+#define  FT_STRDUP(dst,str)   \
+    FT_MEM_SET_ERROR( FT_MEM_STRDUP(dst,str) )
+
+#define  FT_MEM_DUP(dst, address,size)   \
+    (dst) = ft_mem_dup( memory, (address), (FT_ULong)(size), &error )
+
+#define  FT_DUP(dst,address,size)  \
+    FT_MEM_SET_ERROR( FT_MEM_DUP(dst,address,size) )
+
+  /* returns 1 or more if a trunction occured, 0 if the source string fitted the buffer */
+  /* this is *not* the same than the normal strlcpy() call                              */
+  FT_BASE( FT_Int )
+  ft_mem_strcpyn( char*        dst,
+                  const char*  src,
+                  FT_ULong     size );
+
+#define  FT_STRCPYN(dst,src,size)   ft_mem_strcpyn( (char*)dst, (const char*)(src), (FT_ULong)(size) )
+
  /* */
 
 
diff --git a/src/base/ftutil.c b/src/base/ftutil.c
index d76f5e5..eb6f6b1 100644
--- a/src/base/ftutil.c
+++ b/src/base/ftutil.c
@@ -172,6 +172,48 @@
   }
 
 
+  FT_BASE_DEF( FT_Pointer )
+  ft_mem_dup( FT_Memory    memory,
+              const void*  address,
+              FT_ULong     size,
+              FT_Error    *p_error )
+  {
+    FT_Error    error;
+    FT_Pointer  p = ft_mem_qalloc( memory, size, &error );
+
+    if (!error && address)
+      ft_memcpy( p, address, size );
+
+    *p_error = error;
+    return p;
+  }
+
+
+  FT_BASE_DEF( FT_Pointer )
+  ft_mem_strdup( FT_Memory    memory,
+                 const char*  str,
+                 FT_Error    *p_error )
+  {
+    FT_ULong  len = str ? (FT_ULong)ft_strlen(str)+1 : 0;
+
+    return ft_mem_dup( memory, str, len, p_error );
+  }
+
+
+  FT_BASE_DEF( FT_Int )
+  ft_mem_strcpyn( char*        dst,
+                  const char*  src,
+                  FT_ULong     size )
+  {
+    while ( size > 1 && *src != 0 )
+      *dst++ = *src++;
+
+    *dst = 0;  /* always zero-terminate */
+
+    return (*src != 0);
+  }
+
+
   /*************************************************************************/
   /*************************************************************************/
   /*************************************************************************/
diff --git a/src/bdf/bdfdrivr.c b/src/bdf/bdfdrivr.c
index 3407148..beaa267 100644
--- a/src/bdf/bdfdrivr.c
+++ b/src/bdf/bdfdrivr.c
@@ -194,11 +194,9 @@ THE SOFTWARE.
     bdf_font_t*      font   = bdf->bdffont;
     bdf_property_t*  prop;
 
-    char  *istr = NULL, *bstr = NULL;
-    char  *sstr = NULL, *astr = NULL;
-
-    int  parts = 0, len = 0;
-
+    int    nn, len;
+    char*  strings[4] = { NULL, NULL, NULL, NULL };
+    int    lengths[4];
 
     face->style_flags = 0;
 
@@ -209,11 +207,9 @@ THE SOFTWARE.
            *(prop->value.atom) == 'I' || *(prop->value.atom) == 'i' ) )
     {
       face->style_flags |= FT_STYLE_FLAG_ITALIC;
-      istr = ( *(prop->value.atom) == 'O' || *(prop->value.atom) == 'o' )
-               ? (char *)"Oblique"
-               : (char *)"Italic";
-      len += ft_strlen( istr );
-      parts++;
+      strings[2] = ( *(prop->value.atom) == 'O' || *(prop->value.atom) == 'o' )
+                   ? (char *)"Oblique"
+                   : (char *)"Italic";
     }
 
     prop = bdf_get_font_property( font, (char *)"WEIGHT_NAME" );
@@ -222,9 +218,7 @@ THE SOFTWARE.
          ( *(prop->value.atom) == 'B' || *(prop->value.atom) == 'b' ) )
     {
       face->style_flags |= FT_STYLE_FLAG_BOLD;
-      bstr = (char *)"Bold";
-      len += ft_strlen( bstr );
-      parts++;
+      strings[1] = (char *)"Bold";
     }
 
     prop = bdf_get_font_property( font, (char *)"SETWIDTH_NAME" );
@@ -232,9 +226,7 @@ THE SOFTWARE.
          prop->value.atom && *(prop->value.atom)                       &&
          !( *(prop->value.atom) == 'N' || *(prop->value.atom) == 'n' ) )
     {
-      sstr = (char *)(prop->value.atom);
-      len += ft_strlen( sstr );
-      parts++;
+      strings[3] = (char *)(prop->value.atom);
     }
 
     prop = bdf_get_font_property( font, (char *)"ADD_STYLE_NAME" );
@@ -242,60 +234,64 @@ THE SOFTWARE.
          prop->value.atom && *(prop->value.atom)                       &&
          !( *(prop->value.atom) == 'N' || *(prop->value.atom) == 'n' ) )
     {
-      astr = (char *)(prop->value.atom);
-      len += ft_strlen( astr );
-      parts++;
+      strings[0] = (char *)(prop->value.atom);
     }
 
-    if ( !parts || !len )
+    len   = 0;
+
+    for (len = 0, nn = 0; nn < 4; nn++)
     {
-      if ( FT_ALLOC( face->style_name, ft_strlen( "Regular" ) + 1 ) )
-        return error;
+      lengths[nn] = 0;
+      if (strings[nn]) 
+      {
+        lengths[nn] = ft_strlen(strings[nn]);
+        len        += lengths[nn]+1;
+      }
+    }
 
-      ft_strcpy( face->style_name, "Regular" );
+    if ( len == 0 )
+    {
+      strings[0] = "Regular";
+      lengths[0] = ft_strlen(strings[0]);
+      len        = lengths[0]+1;
     }
-    else
+
     {
-      char          *style, *s;
-      unsigned int  i;
+      char*  s;
 
 
-      if ( FT_ALLOC( style, len + parts ) )
+      if ( FT_ALLOC( face->style_name, len ) )
         return error;
 
-      s = style;
+      s = face->style_name;
 
-      if ( astr )
-      {
-        ft_strcpy( s, astr );
-        for ( i = 0; i < ft_strlen( astr ); i++, s++ )
-          if ( *s == ' ' )
-            *s = '-';                     /* replace spaces with dashes */
-        *(s++) = ' ';
-      }
-      if ( bstr )
+      for (nn = 0; nn < 4; nn++)
       {
-        ft_strcpy( s, bstr );
-        s += ft_strlen( bstr );
-        *(s++) = ' ';
-      }
-      if ( istr )
-      {
-        ft_strcpy( s, istr );
-        s += ft_strlen( istr );
-        *(s++) = ' ';
-      }
-      if ( sstr )
-      {
-        ft_strcpy( s, sstr );
-        for ( i = 0; i < ft_strlen( sstr ); i++, s++ )
-          if ( *s == ' ' )
-            *s = '-';                     /* replace spaces with dashes */
-        *(s++) = ' ';
-      }
-      *(--s) = '\0';        /* overwrite last ' ', terminate the string */
+        char*  src = strings[nn];
+        int    len = lengths[nn];
+
+        if ( src == NULL )
+          continue;
+
+        /* separate elements with a space */
+        if (s != face->style_name)
+          *s++ = ' ';
+
+        memcpy( s, src, len );
+
+        /* need to convert spaces to dashes for add_style_name and setwidth_name */
+        if (nn == 0 || nn == 3) 
+        {
+          int  mm;
 
-      face->style_name = style;                     /* allocated string */
+          for (mm = 0; mm < len; mm++)
+            if (s[mm] == ' ')
+              s[mm] = '-';
+        }
+
+        s += len;
+      }
+      *s = 0;
     }
 
     return error;
@@ -394,12 +390,8 @@ THE SOFTWARE.
       prop = bdf_get_font_property( font, "FAMILY_NAME" );
       if ( prop && prop->value.atom )
       {
-        int  l = ft_strlen( prop->value.atom ) + 1;
-
-
-        if ( FT_NEW_ARRAY( bdfface->family_name, l ) )
+        if ( FT_STRDUP( bdfface->family_name, prop->value.atom ) )
           goto Exit;
-        ft_strcpy( bdfface->family_name, prop->value.atom );
       }
       else
         bdfface->family_name = 0;
@@ -503,16 +495,10 @@ THE SOFTWARE.
             const char*  s;
 
 
-            if ( FT_NEW_ARRAY( face->charset_encoding,
-                               ft_strlen( charset_encoding->value.atom ) + 1 ) )
-              goto Exit;
-            if ( FT_NEW_ARRAY( face->charset_registry,
-                               ft_strlen( charset_registry->value.atom ) + 1 ) )
+            if ( FT_STRDUP( face->charset_encoding, charset_encoding->value.atom ) ||
+                 FT_STRDUP( face->charset_registry, charset_registry->value.atom ) )
               goto Exit;
 
-            ft_strcpy( face->charset_registry, charset_registry->value.atom );
-            ft_strcpy( face->charset_encoding, charset_encoding->value.atom );
-
             /* Uh, oh, compare first letters manually to avoid dependency
                on locales. */
             s = face->charset_registry;
diff --git a/src/bdf/bdflib.c b/src/bdf/bdflib.c
index fa3dd2f..7737e54 100644
--- a/src/bdf/bdflib.c
+++ b/src/bdf/bdflib.c
@@ -1253,7 +1253,6 @@
   {
     unsigned long   propid;
     hashnode        hn;
-    int             len;
     bdf_property_t  *prop, *fp;
     FT_Memory       memory = font->memory;
     FT_Error        error = BDF_Err_Ok;
@@ -1272,19 +1271,11 @@
         /* Delete the current atom if it exists. */
         FT_FREE( fp->value.atom );
 
-        if ( value == 0 )
-          len = 1;
-        else
-          len = ft_strlen( value ) + 1;
-
-        if ( len > 1 )
+        if ( value && value[0] != 0 )
         {
-          if ( FT_NEW_ARRAY( fp->value.atom, len ) )
+          if ( FT_STRDUP( fp->value.atom, value ) )
             goto Exit;
-          FT_MEM_COPY( fp->value.atom, value, len );
         }
-        else
-          fp->value.atom = 0;
         break;
 
       case BDF_INTEGER:
@@ -1349,19 +1340,12 @@
     switch ( prop->format )
     {
     case BDF_ATOM:
-      if ( value == 0 )
-        len = 1;
-      else
-        len = ft_strlen( value ) + 1;
-
-      if ( len > 1 )
+      fp->value.atom = 0;
+      if ( value != 0 && value[0] )
       {
-        if ( FT_NEW_ARRAY( fp->value.atom, len ) )
+        if ( FT_STRDUP( fp->value.atom, value ) )
           goto Exit;
-        FT_MEM_COPY( fp->value.atom, value, len );
       }
-      else
-        fp->value.atom = 0;
       break;
 
     case BDF_INTEGER:
diff --git a/src/cff/cffdrivr.c b/src/cff/cffdrivr.c
index 8b67ce2..952e88e 100644
--- a/src/cff/cffdrivr.c
+++ b/src/cff/cffdrivr.c
@@ -220,17 +220,8 @@
     /* now, lookup the name itself */
     gname = cff_index_get_sid_string( &font->string_index, sid, psnames );
 
-    if ( gname && buffer_max > 0 )
-    {
-      FT_UInt  len = (FT_UInt)ft_strlen( gname );
-
-
-      if ( len >= buffer_max )
-        len = buffer_max - 1;
-
-      FT_MEM_COPY( buffer, gname, len );
-      ((FT_Byte*)buffer)[len] = 0;
-    }
+    if ( gname )
+      FT_STRCPYN( buffer, gname, buffer_max );
 
     FT_FREE( gname );
     error = CFF_Err_Ok;
diff --git a/src/cff/cffload.c b/src/cff/cffload.c
index c8f7963..dd2f113 100644
--- a/src/cff/cffload.c
+++ b/src/cff/cffload.c
@@ -570,7 +570,6 @@
     {
       FT_String*   name       = 0;
       const char*  adobe_name = psnames->adobe_std_strings( sid );
-      FT_UInt      len;
 
 
       if ( adobe_name )
@@ -579,12 +578,7 @@
         FT_Error   error;
 
 
-        len = (FT_UInt)ft_strlen( adobe_name );
-        if ( !FT_ALLOC( name, len + 1 ) )
-        {
-          FT_MEM_COPY( name, adobe_name, len );
-          name[len] = 0;
-        }
+        (void)FT_STRDUP( name, adobe_name );
 
         FT_UNUSED( error );
       }
diff --git a/src/cff/cffobjs.c b/src/cff/cffobjs.c
index eb6159f..6e6f839 100644
--- a/src/cff/cffobjs.c
+++ b/src/cff/cffobjs.c
@@ -285,17 +285,9 @@
               const FT_String*  source )
   {
     FT_Error    error;
-    FT_String*  result = 0;
-    FT_Int      len = (FT_Int)ft_strlen( source );
+    FT_String*  result;
 
-
-    if ( !FT_ALLOC( result, len + 1 ) )
-    {
-      FT_MEM_COPY( result, source, len );
-      result[len] = 0;
-    }
-
-    FT_UNUSED( error );
+    result = ft_mem_strdup(memory, source, &error);
 
     return result;
   }
diff --git a/src/pcf/pcfread.c b/src/pcf/pcfread.c
index 12ff5b3..1a2064f 100644
--- a/src/pcf/pcfread.c
+++ b/src/pcf/pcfread.c
@@ -513,10 +513,8 @@ THE SOFTWARE.
         goto Bail;
       }
 
-      if ( FT_NEW_ARRAY( properties[i].name,
-                         ft_strlen( strings + name_offset ) + 1 ) )
+      if ( FT_STRDUP( properties[i].name, strings + name_offset ) )
         goto Bail;
-      ft_strcpy( properties[i].name, strings + name_offset );
 
       FT_TRACE4(( "  %s:", properties[i].name ));
 
@@ -534,10 +532,8 @@ THE SOFTWARE.
           goto Bail;
         }
 
-        if ( FT_NEW_ARRAY( properties[i].value.atom,
-                           ft_strlen( strings + value_offset ) + 1 ) )
+        if ( FT_STRDUP( properties[i].value.atom, strings + value_offset ) )
           goto Bail;
-        ft_strcpy( properties[i].value.atom, strings + props[i].value );
 
         FT_TRACE4(( " `%s'\n", properties[i].value.atom ));
       }
@@ -993,10 +989,9 @@ THE SOFTWARE.
 
     PCF_Property  prop;
 
-    char  *istr = NULL, *bstr = NULL;
-    char  *sstr = NULL, *astr = NULL;
-
-    int  parts = 0, len = 0;
+    int    nn, len;
+    char*  strings[4] = { NULL, NULL, NULL, NULL };
+    int    lengths[4];
 
 
     face->style_flags = 0;
@@ -1007,11 +1002,9 @@ THE SOFTWARE.
            *(prop->value.atom) == 'I' || *(prop->value.atom) == 'i' ) )
     {
       face->style_flags |= FT_STYLE_FLAG_ITALIC;
-      istr = ( *(prop->value.atom) == 'O' || *(prop->value.atom) == 'o' )
-               ? (char *)"Oblique"
-               : (char *)"Italic";
-      len += ft_strlen( istr );
-      parts++;
+      strings[2] = ( *(prop->value.atom) == 'O' || *(prop->value.atom) == 'o' )
+          ? (char *)"Oblique"
+        : (char *)"Italic";
     }
 
     prop = pcf_find_property( pcf, "WEIGHT_NAME" );
@@ -1019,9 +1012,7 @@ THE SOFTWARE.
          ( *(prop->value.atom) == 'B' || *(prop->value.atom) == 'b' ) )
     {
       face->style_flags |= FT_STYLE_FLAG_BOLD;
-      bstr = (char *)"Bold";
-      len += ft_strlen( bstr );
-      parts++;
+      strings[1] = (char *)"Bold";
     }
 
     prop = pcf_find_property( pcf, "SETWIDTH_NAME" );
@@ -1029,9 +1020,7 @@ THE SOFTWARE.
          *(prop->value.atom)                                           &&
          !( *(prop->value.atom) == 'N' || *(prop->value.atom) == 'n' ) )
     {
-      sstr = (char *)(prop->value.atom);
-      len += ft_strlen( sstr );
-      parts++;
+      strings[3] = (char *)(prop->value.atom);
     }
 
     prop = pcf_find_property( pcf, "ADD_STYLE_NAME" );
@@ -1039,60 +1028,62 @@ THE SOFTWARE.
          *(prop->value.atom)                                           &&
          !( *(prop->value.atom) == 'N' || *(prop->value.atom) == 'n' ) )
     {
-      astr = (char *)(prop->value.atom);
-      len += ft_strlen( astr );
-      parts++;
+      strings[0] = (char *)(prop->value.atom);
     }
 
-    if ( !parts || !len )
+    for (len = 0, nn = 0; nn < 4; nn++)
     {
-      if ( FT_ALLOC( face->style_name, 8 ) )
-        return error;
-      ft_strcpy( face->style_name, "Regular" );
-      face->style_name[7] = '\0';
+      lengths[nn] = 0;
+      if (strings[nn]) 
+      {
+        lengths[nn] = ft_strlen(strings[nn]);
+        len        += lengths[nn]+1;
+      }
     }
-    else
+
+    if ( len == 0 )
     {
-      char          *style, *s;
-      unsigned int  i;
+      strings[0] = "Regular";
+      lengths[0] = ft_strlen(strings[0]);
+      len        = lengths[0]+1;
+    }
 
+    {
+      char*  s;
 
-      if ( FT_ALLOC( style, len + parts ) )
+
+      if ( FT_ALLOC( face->style_name, len ) )
         return error;
 
-      s = style;
+      s = face->style_name;
 
-      if ( astr )
-      {
-        ft_strcpy( s, astr );
-        for ( i = 0; i < ft_strlen( astr ); i++, s++ )
-          if ( *s == ' ' )
-            *s = '-';                     /* replace spaces with dashes */
-        *(s++) = ' ';
-      }
-      if ( bstr )
-      {
-        ft_strcpy( s, bstr );
-        s += ft_strlen( bstr );
-        *(s++) = ' ';
-      }
-      if ( istr )
-      {
-        ft_strcpy( s, istr );
-        s += ft_strlen( istr );
-        *(s++) = ' ';
-      }
-      if ( sstr )
+      for (nn = 0; nn < 4; nn++)
       {
-        ft_strcpy( s, sstr );
-        for ( i = 0; i < ft_strlen( sstr ); i++, s++ )
-          if ( *s == ' ' )
-            *s = '-';                     /* replace spaces with dashes */
-        *(s++) = ' ';
-      }
-      *(--s) = '\0';        /* overwrite last ' ', terminate the string */
+        char*  src = strings[nn];
+        int    len = lengths[nn];
+
+        if ( src == NULL )
+          continue;
+
+        /* separate elements with a space */
+        if (s != face->style_name)
+          *s++ = ' ';
+
+        memcpy( s, src, len );
+
+        /* need to convert spaces to dashes for add_style_name and setwidth_name */
+        if (nn == 0 || nn == 3) 
+        {
+          int  mm;
+
+          for (mm = 0; mm < len; mm++)
+            if (s[mm] == ' ')
+              s[mm] = '-';
+        }
 
-      face->style_name = style;                     /* allocated string */
+        s += len;
+      }
+      *s = 0;
     }
 
     return error;
@@ -1173,12 +1164,8 @@ THE SOFTWARE.
       prop = pcf_find_property( face, "FAMILY_NAME" );
       if ( prop && prop->isString )
       {
-        int  l = ft_strlen( prop->value.atom ) + 1;
-
-
-        if ( FT_NEW_ARRAY( root->family_name, l ) )
+        if ( FT_STRDUP( root->family_name, prop->value.atom ) )
           goto Exit;
-        ft_strcpy( root->family_name, prop->value.atom );
       }
       else
         root->family_name = NULL;
@@ -1256,16 +1243,9 @@ THE SOFTWARE.
         if ( charset_registry && charset_registry->isString &&
              charset_encoding && charset_encoding->isString )
         {
-          if ( FT_NEW_ARRAY( face->charset_encoding,
-                             ft_strlen( charset_encoding->value.atom ) + 1 ) )
+          if ( FT_STRDUP( face->charset_encoding, charset_encoding->value.atom ) ||
+               FT_STRDUP( face->charset_registry, charset_registry->value.atom ) )
             goto Exit;
-
-          if ( FT_NEW_ARRAY( face->charset_registry,
-                             ft_strlen( charset_registry->value.atom ) + 1 ) )
-            goto Exit;
-
-          ft_strcpy( face->charset_registry, charset_registry->value.atom );
-          ft_strcpy( face->charset_encoding, charset_encoding->value.atom );
         }
       }
     }
diff --git a/src/sfnt/sfdriver.c b/src/sfnt/sfdriver.c
index 8709327..103e224 100644
--- a/src/sfnt/sfdriver.c
+++ b/src/sfnt/sfdriver.c
@@ -144,17 +144,8 @@
 
 
     error = tt_face_get_ps_name( face, glyph_index, &gname );
-    if ( !error && buffer_max > 0 )
-    {
-      FT_UInt  len = (FT_UInt)( ft_strlen( gname ) );
-
-
-      if ( len >= buffer_max )
-        len = buffer_max - 1;
-
-      FT_MEM_COPY( buffer, gname, len );
-      ((FT_Byte*)buffer)[len] = 0;
-    }
+    if ( !error )
+      FT_STRCPYN( buffer, gname, buffer_max );
 
     return error;
   }
diff --git a/src/type1/t1driver.c b/src/type1/t1driver.c
index 806d35d..56545da 100644
--- a/src/type1/t1driver.c
+++ b/src/type1/t1driver.c
@@ -59,23 +59,7 @@
                      FT_Pointer  buffer,
                      FT_UInt     buffer_max )
   {
-    FT_String*  gname;
-
-
-    gname = face->type1.glyph_names[glyph_index];
-
-    if ( buffer_max > 0 )
-    {
-      FT_UInt  len = (FT_UInt)( ft_strlen( gname ) );
-
-
-      if (len >= buffer_max)
-        len = buffer_max - 1;
-
-      FT_MEM_COPY( buffer, gname, len );
-      ((FT_Byte*)buffer)[len] = 0;
-    }
-
+    FT_STRCPYN( buffer, face->type1.glyph_names[glyph_index], buffer_max );
     return T1_Err_Ok;
   }
 
diff --git a/src/type42/t42drivr.c b/src/type42/t42drivr.c
index 82eb0ba..4bba8f7 100644
--- a/src/type42/t42drivr.c
+++ b/src/type42/t42drivr.c
@@ -61,23 +61,7 @@
                       FT_Pointer  buffer,
                       FT_UInt     buffer_max )
   {
-    FT_String*  gname;
-
-
-    gname = face->type1.glyph_names[glyph_index];
-
-    if ( buffer_max > 0 )
-    {
-      FT_UInt  len = (FT_UInt)( ft_strlen( gname ) );
-
-
-      if ( len >= buffer_max )
-        len = buffer_max - 1;
-
-      FT_MEM_COPY( buffer, gname, len );
-      ((FT_Byte*)buffer)[len] = 0;
-    }
-
+    FT_STRCPYN( buffer, face->type1.glyph_names[glyph_index], buffer_max );
     return T42_Err_Ok;
   }
 
@@ -94,7 +78,7 @@
     {
       gname = face->type1.glyph_names[i];
 
-      if ( !ft_strcmp( glyph_name, gname ) )
+      if ( glyph_name[0] == gname[0] && !ft_strcmp( glyph_name, gname ) )
         return (FT_UInt)ft_atol( (const char *)face->type1.charstrings[i] );
     }