From 20bf7b6a79091263078251eed64b738717d59eb5 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sun, 2 Sep 2018 09:53:41 +0000 Subject: [PATCH] imgcodecs(webp): use safe cast to size_t on Win32 --- modules/imgcodecs/src/grfmt_webp.cpp | 2 +- modules/imgcodecs/src/utils.hpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/modules/imgcodecs/src/grfmt_webp.cpp b/modules/imgcodecs/src/grfmt_webp.cpp index 7b5abfaad9..58ddec3665 100644 --- a/modules/imgcodecs/src/grfmt_webp.cpp +++ b/modules/imgcodecs/src/grfmt_webp.cpp @@ -107,7 +107,7 @@ bool WebPDecoder::readHeader() { fs.open(m_filename.c_str(), std::ios::binary); fs.seekg(0, std::ios::end); - fs_size = fs.tellg(); + fs_size = safeCastToSizeT(fs.tellg(), "File is too large"); fs.seekg(0, std::ios::beg); CV_Assert(fs && "File stream error"); CV_CheckGE(fs_size, WEBP_HEADER_SIZE, "File is too small"); diff --git a/modules/imgcodecs/src/utils.hpp b/modules/imgcodecs/src/utils.hpp index 7af4c6174e..6e0ec95826 100644 --- a/modules/imgcodecs/src/utils.hpp +++ b/modules/imgcodecs/src/utils.hpp @@ -44,6 +44,15 @@ int validateToInt(size_t step); +template static inline +size_t safeCastToSizeT(const _Tp v_origin, const char* msg) +{ + const size_t value_cast = (size_t)v_origin; + if ((_Tp)value_cast != v_origin) + CV_Error(cv::Error::StsError, msg ? msg : "Can't cast value into size_t"); + return value_cast; +} + struct PaletteEntry { unsigned char b, g, r, a;