From 9124371e21160714b6cd6c3e7a7908676df2312f Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Fri, 4 Sep 2026 11:57:49 +0200 Subject: [PATCH] Fix infinite loop in warpAffine Otherwise, p alternates between 1 and -1 That fix is already present in borderInterpolate https://github.com/opencv/opencv/blob/3a718750a4d4bcde78bc93bcaf18b0303e956e55/modules/core/src/copy.cpp#L975 --- modules/imgproc/src/warp_kernels.simd.hpp | 2 ++ modules/imgproc/test/test_imgwarp.cpp | 30 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/modules/imgproc/src/warp_kernels.simd.hpp b/modules/imgproc/src/warp_kernels.simd.hpp index bda168095e..341462d9c7 100644 --- a/modules/imgproc/src/warp_kernels.simd.hpp +++ b/modules/imgproc/src/warp_kernels.simd.hpp @@ -336,6 +336,8 @@ 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 ) { + if( len == 1 ) + return 0; int delta = borderType == BORDER_REFLECT_101; do { diff --git a/modules/imgproc/test/test_imgwarp.cpp b/modules/imgproc/test/test_imgwarp.cpp index 6975c31a3b..ee3a21f18a 100644 --- a/modules/imgproc/test/test_imgwarp.cpp +++ b/modules/imgproc/test/test_imgwarp.cpp @@ -43,6 +43,10 @@ #include "opencv2/ts/ts_gtest.h" #include "test_precomp.hpp" +#include +#include +#include + namespace opencv_test { namespace { class CV_ImgWarpBaseTest : public cvtest::ArrayTest @@ -1199,5 +1203,31 @@ TEST(Imgproc_Warping, DISABLED_playground) } } +TEST(Imgproc_Warping, infinite_loop) +{ + std::promise promise; + auto future = promise.get_future(); + + std::thread worker([&promise]() { + cv::Mat src(1, 10, CV_8UC1, cv::Scalar(128)); + cv::Mat dst; + cv::Matx23f M(1.f, 0.f, 0.f, 0.f, 1.f, 0.f); + + cv::warpAffine(src, dst, M, cv::Size(10, 10), cv::INTER_CUBIC, + cv::BORDER_REFLECT_101); + promise.set_value(); + }); + + auto status = future.wait_for(std::chrono::seconds(1)); + if (status == std::future_status::ready) { + worker.join(); + } else { + worker.detach(); + } + + EXPECT_EQ(status, std::future_status::ready) + << "cv::warpAffine hung in an infinite loop!"; +} + }} // namespace /* End of file. */