Skip to content

Commit c3d60a6

Browse files
committed
imgcodecs: add runtime checks to validate input
original commits: f9b1dbe
1 parent 7b92f3c commit c3d60a6

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

modules/imgcodecs/src/grfmt_jpeg2000_openjpeg.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ bool Jpeg2KOpjDecoderBase::readHeader()
545545
*/
546546
bool hasAlpha = false;
547547
const int numcomps = image_->numcomps;
548-
CV_Assert(numcomps >= 1);
548+
CV_Check(numcomps, numcomps >= 1 && numcomps <= 4, "Unsupported number of components");
549549
for (int i = 0; i < numcomps; i++)
550550
{
551551
const opj_image_comp_t& comp = image_->comps[i];

modules/imgcodecs/src/grfmt_pam.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,11 @@ bool PAMDecoder::readHeader()
470470
selected_fmt = CV_IMWRITE_PAM_FORMAT_GRAYSCALE;
471471
else if (m_channels == 3 && m_maxval < 256)
472472
selected_fmt = CV_IMWRITE_PAM_FORMAT_RGB;
473+
else
474+
CV_Error(Error::StsError, "Can't determine selected_fmt (IMWRITE_PAM_FORMAT_NULL)");
473475
}
476+
CV_CheckDepth(m_sampledepth, m_sampledepth == CV_8U || m_sampledepth == CV_16U, "");
477+
CV_Check(m_channels, m_channels >= 1 && m_channels <= 4, "Unsupported number of channels");
474478
m_type = CV_MAKETYPE(m_sampledepth, m_channels);
475479
m_offset = m_strm.getPos();
476480

@@ -567,6 +571,10 @@ bool PAMDecoder::readData(Mat& img)
567571
FillColorRow1( data, src, m_width, palette );
568572
}
569573
}
574+
else
575+
{
576+
CV_Error(Error::StsError, cv::format("Unsupported value of target_channels: %d", target_channels));
577+
}
570578
} else {
571579
for (int y = 0; y < m_height; y++, data += imp_stride)
572580
{

modules/imgcodecs/src/grfmt_tiff.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ bool TiffDecoder::checkSignature( const String& signature ) const
145145

146146
int TiffDecoder::normalizeChannelsNumber(int channels) const
147147
{
148-
CV_Assert(channels <= 4);
149-
return channels > 4 ? 4 : channels;
148+
CV_Check(channels, channels >= 1 && channels <= 4, "Unsupported number of channels");
149+
return channels;
150150
}
151151

152152
ImageDecoder TiffDecoder::newDecoder() const

0 commit comments

Comments
 (0)