Skip to content

Commit 20a4809

Browse files
Damian-Nordicmaciejbaczmanski
authored andcommitted
net: openthread: rpc: handle NULL service subtype array
The subtype array provided to otSrpClientAddService can be NULL but the RPC client would try to always access the array. Signed-off-by: Damian Krolik <[email protected]>
1 parent 09ecf06 commit 20a4809

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-7
lines changed

subsys/net/openthread/rpc/client/ot_rpc_srp_client.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ static void calc_subtypes_space(const char *const *subtypes, size_t *out_count,
5454
size_t count = 0;
5555
size_t cbor_size = 0;
5656

57-
for (; *subtypes; ++subtypes) {
58-
++count;
59-
cbor_size += 2 + strlen(*subtypes);
57+
if (subtypes) {
58+
for (; *subtypes; ++subtypes) {
59+
++count;
60+
cbor_size += 2 + strlen(*subtypes);
61+
}
6062
}
6163

6264
*out_count = count;
@@ -176,10 +178,15 @@ otError otSrpClientAddService(otInstance *aInstance, otSrpClientService *aServic
176178
calc_txt_space(aService->mTxtEntries, aService->mNumTxtEntries, &txt_size);
177179

178180
cbor_buffer_size = 1 + sizeof(uintptr_t); /* Service pointer */
181+
cbor_buffer_size += 1 + sizeof(num_subtypes);
182+
cbor_buffer_size += 1 + sizeof(aService->mNumTxtEntries);
183+
cbor_buffer_size += 1 + sizeof(name_len + instance_len + 2);
184+
cbor_buffer_size += 1 + sizeof(subtypes_size);
185+
cbor_buffer_size += 1 + sizeof(txt_size);
179186
cbor_buffer_size += 2 + name_len;
180187
cbor_buffer_size += 2 + instance_len;
181-
cbor_buffer_size += 1 + subtypes_size; /* Array of service subtypes */
182-
cbor_buffer_size += 1 + txt_size; /* Map of TXT entries */
188+
cbor_buffer_size += 2 + subtypes_size; /* Array of service subtypes */
189+
cbor_buffer_size += 2 + txt_size; /* Map of TXT entries */
183190
cbor_buffer_size += 1 + sizeof(aService->mPort);
184191
cbor_buffer_size += 1 + sizeof(aService->mPriority);
185192
cbor_buffer_size += 1 + sizeof(aService->mWeight);
@@ -199,8 +206,11 @@ otError otSrpClientAddService(otInstance *aInstance, otSrpClientService *aServic
199206

200207
zcbor_list_start_encode(ctx.zs, num_subtypes);
201208

202-
for (const char *const *subtype = aService->mSubTypeLabels; *subtype != NULL; ++subtype) {
203-
nrf_rpc_encode_str(&ctx, *subtype, -1);
209+
if (aService->mSubTypeLabels != NULL) {
210+
for (const char *const *subtype = aService->mSubTypeLabels; *subtype != NULL;
211+
++subtype) {
212+
nrf_rpc_encode_str(&ctx, *subtype, -1);
213+
}
204214
}
205215

206216
zcbor_list_end_encode(ctx.zs, num_subtypes);

tests/subsys/net/openthread/rpc/client/src/srp_client_suite.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,36 @@
5151
* with indefinite lengths, which is true unless ZCBOR_CANONICAL Kconfig is not selected.
5252
*/
5353

54+
/* clang-format off */
55+
#define CBOR_SERVICE_MIN \
56+
/* Subtypes num: */ \
57+
0x0, \
58+
/* TXT entry count: */ \
59+
0x0, \
60+
/* String buffer size (service + instance): */ \
61+
0xb, \
62+
/* Subtypes' buffer size: */ \
63+
0x0, \
64+
/* TXT buffer size: */ \
65+
0x0, \
66+
/* Service type: */ \
67+
0x65, SERVICE_TYPE, \
68+
/* Service instance: */ \
69+
0x64, SERVICE_INSTANCE, \
70+
/* Subtypes: */ \
71+
0x9f, \
72+
0xff, \
73+
/* TXT: */ \
74+
0xbf, \
75+
0xff, \
76+
/* Other fields: */ \
77+
CBOR_UINT16(PORT_1), \
78+
CBOR_UINT16(SERVICE_PRIORITY), \
79+
CBOR_UINT16(SERVICE_WEIGHT), \
80+
CBOR_UINT32(SERVICE_LEASE), \
81+
CBOR_UINT32(SERVICE_KEY_LEASE)
82+
/* clang-format on */
83+
5484
/* clang-format off */
5585
#define CBOR_SERVICE \
5686
/* Subtypes num: */ \
@@ -118,6 +148,33 @@ static void tc_after(void *f)
118148
mock_nrf_rpc_tr_expect_done();
119149
}
120150

151+
/* Test serialization of otSrpClientAddService() with the minimal service data */
152+
ZTEST(ot_rpc_srp_client, test_otSrpClientAddService_min)
153+
{
154+
otError error;
155+
otSrpClientService service;
156+
157+
service.mName = MAKE_CSTR(SERVICE_TYPE);
158+
service.mInstanceName = MAKE_CSTR(SERVICE_INSTANCE);
159+
service.mSubTypeLabels = NULL;
160+
service.mTxtEntries = NULL;
161+
service.mPort = PORT_1;
162+
service.mPriority = SERVICE_PRIORITY;
163+
service.mWeight = SERVICE_WEIGHT;
164+
service.mNumTxtEntries = 0;
165+
service.mLease = SERVICE_LEASE;
166+
service.mKeyLease = SERVICE_KEY_LEASE;
167+
168+
/* Test serialization of otSrpClientAddService() */
169+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_SRP_CLIENT_ADD_SERVICE,
170+
CBOR_UINT32((uintptr_t)&service), CBOR_SERVICE_MIN),
171+
RPC_RSP(OT_ERROR_NONE));
172+
error = otSrpClientAddService(NULL, &service);
173+
mock_nrf_rpc_tr_expect_done();
174+
175+
zassert_equal(error, OT_ERROR_NONE);
176+
}
177+
121178
/* Test serialization of otSrpClientAddService() followed by otSrpClientClearService() */
122179
ZTEST(ot_rpc_srp_client, test_otSrpClientAddService_otSrpClientClearService)
123180
{

0 commit comments

Comments
 (0)