Skip to content

CBOR Integer Encoding Length Fix #1570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2021

Conversation

davertay
Copy link
Contributor

@davertay davertay commented Jun 24, 2021

This PR fixes an issue where the packed length is sub-optimal for all positive integers that have the MSB bit set.

CBOR integers are packed with a set number of bytes, bucketed by length (8bit, 16bit, 32bit etc) determined by the bit length of the source. Negative integers are denoted with a different Major Type (1 for -ve, 0 for +ve) but are otherwise treated as an unsigned value when packing/unpacking.

The original code bucketing check uses the signed MAX_VALUE constants which means any values with the MSB set will fall into the next highest bucket, and thus pack into double the expected number of bytes.

e.g. 65535 (uint16, all bits high) is encoded with two extra zero bytes as 0x19 0x00 0x00 0xFF 0xFF whereas it should encode to 0x19 0xFF 0xFF.

The RFC does not explicitly state that integers must always pack to the fewest bytes possible, and the format certainly allows it so it is still valid CBOR. e.g. you could pack the value 1 into the space large enough for a 64bit integer and it can still be decoded successfully. However this is certainly at odds with the "C" in "CBOR".

@davertay davertay changed the title Davertay/cbor numbers fix CBOR Integer Encoding Length Fix Jun 24, 2021
@qwwdfsad qwwdfsad requested a review from shanshin June 25, 2021 09:09
@sandwwraith sandwwraith requested review from sandwwraith and removed request for shanshin July 6, 2021 13:56
Copy link
Member

@sandwwraith sandwwraith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks for your help!

@sandwwraith sandwwraith merged commit ab5bbb0 into Kotlin:dev Jul 6, 2021
woainikk pushed a commit that referenced this pull request Aug 12, 2021
by using unsigned numbers for determining buffer size

(cherry picked from commit ab5bbb0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants