Merge pull request #29899 from vrabaud:cpu_bomb

Fix potential CPU bomb
This commit is contained in:
Alexander Smorkalov
2026-09-08 20:01:36 +03:00
committed by GitHub
2 changed files with 24 additions and 9 deletions

View File

@@ -336,17 +336,22 @@ static inline int borderInterpolate_fast( int p, int len, int borderType )
p = p < 0 ? 0 : len - 1;
else if( borderType == BORDER_REFLECT || borderType == BORDER_REFLECT_101 )
{
const int delta = borderType == BORDER_REFLECT_101;
if( len == 1 )
return 0;
int delta = borderType == BORDER_REFLECT_101;
do
{
if( p < 0 )
p = -p - 1 + delta;
else
p = len - 1 - (p - len) - delta;
}
while( (unsigned)p >= (unsigned)len );
// Fast path: single reflection without division for small deviations.
if( -len + delta <= p && p < 2 * len - delta )
return p < 0 ? -p - 1 + delta : 2 * len - 1 - delta - p;
// Bounded fallback: O(1) modulo for large |p|
const int64 period = 2LL * (len - delta);
int64 p64 = p;
p64 %= period;
if( p64 < 0 )
p64 += period;
if( p64 >= len )
p64 = period - p64 - 1 + delta;
p = (int)p64;
}
else if( borderType == BORDER_WRAP )
{

View File

@@ -1253,5 +1253,15 @@ TEST(Imgproc_Warping, infinite_loop)
<< "cv::warpAffine hung in an infinite loop!";
}
TEST(Imgproc_Warping, interpolate_loop)
{
// 1000x1000 destination image with out-of-bounds coordinates:
cv::Mat src(2, 2, CV_8UC1, cv::Scalar(42));
cv::Mat dst;
// A perspective or affine transform mapping pixels to large coordinates:
cv::Matx23d M(1, 0, 1e7, 0, 1, 1e7);
cv::warpAffine(src, dst, M, cv::Size(50, 50), cv::INTER_NEAREST, cv::BORDER_REFLECT_101);
}
}} // namespace
/* End of file. */