@@ -178,10 +178,18 @@ void Read_name(struct TTFONT *font)
178178
179179 table_ptr = NULL ;
180180
181- /* Set default values to avoid future references to */
182- /* undefined pointers. */
183- font->PostName = font->FullName =
184- font->FamilyName = font->Version = font->Style = (char *)" unknown" ;
181+ /* Set default values to avoid future references to undefined
182+ * pointers. Allocate each of PostName, FullName, FamilyName,
183+ * Version, and Style separately so they can be freed safely. */
184+ for (char **ptr = &(font->PostName ); ptr != NULL ; ) {
185+ *ptr = (char *) calloc (sizeof (char ), strlen (" unknown" )+1 );
186+ strcpy (*ptr, " unknown" );
187+ if (ptr == &(font->PostName )) ptr = &(font->FullName );
188+ else if (ptr == &(font->FullName )) ptr = &(font->FamilyName );
189+ else if (ptr == &(font->FamilyName )) ptr = &(font->Version );
190+ else if (ptr == &(font->Version )) ptr = &(font->Style );
191+ else ptr = NULL ;
192+ }
185193 font->Copyright = font->Trademark = (char *)NULL ;
186194
187195 table_ptr = GetTable (font, " name" ); /* pointer to table */
@@ -222,6 +230,7 @@ void Read_name(struct TTFONT *font)
222230 /* Font Family name */
223231 if ( platform == 1 && nameid == 1 )
224232 {
233+ free (font->FamilyName );
225234 font->FamilyName = (char *)calloc (sizeof (char ),length+1 );
226235 strncpy (font->FamilyName ,(const char *)strings+offset,length);
227236 font->FamilyName [length]=(char )NULL ;
@@ -237,6 +246,7 @@ void Read_name(struct TTFONT *font)
237246 /* Font Family name */
238247 if ( platform == 1 && nameid == 2 )
239248 {
249+ free (font->Style );
240250 font->Style = (char *)calloc (sizeof (char ),length+1 );
241251 strncpy (font->Style ,(const char *)strings+offset,length);
242252 font->Style [length]=(char )NULL ;
@@ -252,6 +262,7 @@ void Read_name(struct TTFONT *font)
252262 /* Full Font name */
253263 if ( platform == 1 && nameid == 4 )
254264 {
265+ free (font->FullName );
255266 font->FullName = (char *)calloc (sizeof (char ),length+1 );
256267 strncpy (font->FullName ,(const char *)strings+offset,length);
257268 font->FullName [length]=(char )NULL ;
@@ -267,6 +278,7 @@ void Read_name(struct TTFONT *font)
267278 /* Version string */
268279 if ( platform == 1 && nameid == 5 )
269280 {
281+ free (font->Version );
270282 font->Version = (char *)calloc (sizeof (char ),length+1 );
271283 strncpy (font->Version ,(const char *)strings+offset,length);
272284 font->Version [length]=(char )NULL ;
@@ -282,6 +294,7 @@ void Read_name(struct TTFONT *font)
282294 /* PostScript name */
283295 if ( platform == 1 && nameid == 6 )
284296 {
297+ free (font->PostName );
285298 font->PostName = (char *)calloc (sizeof (char ),length+1 );
286299 strncpy (font->PostName ,(const char *)strings+offset,length);
287300 font->PostName [length]=(char )NULL ;
0 commit comments