1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 23:33:05 +04:00

Merge pull request #28250 from satyam102006:fix/stackblur-overflow-28233

imgproc: fix heap-buffer-overflow in stackBlur #28233 #28250

### Summary
Fixes a heap-buffer-overflow in `cv::stackBlur` when the kernel size is larger than the image dimensions 
### Changes
* Added input validation to clamp the kernel size to the image dimensions.
* Added a regression test (`regression_28233`) covering 1x1 and small image cases.

Fixes #28233
This commit is contained in:
satyam yadav
2025-12-22 13:52:12 +05:30
committed by GitHub
parent a66443fdc2
commit 15fe69b5af
3 changed files with 23 additions and 4 deletions
+9 -3
View File
@@ -41,6 +41,7 @@ OTHER DEALINGS IN THE SOFTWARE.
#include "opencv2/core/hal/intrin.hpp"
#include <iostream>
#include <algorithm>
using namespace std;
@@ -1199,12 +1200,17 @@ void stackBlur(InputArray _src, OutputArray _dst, Size ksize)
CV_Assert( ksize.width > 0 && ksize.width % 2 == 1 &&
ksize.height > 0 && ksize.height % 2 == 1 );
int radiusH = ksize.height / 2;
int radiusW = ksize.width / 2;
int stype = _src.type(), sdepth = _src.depth();
Mat src = _src.getMat();
if (ksize.width > src.cols)
ksize.width = (src.cols % 2 == 0) ? std::max(1, src.cols - 1) : src.cols;
if (ksize.height > src.rows)
ksize.height = (src.rows % 2 == 0) ? std::max(1, src.rows - 1) : src.rows;
int radiusH = ksize.height / 2;
int radiusW = ksize.width / 2;
if (ksize.width == 1)
{
_src.copyTo(_dst);
+13
View File
@@ -311,5 +311,18 @@ TEST_P(StackBlur_GaussianBlur, compare)
}
INSTANTIATE_TEST_CASE_P(Imgproc, StackBlur_GaussianBlur, testing::Values(CV_8U, CV_16S, CV_16U, CV_32F));
TEST(Imgproc_StackBlur, regression_28233)
{
Mat src1(1, 1, CV_8UC1, Scalar(123));
Mat dst1;
EXPECT_NO_THROW(stackBlur(src1, dst1, Size(9, 1)));
EXPECT_EQ(dst1.at<uchar>(0, 0), 123);
Mat src2(3, 3, CV_8UC1, Scalar(50));
Mat dst2;
EXPECT_NO_THROW(stackBlur(src2, dst2, Size(11, 11)));
EXPECT_EQ(dst2.at<uchar>(1, 1), 50);
}
}
}
+1 -1
View File
@@ -552,7 +552,7 @@ QUnit.test('test_filter', function(assert) {
cv.stackBlur(src, src, new cv.Size(3, 3));
// Verify result.
let expected = new Uint8Array([22,29,36,38,43,50]);
let expected = new Uint8Array([14,22,29,46,51,58]);
assert.deepEqual(src.data, expected);
src.delete();