From a2a9a01e05e87956c1f2c853a553eb61bb0c08dc Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Wed, 18 Jul 2018 15:22:42 +0300 Subject: [PATCH] AVI container: verbose error messages --- modules/videoio/src/container_avi.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/modules/videoio/src/container_avi.cpp b/modules/videoio/src/container_avi.cpp index 0c581b3..c2a9369 100644 --- a/modules/videoio/src/container_avi.cpp +++ b/modules/videoio/src/container_avi.cpp @@ -12,7 +12,7 @@ namespace cv // Utility function for safe integer conversions template -inline D safe_int_cast(S val) +inline D safe_int_cast(S val, const char * msg = 0) { typedef std::numeric_limits st; typedef std::numeric_limits dt; @@ -21,7 +21,10 @@ inline D safe_int_cast(S val) const bool in_range_l = (double)val >= (double)dt::min(); if (!in_range_r || !in_range_l) { - CV_Error_(cv::Error::StsOutOfRange, ("Can not convert integer values (%s -> %s), value 0x%llx is out of range", typeid(S).name(), typeid(D).name(), val)); + if (!msg) + CV_Error_(Error::StsOutOfRange, ("Can not convert integer values (%s -> %s), value 0x%llx is out of range", typeid(S).name(), typeid(D).name(), val)); + else + CV_Error(Error::StsOutOfRange, msg); } return static_cast(val); } @@ -128,7 +131,7 @@ public: VideoInputStream(); VideoInputStream(const String& filename); ~VideoInputStream(); - VideoInputStream& read(char*, uint64_t); + VideoInputStream& read(char*, uint32_t); VideoInputStream& seekg(uint64_t); uint64_t tellg(); bool isOpened() const; @@ -229,11 +232,11 @@ void VideoInputStream::close() } } -VideoInputStream& VideoInputStream::read(char* buf, uint64_t count) +VideoInputStream& VideoInputStream::read(char* buf, uint32_t count) { if(isOpened()) { - input.read(buf, safe_int_cast(count)); + input.read(buf, safe_int_cast(count, "Failed to read AVI file: requested chunk size is too large")); m_is_valid = (input.gcount() == (std::streamsize)count); } @@ -243,7 +246,7 @@ VideoInputStream& VideoInputStream::read(char* buf, uint64_t count) VideoInputStream& VideoInputStream::seekg(uint64_t pos) { input.clear(); - input.seekg(safe_int_cast(pos)); + input.seekg(safe_int_cast(pos, "Failed to seek in AVI file: position is out of range")); m_is_valid = !input.eof(); return *this; } @@ -668,7 +671,7 @@ void BitStream::writeBlock() } size_t BitStream::getPos() const { - return safe_int_cast(m_current - m_start) + m_pos; + return safe_int_cast(m_current - m_start, "Failed to determine AVI bufer position: value is out of range") + m_pos; } void BitStream::putByte(int val) @@ -737,7 +740,7 @@ void BitStream::patchInt(uint32_t val, size_t pos) { if( pos >= m_pos ) { - ptrdiff_t delta = safe_int_cast(pos - m_pos); + ptrdiff_t delta = safe_int_cast(pos - m_pos, "Failed to seek in AVI buffer: value is out of range"); CV_Assert( delta < m_current - m_start ); m_start[delta] = (uchar)val; m_start[delta+1] = (uchar)(val >> 8); @@ -747,7 +750,7 @@ void BitStream::patchInt(uint32_t val, size_t pos) else { std::streamoff fpos = output.tellp(); - output.seekp(safe_int_cast(pos)); + output.seekp(safe_int_cast(pos, "Failed to seek in AVI file: value is out of range")); uchar buf[] = { (uchar)val, (uchar)(val >> 8), (uchar)(val >> 16), (uchar)(val >> 24) }; output.write((char *)buf, 4); output.seekp(fpos); @@ -960,7 +963,7 @@ void AVIWriteContainer::endWriteChunk() size_t pospos = AVIChunkSizeIndex.back(); AVIChunkSizeIndex.pop_back(); CV_Assert(currpos >= pospos); - uint32_t chunksz = safe_int_cast(currpos - pospos); + uint32_t chunksz = safe_int_cast(currpos - pospos, "Failed to write AVI file: chunk size is out of bounds"); strm->patchInt(chunksz, pospos); } } @@ -996,7 +999,7 @@ void AVIWriteContainer::writeIndex(int stream_number, StreamType strm_type) void AVIWriteContainer::finishWriteAVI() { - uint32_t nframes = safe_int_cast(frameOffset.size()); + uint32_t nframes = safe_int_cast(frameOffset.size(), "Failed to write AVI file: number of frames is too large"); // Record frames numbers to AVI Header while (!frameNumIndexes.empty()) { -- 2.7.4