From 4ff445af4d39c3d3f278bc2241aea2cafd31f149 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 15 Feb 2022 19:37:22 +0000 Subject: [PATCH] Check that we parsed something in wxBitmapBundle::FromSVG() Don't always return success from this function, NanoSVG just skips everything until the start of the XML prologue and doesn't return an error even if it doesn't find it at all, so check that it could parse at least something to avoid returning a "valid" bundle not containing anything at all. Add a unit test checking that we actually can't create an SVG from a .bmp file (which is something that "worked" before). --- src/generic/bmpsvg.cpp | 9 +++++++++ tests/graphics/bmpbundle.cpp | 8 ++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/generic/bmpsvg.cpp b/src/generic/bmpsvg.cpp index 37928bc89f..76f20dce4c 100644 --- a/src/generic/bmpsvg.cpp +++ b/src/generic/bmpsvg.cpp @@ -207,6 +207,15 @@ wxBitmapBundle wxBitmapBundle::FromSVG(char* data, const wxSize& sizeDef) if ( !svgImage ) return wxBitmapBundle(); + // Somewhat unexpectedly, a non-null but empty image is returned even if + // the data is not SVG at all, e.g. without this check creating a bundle + // from any random file with FromSVGFile() would "work". + if ( svgImage->width == 0 && svgImage->height == 0 && !svgImage->shapes ) + { + nsvgDelete(svgImage); + return wxBitmapBundle(); + } + return wxBitmapBundle(new wxBitmapBundleImplSVG(svgImage, sizeDef)); } diff --git a/tests/graphics/bmpbundle.cpp b/tests/graphics/bmpbundle.cpp index 176515624f..25002b29ef 100644 --- a/tests/graphics/bmpbundle.cpp +++ b/tests/graphics/bmpbundle.cpp @@ -140,9 +140,13 @@ TEST_CASE("BitmapBundle::FromSVG", "[bmpbundle][svg]") TEST_CASE("BitmapBundle::FromSVGFile", "[bmpbundle][svg][file]") { - wxBitmapBundle b = wxBitmapBundle::FromSVGFile("horse.svg", wxSize(20, 20)); + const wxSize size(20, 20); // completely arbitrary + + CHECK( !wxBitmapBundle::FromSVGFile("horse.bmp", size).IsOk() ); + + wxBitmapBundle b = wxBitmapBundle::FromSVGFile("horse.svg", size); REQUIRE( b.IsOk() ); - CHECK( b.GetDefaultSize() == wxSize(20, 20) ); + CHECK( b.GetDefaultSize() == size ); } #endif // wxHAS_SVG