Skip to content
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

Fix possible itemsize overflows in usm_ndarray from-pointer constructors #2003

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions dpctl/tensor/_usmarray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1777,8 +1777,10 @@ cdef api object UsmNDArray_MakeSimpleFromPtr(
Returns:
Created usm_ndarray instance
"""
cdef size_t itemsize = type_bytesize(typenum)
cdef size_t nbytes = itemsize * nelems
cdef int itemsize = type_bytesize(typenum)
if (itemsize < 1):
raise ValueError("dtype with typenum=" + str(typenum) + " is not supported.")
cdef size_t nbytes = (<size_t> itemsize) * nelems
cdef c_dpmem._Memory mobj = c_dpmem._Memory.create_from_usm_pointer_size_qref(
ptr, nbytes, QRef, memory_owner=owner
)
Expand Down Expand Up @@ -1817,7 +1819,7 @@ cdef api object UsmNDArray_MakeFromPtr(
Returns:
Created usm_ndarray instance
"""
cdef size_t itemsize = type_bytesize(typenum)
cdef int itemsize = type_bytesize(typenum)
cdef int err = 0
cdef size_t nelems = 1
cdef Py_ssize_t min_disp = 0
Expand All @@ -1830,6 +1832,8 @@ cdef api object UsmNDArray_MakeFromPtr(
cdef object obj_shape
cdef object obj_strides

if (itemsize < 1):
raise ValueError("dtype with typenum=" + str(typenum) + " is not supported.")
if (nd < 0):
raise ValueError("Dimensionality must be non-negative")
if (ptr is NULL or QRef is NULL):
Expand Down Expand Up @@ -1881,7 +1885,7 @@ cdef api object UsmNDArray_MakeFromPtr(
raise ValueError(
"Given shape, strides and offset reference out-of-bound memory"
)
nbytes = itemsize * (offset + max_disp + 1)
nbytes = (<size_t> itemsize) * (offset + max_disp + 1)
mobj = c_dpmem._Memory.create_from_usm_pointer_size_qref(
ptr, nbytes, QRef, memory_owner=owner
)
Expand Down
Loading