From 2988260107bf596e4e0d4d1de39d324172feeca1 Mon Sep 17 00:00:00 2001 From: miaow1988 Date: Mon, 30 Jul 2018 17:04:15 +0800 Subject: [PATCH] Fixed the int size overflow bug of cv::Mat.push_back(). Changed the type of variable *r* from int to size_t. This change makes sure that a valid result of std::max(r + delta, (r*3+1)/2) can be passed into the reserve function. --- modules/core/src/matrix.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 65ac200463..2da6ca6399 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -602,13 +602,13 @@ void Mat::pop_back(size_t nelems) void Mat::push_back_(const void* elem) { - int r = size.p[0]; + size_t r = size.p[0]; if( isSubmatrix() || dataend + step.p[0] > datalimit ) reserve( std::max(r + 1, (r*3+1)/2) ); size_t esz = elemSize(); memcpy(data + r*step.p[0], elem, esz); - size.p[0] = r + 1; + size.p[0] = int(r + 1); dataend += step.p[0]; uint64 tsz = size.p[0]; for( int i = 1; i < dims; i++ ) @@ -709,7 +709,8 @@ void Mat::resize(size_t nelems, const Scalar& s) void Mat::push_back(const Mat& elems) { - int r = size.p[0], delta = elems.size.p[0]; + size_t r = size.p[0]; + size_t delta = elems.size.p[0]; if( delta == 0 ) return; if( this == &elems ) @@ -726,7 +727,7 @@ void Mat::push_back(const Mat& elems) size.p[0] = elems.size.p[0]; bool eq = size == elems.size; - size.p[0] = r; + size.p[0] = int(r); if( !eq ) CV_Error(CV_StsUnmatchedSizes, "Pushed vector length is not equal to matrix row length"); if( type() != elems.type() ) @@ -735,7 +736,7 @@ void Mat::push_back(const Mat& elems) if( isSubmatrix() || dataend + step.p[0]*delta > datalimit ) reserve( std::max(r + delta, (r*3+1)/2) ); - size.p[0] += delta; + size.p[0] += int(delta); dataend += step.p[0]*delta; //updateContinuityFlag(*this); @@ -744,7 +745,7 @@ void Mat::push_back(const Mat& elems) memcpy(data + r*step.p[0], elems.data, elems.total()*elems.elemSize()); else { - Mat part = rowRange(r, r + delta); + Mat part = rowRange(int(r), int(r + delta)); elems.copyTo(part); } }