Merge pull request #2318 from lovell/svgload-check-dimensions

svgload: skip images with invalid dimensions
This commit is contained in:
John Cupitt 2021-06-28 15:32:19 +01:00 committed by GitHub
commit 5539e7951a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View File

@ -245,9 +245,11 @@ vips_foreign_load_svg_get_flags( VipsForeignLoad *load )
return( VIPS_FOREIGN_SEQUENTIAL );
}
static void
static int
vips_foreign_load_svg_parse( VipsForeignLoadSvg *svg, VipsImage *out )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( svg );
RsvgDimensionData dimensions;
int width;
int height;
@ -261,6 +263,11 @@ vips_foreign_load_svg_parse( VipsForeignLoadSvg *svg, VipsImage *out )
width = dimensions.width;
height = dimensions.height;
if( width <= 0 || height <= 0 ) {
vips_error( class->nickname, "%s", _( "bad dimensions" ) );
return( -1 );
}
/* Calculate dimensions at required dpi/scale.
*/
scale = svg->scale * svg->dpi / 72.0;
@ -300,6 +307,7 @@ vips_foreign_load_svg_parse( VipsForeignLoadSvg *svg, VipsImage *out )
*/
vips_image_pipelinev( out, VIPS_DEMAND_STYLE_FATSTRIP, NULL );
return( 0 );
}
static int
@ -307,9 +315,7 @@ vips_foreign_load_svg_header( VipsForeignLoad *load )
{
VipsForeignLoadSvg *svg = (VipsForeignLoadSvg *) load;
vips_foreign_load_svg_parse( svg, load->out );
return( 0 );
return vips_foreign_load_svg_parse( svg, load->out );
}
static int
@ -386,9 +392,9 @@ vips_foreign_load_svg_load( VipsForeignLoad *load )
* Make tiles 2000 pixels high to limit overcomputation.
*/
t[0] = vips_image_new();
vips_foreign_load_svg_parse( svg, t[0] );
if( vips_image_generate( t[0],
NULL, vips_foreign_load_svg_generate, NULL, svg, NULL ) ||
if( vips_foreign_load_svg_parse( svg, t[0] ) ||
vips_image_generate( t[0], NULL,
vips_foreign_load_svg_generate, NULL, svg, NULL ) ||
vips_tilecache( t[0], &t[1],
"tile_width", VIPS_MIN( t[0]->Xsize, RSVG_MAX_WIDTH ),
"tile_height", 2000,

View File

@ -895,6 +895,10 @@ class TestForeign:
assert abs(im.width * 2 - x.width) < 2
assert abs(im.height * 2 - x.height) < 2
with pytest.raises(pyvips.error.Error):
svg = b'<svg viewBox="0 0 0 0"></svg>'
im = pyvips.Image.new_from_buffer(svg, "")
def test_csv(self):
self.save_load("%s.csv", self.mono)