Skip to content

Commit a99d951

Browse files
clGetPlatformIDs should check if platform initialization was successful
Change-Id: I3e9d78155e6a914ed0d755d81ddc13c4d3a8a291
1 parent 5909a6b commit a99d951

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

runtime/api/api.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,23 @@ cl_int CL_API_CALL clGetPlatformIDs(cl_uint numEntries,
7777
}
7878

7979
// if the platforms are non-nullptr, we need to fill in the platform IDs
80-
if (platforms != nullptr) {
80+
while (platforms != nullptr) {
8181
auto pPlatform = platform();
8282
bool ret = pPlatform->initialize(numPlatformDevices, platformDevices);
8383
DEBUG_BREAK_IF(ret != true);
84-
((void)(ret));
84+
if (!ret) {
85+
retVal = CL_INVALID_VALUE;
86+
break;
87+
}
8588

8689
// we only have one platform so we can program that directly
8790
platforms[0] = pPlatform;
91+
break;
8892
}
8993

9094
// we only have a single platform at this time, so return 1 if num_platforms
9195
// is non-nullptr
92-
if (numPlatforms) {
96+
if (numPlatforms && retVal == CL_SUCCESS) {
9397
*numPlatforms = 1;
9498
}
9599
} while (false);

unit_tests/api/cl_get_platform_ids_tests.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@
2323
#include "cl_api_tests.h"
2424
#include "runtime/context/context.h"
2525
#include "runtime/platform/platform.h"
26+
#include "unit_tests/helpers/variable_backup.h"
2627

2728
using namespace OCLRT;
2829

30+
namespace OCLRT {
31+
extern bool getDevicesResult;
32+
}; // namespace OCLRT
33+
2934
typedef api_tests clGetPlatformIDsTests;
3035

3136
namespace ULT {
@@ -57,4 +62,21 @@ TEST_F(clGetPlatformIDsTests, NoPlatformListReturnsError) {
5762

5863
EXPECT_EQ(CL_INVALID_VALUE, retVal);
5964
}
65+
66+
TEST(clGetPlatformIDsNegativeTests, WhenInitFailedThenErrorIsReturned) {
67+
VariableBackup<decltype(getDevicesResult)> bkp(&getDevicesResult);
68+
bkp = false;
69+
70+
cl_int retVal = CL_SUCCESS;
71+
cl_platform_id platformRet = nullptr;
72+
cl_uint numPlatforms = 0;
73+
74+
retVal = clGetPlatformIDs(1, &platformRet, &numPlatforms);
75+
76+
EXPECT_EQ(CL_INVALID_VALUE, retVal);
77+
EXPECT_EQ(0u, numPlatforms);
78+
EXPECT_EQ(nullptr, platformRet);
79+
80+
platform()->shutdown();
81+
}
6082
} // namespace ULT

0 commit comments

Comments
 (0)