From ad2854c8b3d634b23eddf77a9b5ee013d47a3484 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sun, 22 Sep 2019 10:45:00 +0000 Subject: [PATCH] imgcodecs: add check image.empty() in imwrite()/imencode() --- modules/imgcodecs/src/grfmt_tiff.cpp | 2 ++ modules/imgcodecs/src/loadsave.cpp | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/modules/imgcodecs/src/grfmt_tiff.cpp b/modules/imgcodecs/src/grfmt_tiff.cpp index b3f73329b7..a18ba09897 100644 --- a/modules/imgcodecs/src/grfmt_tiff.cpp +++ b/modules/imgcodecs/src/grfmt_tiff.cpp @@ -824,6 +824,7 @@ bool TiffEncoder::writeLibTiff( const std::vector& img_vec, const std::vect for (size_t page = 0; page < img_vec.size(); page++) { const Mat& img = img_vec[page]; + CV_Assert(!img.empty()); int channels = img.channels(); int width = img.cols, height = img.rows; int type = img.type(); @@ -883,6 +884,7 @@ bool TiffEncoder::writeLibTiff( const std::vector& img_vec, const std::vect const int bitsPerByte = 8; size_t fileStep = (width * channels * bitsPerChannel) / bitsPerByte; + CV_Assert(fileStep > 0); int rowsPerStrip = (int)((1 << 13) / fileStep); readParam(params, TIFFTAG_ROWSPERSTRIP, rowsPerStrip); diff --git a/modules/imgcodecs/src/loadsave.cpp b/modules/imgcodecs/src/loadsave.cpp index 6dd412fe6f..732cd359d0 100644 --- a/modules/imgcodecs/src/loadsave.cpp +++ b/modules/imgcodecs/src/loadsave.cpp @@ -689,6 +689,8 @@ static bool imwrite_( const String& filename, const std::vector& img_vec, for (size_t page = 0; page < img_vec.size(); page++) { Mat image = img_vec[page]; + CV_Assert(!image.empty()); + CV_Assert( image.channels() == 1 || image.channels() == 3 || image.channels() == 4 ); Mat temp; @@ -735,6 +737,9 @@ bool imwrite( const String& filename, InputArray _img, const std::vector& params ) { CV_TRACE_FUNCTION(); + + CV_Assert(!_img.empty()); + std::vector img_vec; if (_img.isMatVector() || _img.isUMatVector()) _img.getMatVector(img_vec); @@ -919,6 +924,7 @@ bool imencode( const String& ext, InputArray _image, CV_TRACE_FUNCTION(); Mat image = _image.getMat(); + CV_Assert(!image.empty()); int channels = image.channels(); CV_Assert( channels == 1 || channels == 3 || channels == 4 );