From cdc5bbc0bc95876985e2301117f175fa7ce6a831 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Thu, 5 Apr 2012 20:23:53 +0000 Subject: [PATCH] fixed crash in I/O tests on Windows by making sure std::vector is non-empty before taking pointer to its elements --- .../core/include/opencv2/core/operations.hpp | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/modules/core/include/opencv2/core/operations.hpp b/modules/core/include/opencv2/core/operations.hpp index 9337912e4c..f765f44950 100644 --- a/modules/core/include/opencv2/core/operations.hpp +++ b/modules/core/include/opencv2/core/operations.hpp @@ -2277,7 +2277,7 @@ public: { set((_Tp*)&vec.val[0], n, true); } Vector(const std::vector<_Tp>& vec, bool _copyData=false) - { set((_Tp*)&vec[0], vec.size(), _copyData); } + { set(!vec.empty() ? (_Tp*)&vec[0] : 0, vec.size(), _copyData); } Vector(const Vector& d) { *this = d; } @@ -2445,14 +2445,17 @@ dot(const Vector<_Tp>& v1, const Vector<_Tp>& v2) assert(v1.size() == v2.size()); _Tw s = 0; - const _Tp *ptr1 = &v1[0], *ptr2 = &v2[0]; - #if CV_ENABLE_UNROLLED - for(; i <= n - 4; i += 4 ) - s += (_Tw)ptr1[i]*ptr2[i] + (_Tw)ptr1[i+1]*ptr2[i+1] + - (_Tw)ptr1[i+2]*ptr2[i+2] + (_Tw)ptr1[i+3]*ptr2[i+3]; -#endif - for( ; i < n; i++ ) - s += (_Tw)ptr1[i]*ptr2[i]; + if( n > 0 ) + { + const _Tp *ptr1 = &v1[0], *ptr2 = &v2[0]; + #if CV_ENABLE_UNROLLED + for(; i <= n - 4; i += 4 ) + s += (_Tw)ptr1[i]*ptr2[i] + (_Tw)ptr1[i+1]*ptr2[i+1] + + (_Tw)ptr1[i+2]*ptr2[i+2] + (_Tw)ptr1[i+3]*ptr2[i+3]; + #endif + for( ; i < n; i++ ) + s += (_Tw)ptr1[i]*ptr2[i]; + } return s; } @@ -3012,7 +3015,7 @@ public: size_t remaining1 = remaining/cn; count = count < remaining1 ? count : remaining1; vec.resize(count); - it->readRaw( string(fmt), (uchar*)&vec[0], count*sizeof(_Tp) ); + it->readRaw( string(fmt), !vec.empty() ? (uchar*)&vec[0] : 0, count*sizeof(_Tp) ); } FileNodeIterator* it; };