Skip to content

Commit 4e7f288

Browse files
reunanenalalek
authored andcommitted
Merge pull request opencv#7452 from reunanen:issue-7409-2.4
Fix findContours crash for very large images (v2.4) * Cast step to size_t in order to avoid integer overflow when processing very large images * Change assert to CV_Assert
1 parent c7c831b commit 4e7f288

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

modules/imgproc/src/contours.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,13 @@ cvStartFindContours( void* _img, CvMemStorage* storage,
284284
scanner->cinfo_storage );
285285
}
286286

287+
CV_Assert(step >= 0);
288+
CV_Assert(size.height >= 1);
289+
287290
/* make zero borders */
288291
int esz = CV_ELEM_SIZE(mat->type);
289292
memset( img, 0, size.width*esz );
290-
memset( img + step * (size.height - 1), 0, size.width*esz );
293+
memset( img + static_cast<size_t>(step) * (size.height - 1), 0, size.width*esz );
291294

292295
img += step;
293296
for( int y = 1; y < size.height - 1; y++, img += step )
@@ -989,6 +992,8 @@ cvFindNextContour( CvContourScanner scanner )
989992
{
990993
if( !scanner )
991994
CV_Error( CV_StsNullPtr, "" );
995+
CV_Assert(scanner->img_step >= 0);
996+
992997
icvEndProcessContour( scanner );
993998

994999
/* initialize local state */
@@ -1063,7 +1068,7 @@ cvFindNextContour( CvContourScanner scanner )
10631068
is_hole = 1;
10641069
}
10651070

1066-
if( mode == 0 && (is_hole || img0[lnbd.y * step + lnbd.x] > 0) )
1071+
if( mode == 0 && (is_hole || img0[lnbd.y * static_cast<size_t>(step) + lnbd.x] > 0) )
10671072
goto resume_scan;
10681073

10691074
origin.y = y;
@@ -1077,8 +1082,8 @@ cvFindNextContour( CvContourScanner scanner )
10771082
else
10781083
{
10791084
int lval = (img0_i ?
1080-
img0_i[lnbd.y * step_i + lnbd.x] :
1081-
(int)img0[lnbd.y * step + lnbd.x]) & 0x7f;
1085+
img0_i[lnbd.y * static_cast<size_t>(step_i) + lnbd.x] :
1086+
(int)img0[lnbd.y * static_cast<size_t>(step) + lnbd.x]) & 0x7f;
10821087
_CvContourInfo *cur = scanner->cinfo_table[lval];
10831088

10841089
/* find the first bounding contour */
@@ -1090,11 +1095,11 @@ cvFindNextContour( CvContourScanner scanner )
10901095
if( par_info )
10911096
{
10921097
if( (img0_i &&
1093-
icvTraceContour_32s( img0_i + par_info->origin.y * step_i +
1098+
icvTraceContour_32s( img0_i + par_info->origin.y * static_cast<size_t>(step_i) +
10941099
par_info->origin.x, step_i, img_i + lnbd.x,
10951100
par_info->is_hole ) > 0) ||
10961101
(!img0_i &&
1097-
icvTraceContour( img0 + par_info->origin.y * step +
1102+
icvTraceContour( img0 + par_info->origin.y * static_cast<size_t>(step) +
10981103
par_info->origin.x, step, img + lnbd.x,
10991104
par_info->is_hole ) > 0) )
11001105
break;

0 commit comments

Comments
 (0)