Skip to content

Commit f611a03

Browse files
committed
Fixed coverity in acl_device.cpp: Argument cannot be negative (NEGATIVE_RETURNS)
`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 f611a03

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/acl_device.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,14 @@ 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 =
215-
ACL_RANGE_SIZE(device->def.autodiscovery_def.global_mem_defs[gmem_id]
218+
ACL_RANGE_SIZE(device->def.autodiscovery_def
219+
.global_mem_defs[static_cast<size_t>(gmem_id)]
216220
.get_usable_range());
217221
#ifdef __arm__
218222
// on SoC board, two DDR systems are not equivalent
@@ -255,10 +259,14 @@ CL_API_ENTRY cl_int CL_API_CALL clGetDeviceInfoIntelFPGA(
255259
// However conformance_test_api min_max_constant_buffer_size
256260
// expects to allocate two buffers of the size we say here.
257261
// 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));
262+
auto gmem_id = acl_get_default_device_global_memory(device->def);
263+
if (gmem_id < 0) {
264+
RESULT_INT(0);
265+
break;
266+
}
260267
cl_ulong size =
261-
ACL_RANGE_SIZE(device->def.autodiscovery_def.global_mem_defs[gmem_id]
268+
ACL_RANGE_SIZE(device->def.autodiscovery_def
269+
.global_mem_defs[static_cast<size_t>(gmem_id)]
262270
.get_usable_range()) /
263271
4;
264272
#ifdef __arm__
@@ -268,10 +276,14 @@ CL_API_ENTRY cl_int CL_API_CALL clGetDeviceInfoIntelFPGA(
268276
RESULT_ULONG(size);
269277
} break;
270278
case CL_DEVICE_MAX_MEM_ALLOC_SIZE: {
271-
auto gmem_id =
272-
static_cast<size_t>(acl_get_default_device_global_memory(device->def));
279+
auto gmem_id = acl_get_default_device_global_memory(device->def);
280+
if (gmem_id < 0) {
281+
RESULT_INT(0);
282+
break;
283+
}
273284
cl_ulong size =
274-
ACL_RANGE_SIZE(device->def.autodiscovery_def.global_mem_defs[gmem_id]
285+
ACL_RANGE_SIZE(device->def.autodiscovery_def
286+
.global_mem_defs[static_cast<size_t>(gmem_id)]
275287
.get_usable_range());
276288
#ifdef __arm__
277289
// on SoC board, two DDR systems are not equivalent

0 commit comments

Comments
 (0)