1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 15:53:03 +04:00

Merge pull request #27621 from Kumataro:trial27557

Add strict validation for encoding parameters #27621

Close https://github.com/opencv/opencv/issues/27557

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Kumataro
2025-08-12 22:04:45 +09:00
committed by GitHub
parent d33999cb6e
commit c5bb6a014a
22 changed files with 448 additions and 84 deletions
+34
View File
@@ -166,6 +166,8 @@ TEST_P(Imgcodecs_ExtSize, write_imageseq)
continue;
if (cn == 1 && ext == ".gif")
continue;
if (cn == 1 && ext == ".webp")
continue;
string filename = cv::tempfile(format("%d%s", cn, ext.c_str()).c_str());
Mat img_gt(size, CV_MAKETYPE(CV_8U, cn), Scalar::all(0));
@@ -257,6 +259,9 @@ const string all_exts[] =
#ifdef HAVE_IMGCODEC_GIF
".gif",
#endif
#ifdef HAVE_WEBP
".webp",
#endif
};
vector<Size> all_sizes()
@@ -307,6 +312,35 @@ TEST_P(Imgcodecs_pbm, write_read)
INSTANTIATE_TEST_CASE_P(All, Imgcodecs_pbm, testing::Bool());
#endif
// See https://github.com/opencv/opencv/issues/27557
typedef testing::TestWithParam<string> Imgcodecs_invalid_key;
TEST_P(Imgcodecs_invalid_key, encode_regression27557)
{
const string ext = GetParam();
const int matType = ((ext == ".pbm") || (ext == ".pgm"))? CV_8UC1 : CV_8UC3;
Mat src(100, 100, matType, Scalar(0, 255, 0));
std::vector<uchar> buf;
bool status = false;
EXPECT_NO_THROW(status = imencode(ext, src, buf, { -1, -1 }));
EXPECT_TRUE(status);
}
TEST_P(Imgcodecs_invalid_key, write_regression27557)
{
const string ext = GetParam();
string fname = tempfile(ext.c_str());
const int matType = ((ext == ".pbm") || (ext == ".pgm"))? CV_8UC1 : CV_8UC3;
Mat src(100, 100, matType, Scalar(0, 255, 0));
std::vector<uchar> buf;
bool status = false;
EXPECT_NO_THROW(status = imwrite(fname, src, { -1, -1 }));
EXPECT_TRUE(status);
remove(fname.c_str());
}
INSTANTIATE_TEST_CASE_P(All, Imgcodecs_invalid_key, testing::ValuesIn(all_exts));
//==================================================================================================