try to stop non-utf8 strings getting into metadata

still seems possible, strangely, investigate
This commit is contained in:
John Cupitt 2017-08-16 18:36:25 +01:00
parent 7134c64836
commit c4e8e31007
2 changed files with 38 additions and 13 deletions

View File

@ -7,6 +7,8 @@
* - from header.c
* 16/7/13
* - leakcheck VipsArea
* 16/8/17
* - validate strings as utf-8 on set
*/
/*
@ -507,17 +509,22 @@ transform_save_string_ref_string( const GValue *src_value, GValue *dest_value )
* @str: (transfer none): string to store
*
* Create a new refstring. These are reference-counted immutable strings, used
* to store string data in vips image metadata.
* to store string data in vips image metadata.
*
* Strings must be valid utf-8; use blob for binary data.
*
* See also: vips_area_unref().
*
* Returns: (transfer full): the new #VipsRefString.
* Returns: (transfer full): the new #VipsRefString, or NULL on error.
*/
VipsRefString *
vips_ref_string_new( const char *str )
{
VipsArea *area;
if( !g_utf8_validate( str, -1, NULL ) )
str = "<invalid utf-8 string>";
area = vips_area_new( (VipsCallbackFn) g_free, g_strdup( str ) );
/* Handy place to cache this.
@ -1402,12 +1409,17 @@ vips_value_get_save_string( const GValue *value )
* @str: C string to copy into the GValue
*
* Copies the C string into @value.
*
* @str should be a valid utf-8 string.
*/
void
vips_value_set_save_string( GValue *value, const char *str )
{
g_assert( G_VALUE_TYPE( value ) == VIPS_TYPE_SAVE_STRING );
if( !g_utf8_validate( str, -1, NULL ) )
str = "<invalid utf-8 string>";
g_value_set_boxed( value, str );
}
@ -1459,6 +1471,8 @@ vips_value_get_ref_string( const GValue *value, size_t *length )
* vips_ref_string are immutable C strings that are copied between images by
* copying reference-counted pointers, making them much more efficient than
* regular %GValue strings.
*
* @str should be a valid utf-8 string.
*/
void
vips_value_set_ref_string( GValue *value, const char *str )

View File

@ -19,6 +19,8 @@
* - moved to vips_ namespace
* 25/2/17
* - use expat for xml read, printf for xml write
* 16/8/17
* - validate strs as being utf-8 before we write
*/
/*
@ -796,13 +798,19 @@ build_xml_meta( VipsMeta *meta, VipsDbuf *dbuf )
return( meta );
}
/* We need to validate the str to make sure we'll be able to
* read it back.
*/
str = vips_value_get_save_string( &save_value );
vips_dbuf_writef( dbuf, " <field type=\"%s\" name=\"",
g_type_name( type ) );
dbuf_write_quotes( dbuf, meta->name );
vips_dbuf_writef( dbuf, "\">" );
dbuf_write_amp( dbuf, str );
vips_dbuf_writef( dbuf, "</field>\n" );
if( g_utf8_validate( str, -1, NULL ) ) {
vips_dbuf_writef( dbuf,
" <field type=\"%s\" name=\"",
g_type_name( type ) );
dbuf_write_quotes( dbuf, meta->name );
vips_dbuf_writef( dbuf, "\">" );
dbuf_write_amp( dbuf, str );
vips_dbuf_writef( dbuf, "</field>\n" );
}
g_value_unset( &save_value );
}
@ -827,10 +835,13 @@ build_xml( VipsImage *image )
vips_dbuf_writef( &dbuf, " <header>\n" );
str = vips_image_get_history( image );
vips_dbuf_writef( &dbuf, " <field type=\"%s\" name=\"Hist\">",
g_type_name( VIPS_TYPE_REF_STRING ) );
dbuf_write_amp( &dbuf, str );
vips_dbuf_writef( &dbuf, "</field>\n" );
if( g_utf8_validate( str, -1, NULL ) ) {
vips_dbuf_writef( &dbuf,
" <field type=\"%s\" name=\"Hist\">",
g_type_name( VIPS_TYPE_REF_STRING ) );
dbuf_write_amp( &dbuf, str );
vips_dbuf_writef( &dbuf, "</field>\n" );
}
vips_dbuf_writef( &dbuf, " </header>\n" );
vips_dbuf_writef( &dbuf, " <meta>\n" );
@ -940,7 +951,7 @@ vips__writehist( VipsImage *image )
}
#ifdef DEBUG
printf( "vips__writehist: saved XML is: \"%s\"", xml );
printf( "vips__writehist: saved XML is: \"%s\"\n", xml );
#endif /*DEBUG*/
g_free( xml );