Files
opencv-MIRROR/modules/objdetect/test
Parchuri Poojith Kumar c776338cf8 Merge pull request #31042 from poojithparchuri-iiitk:qr-encoder-validate
objdetect: validate the QR encoder version and correction level - #31042

QRCodeEncoder::create accepts any version and correction level, and the encoder then uses them as indexes into its tables. A version above 40 crashes or hangs it, and an unknown correction level crashes it too. With the Python package this is easy to see: set params.version = 41 or params.correction_level = 100 and call encode() on the encoder, and the process segfaults. With version 100 or 255 it hangs (I stopped it after 15 seconds), and with version 40 and correction_level 4 it crashes with SIGFPE, an arithmetic error.. The tables have MAX_VERSION + 1 entries, so version 41 reads past the end.

This adds two checks to the constructor of the encoder: the version must be in [0, 40], where 0 still selects it automatically, and the correction level must be one of the four known values. Anything else raises an error. Valid values are not affected.

I added three tests: an invalid version, an invalid correction level, and one that checks that the lowest and highest valid values (version 0, 1 and 40 with all four levels) still encode. On Windows with MSVC, without the change the two tests for invalid values fail, because the encoder accepts the bad values, and with the change they pass. The self-contained existing encoder tests also pass (Kanji, structured append, auto version, long text). I did not run the existing encoder tests that need images from opencv_extra, so CI will be the first run of those. My AI assistant also reproduced the crashes on the released 4.14.0 and 5.1.0 Python packages, and qrcode_encoder.cpp is the same in 4.14.0 and current 4.x.

This is separate from my open PR #30773, which changes the decoder part of the same file, so they do not touch the same lines.

I built and tested it myself on Windows, and an AI assistant (Claude) helped me find the problem and write the change and the tests.

- [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
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
2026-11-24 10:57:55 +03:00
..
2021-05-25 11:28:23 +03:00