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
This commit is contained in:
David Turner 2007-02-12 14:55:03 +00:00
parent fe33408f9a
commit c0f9c4aadd
13 changed files with 222 additions and 244 deletions

View File

@ -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>

View File

@ -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;

View File

@ -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) )
/* */

View File

@ -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);
}
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/

View File

@ -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 )
{
if ( FT_ALLOC( face->style_name, ft_strlen( "Regular" ) + 1 ) )
return error;
len = 0;
ft_strcpy( face->style_name, "Regular" );
for (len = 0, nn = 0; nn < 4; nn++)
{
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 )
for (nn = 0; nn < 4; nn++)
{
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 )
{
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];
face->style_name = style; /* allocated string */
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] = '-';
}
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,15 +495,9 @@ THE SOFTWARE.
const char* s;
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 );
/* Uh, oh, compare first letters manually to avoid dependency
on locales. */

View File

@ -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:

View File

@ -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;

View File

@ -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 );
}

View File

@ -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;
}

View File

@ -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 )
for (nn = 0; nn < 4; nn++)
{
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 )
{
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];
face->style_name = style; /* allocated string */
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] = '-';
}
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 );
}
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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] );
}