Handle failed memory allocations (#426)

The test has been ignoring potential failures in memory allocations,
giving misleading error messages.

Add a simple check and return early if the allocation failed.

Signed-off-by: Radek Szymanski <radek.szymanski@arm.com>
This commit is contained in:
Radek Szymanski
2019-08-05 15:04:46 +01:00
committed by Kévin Petit
parent 313fb94fcc
commit 358ad78344
2 changed files with 30 additions and 0 deletions

View File

@@ -1358,6 +1358,13 @@ int test_min_max_constant_buffer_size(cl_device_id deviceID, cl_context context,
size_t sizeToAllocate = ((size_t)currentSize/sizeof( cl_int ))*sizeof(cl_int);
size_t numberOfInts = sizeToAllocate/sizeof(cl_int);
constantData = (cl_int *)malloc( sizeToAllocate);
if (constantData == NULL)
{
log_error("Failed to allocate memory for constantData!\n");
free_mtdata(d);
return EXIT_FAILURE;
}
for(i=0; i<(int)(numberOfInts); i++)
constantData[i] = (int)genrand_int32(d);
@@ -1411,6 +1418,14 @@ int test_min_max_constant_buffer_size(cl_device_id deviceID, cl_context context,
allocPassed = 1;
resultData = (cl_int *)malloc(sizeToAllocate);
if (resultData == NULL)
{
log_error("Failed to allocate memory for resultData!\n");
free(constantData);
free_mtdata(d);
return EXIT_FAILURE;
}
error = clEnqueueReadBuffer(queue, streams[1], CL_TRUE, 0, sizeToAllocate, resultData, 0, NULL, NULL);
test_error( error, "clEnqueueReadBuffer failed");

View File

@@ -1353,6 +1353,13 @@ int test_min_max_constant_buffer_size(cl_device_id deviceID, cl_context context,
size_t sizeToAllocate = ((size_t)currentSize/sizeof( cl_int ))*sizeof(cl_int);
size_t numberOfInts = sizeToAllocate/sizeof(cl_int);
constantData = (cl_int *)malloc( sizeToAllocate);
if (constantData == NULL)
{
log_error("Failed to allocate memory for constantData!\n");
free_mtdata(d);
return EXIT_FAILURE;
}
for(i=0; i<(int)(numberOfInts); i++)
constantData[i] = (int)genrand_int32(d);
@@ -1405,6 +1412,14 @@ int test_min_max_constant_buffer_size(cl_device_id deviceID, cl_context context,
allocPassed = 1;
resultData = (cl_int *)malloc(sizeToAllocate);
if (resultData == NULL)
{
log_error("Failed to allocate memory for resultData!\n");
free(constantData);
free_mtdata(d);
return EXIT_FAILURE;
}
error = clEnqueueReadBuffer(queue, streams[1], CL_TRUE, 0, sizeToAllocate, resultData, 0, NULL, NULL);
test_error( error, "clEnqueueReadBuffer failed");