Skip to content

Commit b5c6a5f

Browse files
committed
Added some decode tests
1 parent 0d1f285 commit b5c6a5f

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

ArduinoCloudThing.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,29 +214,40 @@ void ArduinoCloudThing::decode(uint8_t *payload, size_t length) {
214214
// parse cbor object
215215
cbor_value_enter_container(&dataArray, &recursedMap);
216216

217-
CborType type = cbor_value_get_type(&recursedMap);
218-
if (type != CborMapType) {
217+
if (cbor_value_get_type(&recursedMap) != CborMapType) {
219218
// stop the decode when 1st item thai is not a cbor map is found.
220-
cbor_value_advance(&dataArray);
219+
err = cbor_value_advance(&dataArray);
220+
// avoid infinite loop if it is not possible to advance to the next array value
221+
if (err != CborNoError) {
222+
break;
223+
}
224+
// go to the next element
221225
continue;
222226

223227
} else {
224-
225228
while (!cbor_value_at_end(&recursedMap)) {
226229

227230
// if the current element is not a cbor object as expected, skip it and go ahead.
228231
if(cbor_value_get_type(&recursedMap) != CborMapType) {
229-
cbor_value_advance(&recursedMap);
232+
233+
err = cbor_value_advance(&recursedMap);
234+
if (err != CborNoError) {
235+
break;
236+
}
230237
continue;
231238
}
232239

233240
CborValue name;
234241
// chechk for the if the a property has a name, if yes Cbor value name will properly updated
235242
cbor_value_map_find_value(&recursedMap, "n", &name);
236243

237-
// check if a property has a name, of string type, if not do nothin and skip curtrent property
244+
// check if a property has a name, of string type, if not do nothing and skip curtrent property
238245
if (name.type != CborTextStringType) {
239-
cbor_value_advance(&recursedMap);
246+
err = cbor_value_advance(&recursedMap);
247+
// problem to advance to the next array object
248+
if (err != CborNoError)
249+
break;
250+
240251
continue;
241252
}
242253

@@ -307,12 +318,20 @@ void ArduinoCloudThing::decode(uint8_t *payload, size_t length) {
307318
if (property->callback != NULL) {
308319
property->callback();
309320
}
310-
}
321+
}
322+
311323
// Continue to scan the cbor map
312-
cbor_value_advance(&recursedMap);
324+
err = cbor_value_advance(&recursedMap);
325+
if (err != CborNoError) {
326+
break;
327+
}
313328
}
314329
}
330+
331+
if (err != CborNoError)
332+
break;
315333
// Leave the current cbor object, and advance to the next one
316-
err = cbor_value_leave_container(&dataArray, &recursedMap);
334+
cbor_value_leave_container(&dataArray, &recursedMap);
335+
317336
}
318337
}

0 commit comments

Comments
 (0)