Skip to content

Opencv does not check for numpy writeable flags when writing on array. #859

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

Closed
haixuanTao opened this issue Jun 26, 2023 · 1 comment · Fixed by opencv/opencv#24026 or intel/cloud-native-ai-pipeline#179
Assignees

Comments

@haixuanTao
Copy link

haixuanTao commented Jun 26, 2023

Expected behaviour

Numpy array has a flag indicating if the array is writeable or not.

In the case it is not writeable and the user is trying to write on the array, I would imagine opencv would raise an issue or would use a copy.

Actual behaviour

Opencv write on the array without checking the flag that can raise a segmentation fault.

Steps to reproduce

The following example is a simple example taken from https://github.com/dora-rs/dora , which use the latest pyarrow. You can probably make a numpy only example.

import pyarrow as pa
import numpy as np
import cv2

array = pa.array(np.zeros((100))).to_numpy().reshape((10,10))

assert array.flags.writeable == False, "Array should be not writable"

cv2.rectangle(array,(0,0),(5,5),(255),2)
print(array)
array([[255., 255., 255., 255., 255., 255., 255.,   0.,   0.,   0.],
       [255., 255., 255., 255., 255., 255., 255.,   0.,   0.,   0.],
       [255., 255.,   0.,   0., 255., 255., 255.,   0.,   0.,   0.],
       [255., 255.,   0.,   0., 255., 255., 255.,   0.,   0.,   0.],
       [255., 255., 255., 255., 255., 255., 255.,   0.,   0.,   0.],
       [255., 255., 255., 255., 255., 255., 255.,   0.,   0.,   0.],
       [255., 255., 255., 255., 255., 255.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.]])

Errors

This can lead to segmentation fault error if the data is not owned by the current process.

I wonder if #728 is linked to this issue.

@asmorkalov
Copy link
Collaborator

The fix will be released with 4.9.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants