@@ -127,7 +127,8 @@ void TestPowerSourceCluster::TestEndpointList(nlTestSuite * apSuite, void * apCo
127
127
NL_TEST_ASSERT (apSuite, vec.size () == 0 );
128
128
}
129
129
130
- if (powerSourceServer.GetNumSupportedEndpointLists () < 2 )
130
+ if (powerSourceServer.GetNumSupportedEndpointLists () < 2 ||
131
+ powerSourceServer.GetNumSupportedEndpointLists () > std::numeric_limits<uint16_t >::max ())
131
132
{
132
133
// Test assumes at least two endpoints. This runs on linux, not worthwhile to run on platforms with fewer endpoints.
133
134
return ;
@@ -140,6 +141,11 @@ void TestPowerSourceCluster::TestEndpointList(nlTestSuite * apSuite, void * apCo
140
141
EndpointId list1[10 ] = { 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 };
141
142
EndpointId listRest[1 ] = { 2 };
142
143
144
+ // we checked earlier that this fit
145
+ // This test just uses endpoints in order, so we want to set endpoints from
146
+ // 0 to numEndpoints - 1, and use this for overflow checking
147
+ EndpointId numEndpoints = static_cast <EndpointId>(powerSourceServer.GetNumSupportedEndpointLists ());
148
+
143
149
// Endpoint 0 - list of 5
144
150
err = powerSourceServer.SetEndpointList (0 , Span<EndpointId>(list0));
145
151
NL_TEST_ASSERT (apSuite, err == CHIP_NO_ERROR);
@@ -163,7 +169,7 @@ void TestPowerSourceCluster::TestEndpointList(nlTestSuite * apSuite, void * apCo
163
169
}
164
170
165
171
// Remaining endpoints - list of 1
166
- for (EndpointId ep = 2 ; ep < powerSourceServer. GetNumSupportedEndpointLists () ; ++ep)
172
+ for (EndpointId ep = 2 ; ep < numEndpoints ; ++ep)
167
173
{
168
174
err = powerSourceServer.SetEndpointList (ep, Span<EndpointId>(listRest));
169
175
NL_TEST_ASSERT (apSuite, err == CHIP_NO_ERROR);
@@ -205,7 +211,7 @@ void TestPowerSourceCluster::TestEndpointList(nlTestSuite * apSuite, void * apCo
205
211
}
206
212
207
213
// Remaining endpoints
208
- for (EndpointId ep = 2 ; ep < powerSourceServer. GetNumSupportedEndpointLists () ; ++ep)
214
+ for (EndpointId ep = 2 ; ep < numEndpoints ; ++ep)
209
215
{
210
216
readBack = powerSourceServer.GetEndpointList (ep);
211
217
NL_TEST_EXIT_ON_FAILED_ASSERT (apSuite, readBack != nullptr );
@@ -230,7 +236,7 @@ void TestPowerSourceCluster::TestEndpointList(nlTestSuite * apSuite, void * apCo
230
236
}
231
237
232
238
// Ensure only the overwritten list was changed, using read interface
233
- for (EndpointId ep = 0 ; ep < powerSourceServer. GetNumSupportedEndpointLists () + 1 ; ++ep)
239
+ for (EndpointId ep = 0 ; ep < numEndpoints + 1 ; ++ep)
234
240
{
235
241
std::vector<EndpointId> vec = ReadEndpointsThroughAttributeReader (apSuite, ep);
236
242
if (ep == 0 )
@@ -241,7 +247,7 @@ void TestPowerSourceCluster::TestEndpointList(nlTestSuite * apSuite, void * apCo
241
247
NL_TEST_ASSERT (apSuite, vec[j] == list0[j]);
242
248
}
243
249
}
244
- else if (ep == powerSourceServer. GetNumSupportedEndpointLists () )
250
+ else if (ep == numEndpoints )
245
251
{
246
252
NL_TEST_ASSERT (apSuite, vec.size () == 0 );
247
253
}
@@ -258,7 +264,7 @@ void TestPowerSourceCluster::TestEndpointList(nlTestSuite * apSuite, void * apCo
258
264
// *****************
259
265
// Test removal
260
266
// *****************
261
- for (EndpointId ep = 0 ; ep < powerSourceServer. GetNumSupportedEndpointLists () ; ++ep)
267
+ for (EndpointId ep = 0 ; ep < numEndpoints ; ++ep)
262
268
{
263
269
err = powerSourceServer.SetEndpointList (ep, Span<EndpointId>());
264
270
NL_TEST_ASSERT (apSuite, err == CHIP_NO_ERROR);
@@ -267,7 +273,7 @@ void TestPowerSourceCluster::TestEndpointList(nlTestSuite * apSuite, void * apCo
267
273
}
268
274
269
275
// Check through the read interface
270
- for (EndpointId ep = 0 ; ep < powerSourceServer. GetNumSupportedEndpointLists () + 1 ; ++ep)
276
+ for (EndpointId ep = 0 ; ep < numEndpoints + 1 ; ++ep)
271
277
{
272
278
std::vector<EndpointId> vec = ReadEndpointsThroughAttributeReader (apSuite, ep);
273
279
NL_TEST_ASSERT (apSuite, vec.size () == 0 );
0 commit comments