Skip to content

Commit 62ed6cd

Browse files
committed
core: fix copyTo(with mask) dst initialization
1 parent b0bce60 commit 62ed6cd

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

modules/core/src/copy.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,21 @@ void Mat::copyTo( OutputArray _dst, InputArray _mask ) const
385385
CV_Assert( size() == mask.size() );
386386
}
387387

388-
uchar* data0 = _dst.getMat().data;
389-
_dst.create( dims, size, type() );
390-
Mat dst = _dst.getMat();
388+
Mat dst;
389+
{
390+
Mat dst0 = _dst.getMat();
391+
_dst.create(dims, size, type()); // TODO Prohibit 'dst' re-creation, user should pass it explicitly with correct size/type or empty
392+
dst = _dst.getMat();
391393

392-
if( dst.data != data0 ) // do not leave dst uninitialized
393-
dst = Scalar(0);
394+
if (dst.data != dst0.data) // re-allocation happened
395+
{
396+
#ifdef OPENCV_FUTURE
397+
CV_Assert(dst0.empty() &&
398+
"copyTo(): dst size/type mismatch (looks like a bug) - use dst.release() before copyTo() call to suppress this message");
399+
#endif
400+
dst = Scalar(0); // do not leave dst uninitialized
401+
}
402+
}
394403

395404
CV_IPP_RUN_FAST(ipp_copyTo(*this, dst, mask))
396405

0 commit comments

Comments
 (0)