bergzand / NanoCBOR

CBOR library aimed at heavily constrained devices

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nanocbor_skip() doesn't work on nested structures

nattelog opened this issue · comments

According to its documentation it should:

"skip over nested structures in the CBOR stream such as (nested) arrays and maps"

However it doesn't seem to work. A simple test case such as the following fails:

nanocbor_value_t decoder;
uint8_t byteval = 0xA0; /* empty map, size 0 */

nanocbor_decoder_init(&decoder, &byteval, sizeof(byteval));
nanocbor_skip(&decoder);

CU_ASSERT_EQUAL(&byteval + 1, decoder.cur);

Investigating this I found something suspicious in _skip_limited. The part that recursively skips the content of the map/array is inside a conditional branch which is never entered. It checks if the return code from nanocbor_enter_map/array is > 0, which it never is, since those function either returns something < 0 or 0. Changing that expression to == 0, the test case works.

diff --git a/src/decoder.c b/src/decoder.c
index 6b69405..51a07cc 100644
--- a/src/decoder.c
+++ b/src/decoder.c
@@ -365,7 +365,7 @@ static int _skip_limited(nanocbor_value_t *it, uint8_t limit)
         res = (type == NANOCBOR_TYPE_MAP
                ? nanocbor_enter_map(it, &recurse)
                : nanocbor_enter_array(it, &recurse));
-        if (res > 0) {
+        if (res == 0) {
             while (!nanocbor_at_end(&recurse)) {
                 res = _skip_limited(&recurse, limit - 1);
                 if (res < 0) {

Is this actually an issue or have I misunderstood something about skip on nested structures?

Thanks for the extensive bug report, I'll investigate later today!

The issue should be fixed with #47. Feel free to reopen this if the issue persists. Thanks again for reporting!

Awesome, thanks!