diff --git a/modules/imgcodecs/src/bitstrm.cpp b/modules/imgcodecs/src/bitstrm.cpp index 80bb26c097..a11697264b 100644 --- a/modules/imgcodecs/src/bitstrm.cpp +++ b/modules/imgcodecs/src/bitstrm.cpp @@ -42,6 +42,7 @@ #include "precomp.hpp" #include "bitstrm.hpp" +#include "utils.hpp" namespace cv { @@ -165,7 +166,7 @@ void RBaseStream::release() void RBaseStream::setPos( int pos ) { - assert( isOpened() && pos >= 0 ); + CV_Assert(isOpened() && pos >= 0); if( !m_file ) { @@ -182,14 +183,19 @@ void RBaseStream::setPos( int pos ) int RBaseStream::getPos() { - assert( isOpened() ); - return m_block_pos + (int)(m_current - m_start); + CV_Assert(isOpened()); + int pos = validateToInt((m_current - m_start) + m_block_pos); + CV_Assert(pos >= m_block_pos); // overflow check + CV_Assert(pos >= 0); // overflow check + return pos; } void RBaseStream::skip( int bytes ) { - assert( bytes >= 0 ); + CV_Assert(bytes >= 0); + uchar* old = m_current; m_current += bytes; + CV_Assert(m_current >= old); // overflow check } ///////////////////////// RLByteStream //////////////////////////// @@ -221,7 +227,7 @@ int RLByteStream::getBytes( void* buffer, int count ) { uchar* data = (uchar*)buffer; int readed = 0; - assert( count >= 0 ); + CV_Assert(count >= 0); while( count > 0 ) { @@ -373,7 +379,7 @@ void WBaseStream::writeBlock() { int size = (int)(m_current - m_start); - assert( isOpened() ); + CV_Assert(isOpened()); if( size == 0 ) return; @@ -444,7 +450,7 @@ void WBaseStream::release() int WBaseStream::getPos() { - assert( isOpened() ); + CV_Assert(isOpened()); return m_block_pos + (int)(m_current - m_start); } @@ -467,7 +473,7 @@ void WLByteStream::putBytes( const void* buffer, int count ) { uchar* data = (uchar*)buffer; - assert( data && m_current && count >= 0 ); + CV_Assert(data && m_current && count >= 0); while( count ) { diff --git a/modules/imgcodecs/src/grfmt_bmp.cpp b/modules/imgcodecs/src/grfmt_bmp.cpp index ab46219aa0..25a386eaeb 100644 --- a/modules/imgcodecs/src/grfmt_bmp.cpp +++ b/modules/imgcodecs/src/grfmt_bmp.cpp @@ -95,6 +95,7 @@ bool BmpDecoder::readHeader() m_offset = m_strm.getDWord(); int size = m_strm.getDWord(); + CV_Assert(size > 0); // overflow, 2Gb limit if( size >= 36 ) { diff --git a/modules/imgcodecs/src/grfmt_pam.cpp b/modules/imgcodecs/src/grfmt_pam.cpp index 3dd4605588..5eeaed1ec6 100644 --- a/modules/imgcodecs/src/grfmt_pam.cpp +++ b/modules/imgcodecs/src/grfmt_pam.cpp @@ -53,6 +53,8 @@ #include "utils.hpp" #include "grfmt_pam.hpp" +using namespace cv; + /* the PAM related fields */ #define MAX_PAM_HEADER_IDENITFIER_LENGTH 8 #define MAX_PAM_HEADER_VALUE_LENGTH 255 @@ -184,7 +186,7 @@ basic_conversion (void *src, const struct channel_layout *layout, int src_sampe_ } break; default: - assert (0); + CV_Error(Error::StsInternal, ""); } break; } @@ -205,12 +207,12 @@ basic_conversion (void *src, const struct channel_layout *layout, int src_sampe_ } break; default: - assert (0); + CV_Error(Error::StsInternal, ""); } break; } default: - assert (0); + CV_Error(Error::StsInternal, ""); } } @@ -711,7 +713,7 @@ bool PAMEncoder::write( const Mat& img, const std::vector& params ) } else strm.putBytes( data, stride*height ); } else - assert (0); + CV_Error(Error::StsInternal, ""); strm.close(); return true; diff --git a/modules/imgcodecs/src/grfmt_sunras.cpp b/modules/imgcodecs/src/grfmt_sunras.cpp index 77efc4b1bf..85a9ab9d34 100644 --- a/modules/imgcodecs/src/grfmt_sunras.cpp +++ b/modules/imgcodecs/src/grfmt_sunras.cpp @@ -124,7 +124,7 @@ bool SunRasterDecoder::readHeader() m_type = IsColorPalette( m_palette, m_bpp ) ? CV_8UC3 : CV_8UC1; m_offset = m_strm.getPos(); - assert( m_offset == 32 + m_maplength ); + CV_Assert(m_offset == 32 + m_maplength); result = true; } } @@ -137,7 +137,7 @@ bool SunRasterDecoder::readHeader() m_offset = m_strm.getPos(); - assert( m_offset == 32 + m_maplength ); + CV_Assert(m_offset == 32 + m_maplength); result = true; } } @@ -230,7 +230,7 @@ bool SunRasterDecoder::readData( Mat& img ) code = m_strm.getByte(); if( len > line_end - tsrc ) { - assert(0); + CV_Error(Error::StsInternal, ""); goto bad_decoding_1bpp; } @@ -371,7 +371,7 @@ bad_decoding_end: result = true; break; default: - assert(0); + CV_Error(Error::StsInternal, ""); } } CV_CATCH_ALL diff --git a/modules/imgcodecs/src/precomp.hpp b/modules/imgcodecs/src/precomp.hpp index d5ce248498..c280b08ce2 100644 --- a/modules/imgcodecs/src/precomp.hpp +++ b/modules/imgcodecs/src/precomp.hpp @@ -56,7 +56,6 @@ #include #include #include -#include #if defined _WIN32 || defined WINCE #include diff --git a/modules/imgcodecs/src/utils.cpp b/modules/imgcodecs/src/utils.cpp index 474dae008c..3273912289 100644 --- a/modules/imgcodecs/src/utils.cpp +++ b/modules/imgcodecs/src/utils.cpp @@ -670,7 +670,7 @@ cvConvertImage( const CvArr* srcarr, CvArr* dstarr, int flags ) icvCvt_BGR2Gray_8u_C3C1R( s, s_step, d, d_step, size, swap_rb ); break; case 33: - assert( swap_rb ); + CV_Assert(swap_rb); icvCvt_RGB2BGR_8u_C3R( s, s_step, d, d_step, size ); break; case 41: