ZCBOR encode function returns success in case where cbor output is not correct.

Hardware is nrf5340DKI

nrfconnect v2.6.0

zcbor library 0.8.1.

I'm running a project on the nrf5340 DK that is using the zcbor library to encode log events. The cddl definition includes a couple 64 byte integers for a timestamp and id, and then a bstr object to hold addition event details. I am encountering an issue when one of the 64 bit fields has a value that only needs 16 bits.

In this case, the encode function returns success, and a reasonable length for the cbor output. But then passing that output to the decode function fails. I can resolve the issue by passing a larger buffer for the cbor output. However it's concerning that the library doesn't return an error if the buffer I passed wasn't large enough.

I'm attaching an application for the nrf5340DK to demonstrate this issue. This app runs 4 Test Cases:

Test Case Value stored in uint64 field Buffer size for CBOR Output Result Encode function Return Value  Decode function Return Value   
1 0x27bff
(> 16 bits)
64 OK 0 0 OK
2 0x9f00
(16 bits)
64 OK 0 0 OK
3 0x27bff
(> 16 bits)
56 OK 0 0 OK
4 0x9f00
(16 bits)
56 CBOR Output Error 0 9 CBOR output incorrect

The CBOR output string is the same for Test Cases 1 and 3.

The CBOR output for Test Case 2 and 4 differ by 2 bytes. A 0xbf type header at the beginning of the bstr object and the length of the bstr. 
bf6373657618286363617402627473199f006269641baaaaaaaa00000c5e61654bbf626d76194763616300ffff
bf6373657618286363617402627473199f006269641baaaaaaaa00000c5e61654a626d76194763616300ffff

It would be interesting to understand why this happens, but I think my biggest concern is that the encode function returned a success code when the output is clearly not correct.

I'm fairly new to using zephy and very new to cbor. So it is certainly possible that I'm not using the library correctly. but it certainly appears that there is a bug.

The application will output the following:  Which includes the CBOR output string, and Pass if the encode and decode functions returned success

*** Booting nRF Connect SDK d96769faceca ***
Start
CBOR: bf63736576182863636174026274731a00027bff6269641baaaaaaaa00000c5e61654bbf626d76194763616300ffff
Test Case 0: Passed verification

CBOR: bf6373657618286363617402627473199f006269641baaaaaaaa00000c5e61654bbf626d76194763616300ffff
Test Case 1: Passed verification

CBOR: bf63736576182863636174026274731a00027bff6269641baaaaaaaa00000c5e61654bbf626d76194763616300ffff
Test Case 2: Passed verification

CBOR: bf6373657618286363617402627473199f006269641baaaaaaaa00000c5e61654a626d76194763616300ffff
Test Case 3: Failed verification

It May, or may not, be helpful. But from my debugging it looks like the output gets built correctly in memory and then when one of the backup states are restored, the data gets shifted and overwrites the 0xbf. It also doesn't appear that it over runs the end of the passed buffer, so it's not clear why a larger buffer resolves the issue.
I can provide a complete project for the nrf5340 DK that demonstrates this error.

cborEncodeIssue.tar.bz2

Related