From 95120928592c4d977abbef3b6582eae12b4058c2 Mon Sep 17 00:00:00 2001
From: Werner Lemberg <wl@gnu.org>
Date: Sat, 8 Nov 2008 07:20:25 +0000
Subject: [PATCH] * src/sfnt/sfobjs.c (tt_face_get_name): Modified to return an
 error code so that memory allocation problems can be distinguished from
 missing table entries.  Reported by Graham Asher. (GET_NAME): New macro.
 (sfnt_load_face): Use it.

---
 ChangeLog         |  8 ++++++
 src/sfnt/sfobjs.c | 64 ++++++++++++++++++++++++-----------------------
 src/sfnt/ttpost.c |  9 ++++---
 3 files changed, 46 insertions(+), 35 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index eabc5de47..aec026e86 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2008-11-08  Werner Lemberg  <wl@gnu.org>
+
+	* src/sfnt/sfobjs.c (tt_face_get_name): Modified to return an error
+	code so that memory allocation problems can be distinguished from
+	missing table entries.  Reported by Graham Asher.
+	(GET_NAME): New macro.
+	(sfnt_load_face): Use it.
+
 2008-11-05  Werner Lemberg  <wl@gnu.org>
 
 	* devel/ftoption.h, include/freetype/config/ftoption.h
diff --git a/src/sfnt/sfobjs.c b/src/sfnt/sfobjs.c
index 3333aa62f..19a757141 100644
--- a/src/sfnt/sfobjs.c
+++ b/src/sfnt/sfobjs.c
@@ -123,14 +123,20 @@
   /*                                                                       */
   /*    nameid :: The name id of the name record to return.                */
   /*                                                                       */
-  /* <Return>                                                              */
-  /*    Character string.  NULL if no name is present.                     */
+  /* <InOut>                                                               */
+  /*    name   :: The address of a string pointer.  NULL if no name is     */
+  /*              present.                                                 */
   /*                                                                       */
-  static FT_String*
-  tt_face_get_name( TT_Face    face,
-                    FT_UShort  nameid )
+  /* <Return>                                                              */
+  /*    FreeType error code.  0 means success.                             */
+  /*                                                                       */
+  static FT_Error
+  tt_face_get_name( TT_Face      face,
+                    FT_UShort    nameid,
+                    FT_String**  name )
   {
     FT_Memory         memory = face->root.memory;
+    FT_Error          error  = SFNT_Err_Ok;
     FT_String*        result = NULL;
     FT_UShort         n;
     TT_NameEntryRec*  rec;
@@ -145,6 +151,8 @@
     TT_NameEntry_ConvertFunc  convert;
 
 
+    FT_ASSERT( name );
+
     rec = face->name_table.names;
     for ( n = 0; n < face->num_names; n++, rec++ )
     {
@@ -256,11 +264,8 @@
     {
       if ( rec->string == NULL )
       {
-        FT_Error   error  = SFNT_Err_Ok;
         FT_Stream  stream = face->name_table.stream;
 
-        FT_UNUSED( error );
-
 
         if ( FT_QNEW_ARRAY ( rec->string, rec->stringLength ) ||
              FT_STREAM_SEEK( rec->stringOffset )              ||
@@ -277,7 +282,8 @@
     }
 
   Exit:
-    return result;
+    *name = result;
+    return error;
   }
 
 
@@ -499,6 +505,13 @@
     FT_TRACE3(( "\n" ));                                      \
   } while ( 0 )
 
+#define GET_NAME( id, field )                                 \
+  do {                                                        \
+    error = tt_face_get_name( face, TT_NAME_ID_##id, field ); \
+    if ( error )                                              \
+      goto Exit;                                              \
+  } while ( 0 )
+
 
   FT_LOCAL_DEF( FT_Error )
   sfnt_load_face( FT_Stream      stream,
@@ -699,8 +712,6 @@
     LOAD_( gasp );
     LOAD_( kern );
 
-    error = SFNT_Err_Ok;
-
     face->root.num_glyphs = face->max_profile.numGlyphs;
 
 #if 0
@@ -713,17 +724,13 @@
     if ( face->os2.version != 0xFFFFU && face->os2.fsSelection & 256 )
 #endif
     {
-      face->root.family_name =
-        tt_face_get_name( face, TT_NAME_ID_PREFERRED_FAMILY );
+      GET_NAME( PREFERRED_FAMILY, &face->root.family_name );
       if ( !face->root.family_name )
-        face->root.family_name =
-          tt_face_get_name( face, TT_NAME_ID_FONT_FAMILY );
+        GET_NAME( FONT_FAMILY, &face->root.family_name );
 
-      face->root.style_name =
-        tt_face_get_name( face, TT_NAME_ID_PREFERRED_SUBFAMILY );
+      GET_NAME( PREFERRED_SUBFAMILY, &face->root.style_name );
       if ( !face->root.style_name )
-        face->root.style_name =
-          tt_face_get_name( face, TT_NAME_ID_FONT_SUBFAMILY );
+        GET_NAME( FONT_SUBFAMILY, &face->root.style_name );
     }
 #if 0
     else
@@ -732,23 +739,17 @@
       /* subfamily) is still under consideration by Microsoft and */
       /* not implemented in the current version of WPF.           */
 
-      face->root.family_name =
-        tt_face_get_name( face, TT_NAME_ID_WWS_FAMILY );
+      GET_NAME( WWS_FAMILY, &face->root.family_name );
       if ( !face->root.family_name )
-        face->root.family_name =
-          tt_face_get_name( face, TT_NAME_ID_PREFERRED_FAMILY );
+        GET_NAME( PREFERRED_FAMILY, &face->root.family_name );
       if ( !face->root.family_name )
-        face->root.family_name =
-          tt_face_get_name( face, TT_NAME_ID_FONT_FAMILY );
+        GET_NAME( FONT_FAMILY, &face->root.family_name );
 
-      face->root.style_name =
-        tt_face_get_name( face, TT_NAME_ID_WWS_SUBFAMILY );
+      GET_NAME( WWS_SUBFAMILY, &face->root.style_name );
       if ( !face->root.style_name )
-        face->root.style_name =
-          tt_face_get_name( face, TT_NAME_ID_PREFERRED_SUBFAMILY );
+        GET_NAME( PREFERRED_SUBFAMILY, &face->root.style_name );
       if ( !face->root.style_name )
-        face->root.style_name =
-          tt_face_get_name( face, TT_NAME_ID_FONT_SUBFAMILY );
+        GET_NAME( FONT_SUBFAMILY, &face->root.style_name );
     }
 #endif
 
@@ -1032,6 +1033,7 @@
 
 #undef LOAD_
 #undef LOADM_
+#undef GET_NAME
 
 
   FT_LOCAL_DEF( void )
diff --git a/src/sfnt/ttpost.c b/src/sfnt/ttpost.c
index f434cfa9f..ce628e210 100644
--- a/src/sfnt/ttpost.c
+++ b/src/sfnt/ttpost.c
@@ -416,13 +416,14 @@
   /*    tt_face_get_ps_name                                                */
   /*                                                                       */
   /* <Description>                                                         */
-  /*    Gets the PostScript glyph name of a glyph.                         */
+  /*    Get the PostScript glyph name of a glyph.                          */
   /*                                                                       */
   /* <Input>                                                               */
   /*    face   :: A handle to the parent face.                             */
   /*                                                                       */
   /*    idx    :: The glyph index.                                         */
   /*                                                                       */
+  /* <InOut>                                                               */
   /*    PSname :: The address of a string pointer.  Will be NULL in case   */
   /*              of error, otherwise it is a pointer to the glyph name.   */
   /*                                                                       */
@@ -436,9 +437,9 @@
                        FT_UInt      idx,
                        FT_String**  PSname )
   {
-    FT_Error         error;
-    TT_Post_Names    names;
-    FT_Fixed         format;
+    FT_Error       error;
+    TT_Post_Names  names;
+    FT_Fixed       format;
 
 #ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
     FT_Service_PsCMaps  psnames;