From 8ef8072ba151dc06214ee70985a7fb03ebc2932f Mon Sep 17 00:00:00 2001 From: Werner Lemberg Date: Tue, 19 Oct 2021 22:59:46 +0200 Subject: [PATCH] [bdf, cid, pfr, winfonts] Improve rejection of other font formats. This is mainly for better diagnostics of malformed fonts. * src/bdf/bdflib.c (_bfd_readstream): Stop font format testing if the first input line is too long or doesn't end with `\r` or `\n`. * src/cid/cidparse.c (cid_parser_new): Don't handle too short input as an error but as an unknown format. * src/pfr/pfrobjs.c (pfr_face_init): Ditto. * src/winfonts/winfnt.c (fnt_font_load, fnt_face_get_dll_font): Ditto. --- src/bdf/bdflib.c | 21 ++++++++++++++++----- src/cid/cidparse.c | 4 ++++ src/pfr/pfrobjs.c | 4 ++++ src/winfonts/winfnt.c | 7 +++++++ 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/bdf/bdflib.c b/src/bdf/bdflib.c index 82272ced6..b65c8a2f3 100644 --- a/src/bdf/bdflib.c +++ b/src/bdf/bdflib.c @@ -581,8 +581,14 @@ /* or even resizing it */ if ( end >= avail ) { - if ( bytes == 0 ) /* last line in file doesn't end in \r or \n */ - break; /* ignore it then exit */ + if ( bytes == 0 ) + { + /* last line in file doesn't end in \r or \n; */ + /* ignore it then exit */ + if ( lineno == 1 ) + error = FT_THROW( Missing_Startfont_Field ); + break; + } if ( start == 0 ) { @@ -593,8 +599,13 @@ if ( buf_size >= 65536UL ) /* limit ourselves to 64KByte */ { - FT_ERROR(( "_bdf_readstream: " ERRMSG6, lineno )); - error = FT_THROW( Invalid_Argument ); + if ( lineno == 1 ) + error = FT_THROW( Missing_Startfont_Field ); + else + { + FT_ERROR(( "_bdf_readstream: " ERRMSG6, lineno )); + error = FT_THROW( Invalid_Argument ); + } goto Exit; } @@ -2169,7 +2180,7 @@ unsigned long lineno = 0; /* make compiler happy */ _bdf_parse_t *p = NULL; - FT_Error error = FT_Err_Ok; + FT_Error error = FT_Err_Ok; if ( FT_NEW( p ) ) diff --git a/src/cid/cidparse.c b/src/cid/cidparse.c index 96ca1c350..852c9b6b9 100644 --- a/src/cid/cidparse.c +++ b/src/cid/cidparse.c @@ -73,7 +73,11 @@ /* first of all, check the font format in the header */ if ( FT_FRAME_ENTER( 31 ) ) + { + FT_TRACE2(( " not a CID-keyed font\n" )); + error = FT_THROW( Unknown_File_Format ); goto Exit; + } if ( ft_strncmp( (char *)stream->cursor, "%!PS-Adobe-3.0 Resource-CIDFont", 31 ) ) diff --git a/src/pfr/pfrobjs.c b/src/pfr/pfrobjs.c index 5302fac4c..3080cb650 100644 --- a/src/pfr/pfrobjs.c +++ b/src/pfr/pfrobjs.c @@ -83,7 +83,11 @@ /* load the header and check it */ error = pfr_header_load( &face->header, stream ); if ( error ) + { + FT_TRACE2(( " not a PFR font\n" )); + error = FT_THROW( Unknown_File_Format ); goto Exit; + } if ( !pfr_header_check( &face->header ) ) { diff --git a/src/winfonts/winfnt.c b/src/winfonts/winfnt.c index 9c4b13ed9..b4fabad28 100644 --- a/src/winfonts/winfnt.c +++ b/src/winfonts/winfnt.c @@ -217,7 +217,11 @@ /* first of all, read the FNT header */ if ( FT_STREAM_SEEK( font->offset ) || FT_STREAM_READ_FIELDS( winfnt_header_fields, header ) ) + { + FT_TRACE2(( " not a Windows FNT file\n" )); + error = FT_THROW( Unknown_File_Format ); goto Exit; + } /* check header */ if ( header->version != 0x200 && @@ -284,7 +288,10 @@ /* does it begin with an MZ header? */ if ( FT_STREAM_SEEK( 0 ) || FT_STREAM_READ_FIELDS( winmz_header_fields, &mz_header ) ) + { + error = FT_ERR( Unknown_File_Format ); goto Exit; + } error = FT_ERR( Unknown_File_Format ); if ( mz_header.magic == WINFNT_MZ_MAGIC )