-
-
Notifications
You must be signed in to change notification settings - Fork 56.2k
Issue #27557 Added validation for imwrite and imencode and its alternative #27616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Proposed ChangesI'm trying to fix issue #27557 where invalid parameters like What I added:
How it works:
Before vs After:Before (Silent acceptance): .jpg imencode result: 1
.jpg imwrite result: 1 After (Clear error reporting): terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.13.0-dev) .../loadsave.cpp:203: error: (-5:Bad argument)
Invalid parameter ID: -1. Parameter IDs must be non-negative.
in function 'validateEncodingParams' Test Code:#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, const char** argv)
{
std::vector<std::string> exts = {
".bmp", ".gif", ".ppm", ".ras", ".png",
".tiff", ".jpg", ".webp", ".jp2"
};
Mat image = Mat(100, 100, CV_8UC3, Scalar(0, 255, 0));
for (size_t i = 0; i < exts.size(); i++)
{
std::vector<uchar> buf;
bool enc_result = imencode(exts[i], image, buf, { -1, -1 });
bool write_result = imwrite("test" + exts[i], image, { -1, -1 });
std::cout << exts[i] << " imencode result: " << enc_result << std::endl;
std::cout << exts[i] << " imwrite result: " << write_result << std::endl;
}
return 0;
} Testing:I tested it with the code from the original issue and now instead of silently succeeding, it properly throws an error. Fixes #27557 |
Please suggest if it needs improvement or any changes in the logic. |
Hello, thank you for your pull request. This comment is by one of contributors, not maintainers. I believe it is better to validate encode param (the pairs of key and value) on each Encoders implementation. My idea is Kumataro@52cad13
However, this mechanism doesn't accept unused invalid parameters. The validation may be a bit excessive. |
@Kumataro Thanks for the feedback! I'm still getting to know the workings of how to structure things properly in bigger projects, so this is really helpful. You make a good point about the distributed validation. I went with the centralized thing because it seemed straightforward to implement, but yeah, having to update that lookup table every time someone adds a format isn't great. Plus I didn't really think about the string processing cost as it was not much noticeable when dealing with single images, but it could add up when handling a large batch. Your approach makes more sense - let each encoder worry about its own stuff. Much cleaner that way. I saw you mentioned Kumataro/opencv@52cad13 - did you already code this up? If you're planning to make a PR and the maintainers like it better, no worries on my end. I can close this one. I guess I should wait to see what the maintainers think? My main thing was just stopping those invalid parameters from getting silently ignored, so whatever works best for the project is fine by me. Thanks for explaining all this - definitely helps me think about these design trade-offs better. |
Thanks for your reply! With my method, there's an issue where the same code may or may not throw an error depending on the user's build configuration. For example, if JPEG and WebP encoding parameters are specified simultaneously in a single vector, an error will occur as the parameter specification is invalid only if WebP is disabled. I believe this is an extremely rare case. However, there is a slight risk of it causing problems later. To prevent this, centralized management (loadsave.c) is preferable to decentralized management (individual encoders). I'll await the maintainer's comments on whether decentralized management is acceptable. |
Closed in favor of #27621 |
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.
Close #27557.