Skip to content

Commit 8633df2

Browse files
committed
acl_device.cpp: fixed coverity Argument cannot be negative
`acl_get_default_device_global_memory` can return `-1` if no device memory is found. By static_casting it to `size_t`, it makes the -1 into something irrationally big. Therefore, after returning from the function, I added a check to see if `gmem_id` is negative. If it is negative, then I return 0. I assumed that no device private memory means that by default the sizes of global memory, of max constant buffer, and of max device memory allocation are all `0`.
1 parent 7de03cb commit 8633df2

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/acl_device.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,11 @@ CL_API_ENTRY cl_int CL_API_CALL clGetDeviceInfoIntelFPGA(
209209
RESULT_INT(0);
210210
break;
211211
case CL_DEVICE_GLOBAL_MEM_SIZE: {
212-
auto gmem_id =
213-
static_cast<size_t>(acl_get_default_device_global_memory(device->def));
212+
auto gmem_id = acl_get_default_device_global_memory(device->def);
213+
if (gmem_id < 0) {
214+
RESULT_INT(0);
215+
break;
216+
}
214217
cl_ulong size =
215218
ACL_RANGE_SIZE(device->def.autodiscovery_def.global_mem_defs[gmem_id]
216219
.get_usable_range());
@@ -255,8 +258,11 @@ CL_API_ENTRY cl_int CL_API_CALL clGetDeviceInfoIntelFPGA(
255258
// However conformance_test_api min_max_constant_buffer_size
256259
// expects to allocate two buffers of the size we say here.
257260
// So be a shade conservative and cut it down by 4.
258-
auto gmem_id =
259-
static_cast<size_t>(acl_get_default_device_global_memory(device->def));
261+
auto gmem_id = acl_get_default_device_global_memory(device->def);
262+
if (gmem_id < 0) {
263+
RESULT_INT(0);
264+
break;
265+
}
260266
cl_ulong size =
261267
ACL_RANGE_SIZE(device->def.autodiscovery_def.global_mem_defs[gmem_id]
262268
.get_usable_range()) /
@@ -268,8 +274,11 @@ CL_API_ENTRY cl_int CL_API_CALL clGetDeviceInfoIntelFPGA(
268274
RESULT_ULONG(size);
269275
} break;
270276
case CL_DEVICE_MAX_MEM_ALLOC_SIZE: {
271-
auto gmem_id =
272-
static_cast<size_t>(acl_get_default_device_global_memory(device->def));
277+
auto gmem_id = acl_get_default_device_global_memory(device->def);
278+
if (gmem_id < 0) {
279+
RESULT_INT(0);
280+
break;
281+
}
273282
cl_ulong size =
274283
ACL_RANGE_SIZE(device->def.autodiscovery_def.global_mem_defs[gmem_id]
275284
.get_usable_range());

0 commit comments

Comments
 (0)