mirror of
https://github.com/opencv/opencv.git
synced 2026-09-11 04:43:22 -05:00
9dba8a7df0764118470ac2edf99f78bd9362a072
python: fix segfault on 0-channel numpy array input #28747 ### Pull Request Readiness Checklist - [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 N/A: a Python unit test is added in `modules/python/test/test_mat.py`. No external test data required. - [x] The feature is well documented and sample code can be built with the project CMake N/A: this is a bug fix, not a new feature. No documentation update needed. ### Problem Passing a numpy array with shape `(H, W, 0)` (0 channels) to any OpenCV function that accepts a `Mat` argument (e.g. `cv2.resize`, `cv2.warpAffine`, `cv2.blur`) causes a **segfault**. **Reproducer:** ```python import cv2 import numpy as np arr = np.zeros((100, 100, 0), np.uint8) cv2.resize(arr, (200, 200)) # segfault ``` ### Root Cause In `modules/python/src2/cv2_convert.cpp`, the numpy→Mat conversion checks channel validity only against `CV_CN_MAX` (upper bound): ```cpp if (channels > CV_CN_MAX) // channels=0 passes this check ``` With `channels=0`, `CV_MAKETYPE(0, 0)` produces `type=-8`, which corrupts the Mat's internal type field and causes undefined behavior downstream. ### Fix Extend the check to also reject `channels < 1`: ```cpp if (channels < 1 || channels > CV_CN_MAX) ``` **After fix:** ``` cv2.error: src unable to wrap channels, invalid count (0, must be in [1, 512]) ``` ### Notes - This affects all functions that accept a `Mat` input, not just `cv2.resize` - The same bug exists in the `5.x` branch - A 0-channel array has no valid OpenCV Mat representation; rejecting it with a clear error is the correct behavior and poses no backward-compatibility risk (the previous behavior was a crash)
OpenCV: Open Source Computer Vision Library
Resources
- Homepage: https://opencv.org
- Courses: https://opencv.org/courses
- Docs: https://docs.opencv.org/4.x/
- Q&A forum: https://forum.opencv.org
- previous forum (read only): http://answers.opencv.org
- Issue tracking: https://github.com/opencv/opencv/issues
- Additional OpenCV functionality: https://github.com/opencv/opencv_contrib
- Donate to OpenCV: https://opencv.org/support/
Contributing
Please read the contribution guidelines before starting work on a pull request.
Summary of the guidelines:
- One pull request per issue;
- Choose the right base branch;
- Include tests and documentation;
- Clean up "oops" commits before submitting;
- Follow the coding style guide.
Additional Resources
- Submit your OpenCV-based project for inclusion in Community Friday on opencv.org
- Subscribe to the OpenCV YouTube Channel featuring OpenCV Live, an hour-long streaming show
- Follow OpenCV on LinkedIn for daily posts showing the state-of-the-art in computer vision & AI
- Apply to be an OpenCV Volunteer to help organize events and online campaigns as well as amplify them
- Follow OpenCV on Mastodon in the Fediverse
- Follow OpenCV on Twitter
- OpenCV.ai: Computer Vision and AI development services from the OpenCV team.
Description
Languages
C++
87.6%
C
3.2%
Python
2.9%
CMake
2%
Java
1.5%
Other
2.6%