From 84a0c49ec3d7395057e8a8babcd7eea3866471e3 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Thu, 11 Jun 2015 14:02:34 -0700 Subject: [PATCH 1/2] Avoid crashing if we try to read a png as a jpeg - closes #2903 --- src/jpeg_reader.cpp | 5 ++++- test/data | 2 +- test/unit/imaging/image_io_test.cpp | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/jpeg_reader.cpp b/src/jpeg_reader.cpp index 213469403..e6db1d527 100644 --- a/src/jpeg_reader.cpp +++ b/src/jpeg_reader.cpp @@ -206,8 +206,11 @@ void jpeg_reader::attach_stream (j_decompress_ptr cinfo, input_stream* in) } template -void jpeg_reader::on_error(j_common_ptr /*cinfo*/) +void jpeg_reader::on_error(j_common_ptr cinfo) { + char buffer[JMSG_LENGTH_MAX]; + (*cinfo->err->format_message)(cinfo, buffer); + throw image_reader_exception(std::string("JPEG Reader: libjpeg could not read image: ") + buffer); } template diff --git a/test/data b/test/data index bb25d5132..a6838f99a 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit bb25d5132c0245aa6188d04b9e22d191fb5aca7c +Subproject commit a6838f99a4c4be37989cea6718442cf4b53bd616 diff --git a/test/unit/imaging/image_io_test.cpp b/test/unit/imaging/image_io_test.cpp index 8772ef3cd..eb38ae623 100644 --- a/test/unit/imaging/image_io_test.cpp +++ b/test/unit/imaging/image_io_test.cpp @@ -26,6 +26,21 @@ SECTION("readers") { type = mapnik::type_from_filename(should_throw); REQUIRE( type ); REQUIRE_THROWS(std::unique_ptr reader(mapnik::get_image_reader(should_throw,*type))); + + // actually a png so jpeg reader should throw + should_throw = "./test/data/images/landusepattern.jpg"; + REQUIRE( mapnik::util::exists( should_throw ) ); + type = mapnik::type_from_filename(should_throw); + REQUIRE( type ); + try + { + std::unique_ptr reader(mapnik::get_image_reader(should_throw,*type)); + REQUIRE(false); + } + catch (std::exception const& ex) + { + REQUIRE( std::string(ex.what()) == std::string("JPEG Reader: libjpeg could not read image: Not a JPEG file: starts with 0x89") ); + } #endif REQUIRE_THROWS(mapnik::image_rgba8 im(-10,-10)); // should throw rather than overflow From 5465adc2295cf91c8e122a7ec1a4f154001433b3 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Thu, 11 Jun 2015 14:04:20 -0700 Subject: [PATCH 2/2] fix expected error message --- test/unit/imaging/image_io_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/imaging/image_io_test.cpp b/test/unit/imaging/image_io_test.cpp index eb38ae623..d0a984d7d 100644 --- a/test/unit/imaging/image_io_test.cpp +++ b/test/unit/imaging/image_io_test.cpp @@ -39,7 +39,7 @@ SECTION("readers") { } catch (std::exception const& ex) { - REQUIRE( std::string(ex.what()) == std::string("JPEG Reader: libjpeg could not read image: Not a JPEG file: starts with 0x89") ); + REQUIRE( std::string(ex.what()) == std::string("JPEG Reader: libjpeg could not read image: Not a JPEG file: starts with 0x89 0x50") ); } #endif