Skip to content

Commit 17c2209

Browse files
committed
Add check for narrowing cast;
Signed-off-by: Lukas Sommer <[email protected]>
1 parent 5b5bb82 commit 17c2209

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

sycl/plugins/hip/pi_hip.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,19 @@ pi_result hip_piMemGetInfo(pi_mem memObj, pi_mem_info queriedInfo,
21312131
/// \return PI_SUCCESS
21322132
pi_result hip_piextMemGetNativeHandle(pi_mem mem,
21332133
pi_native_handle *nativeHandle) {
2134+
if (sizeof(_pi_mem::mem_::buffer_mem_::native_type) >
2135+
sizeof(pi_native_handle)) {
2136+
// Check that all the upper bits that cannot be represented by
2137+
// pi_native_handle are empty.
2138+
// NOTE: The following shift might trigger a warning, but the check in the
2139+
// if above makes sure that this does not underflow.
2140+
_pi_mem::mem_::buffer_mem_::native_type upperBits =
2141+
mem->mem_.buffer_mem_.get() >> (sizeof(pi_native_handle) * CHAR_BIT);
2142+
if (upperBits) {
2143+
// Return an error if any of the remaining bits is non-zero.
2144+
return PI_INVALID_MEM_OBJECT;
2145+
}
2146+
}
21342147
*nativeHandle = static_cast<pi_native_handle>(mem->mem_.buffer_mem_.get());
21352148
return PI_SUCCESS;
21362149
}

0 commit comments

Comments
 (0)