Merge pull request #30592 from sshivensolanki:fix-laplacian-16f-overflow

imgproc: fix heap overflow in Laplacian for CV_16F
This commit is contained in:
Alexander Smorkalov
2026-10-30 10:13:58 +03:00
committed by GitHub
2 changed files with 12 additions and 1 deletions

View File

@@ -619,7 +619,9 @@ void cv::Laplacian( InputArray _src, OutputArray _dst, int ddepth, int ksize,
ddepth = sdepth;
_dst.create( _src.size(), CV_MAKETYPE(ddepth, cn) );
int ktype = std::max(CV_32F, std::max(ddepth, sdepth));
// the kernel is filled as float or double below, so it must be CV_32F or CV_64F
// (std::max over the depths picked CV_16F, since CV_16F > CV_64F)
int ktype = sdepth == CV_64F || ddepth == CV_64F ? CV_64F : CV_32F;
Mat kernel;
if( ksize == 1 || ksize == 3 )

View File

@@ -2566,6 +2566,15 @@ TEST(Imgproc, filter_empty_src_16857)
EXPECT_TRUE(dst2.empty());
}
TEST(Imgproc_Laplacian, regression_16F_kernel_overflow)
{
// the 3x3 kernel used to be created as CV_16F and then filled with 9 doubles,
// writing past its end; now it is CV_32F and filter2D rejects the CV_16F input
Mat src(8, 8, CV_16FC1, Scalar(1)), dst;
EXPECT_THROW(Laplacian(src, dst, -1, 1), cv::Exception);
EXPECT_THROW(Laplacian(src, dst, -1, 3), cv::Exception);
}
TEST(Imgproc_GaussianBlur, regression_11303)
{
cv::Mat dst;