Fix handling of extraneous data in Unmarshal() & Valid() #380
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously,
Unmarshal()
andValid()
ignored trailing bytes (if any) after the current CBOR data item. This matched the behavior ofDecoder.Decode()
.However,
Unmarshal()
andValid()
are typically used for single CBOR data item, so trailing bytes (if present) should return ExtraneousDataError given what RFC 8949 says:This is a bug fix rather than a new non-default option because the previous behavior of ignoring extra data didn't exactly match RFC 8949 and was also different from how
Marshal()
inencoding/json
handles extraneous data.Special thanks to @zensh for reporting this issue and proposing a solution.
Closes #359
How to handle extraneous data as non-errors
Decoder.Decode()
andUnmarshalFirst()
can handle extraneous data as non-errors.Decoder.Decode()
usesio.Reader
and ignores remaining bytes after reading a single CBOR data item. By design, it does not treat extraneous data as an error.🆕
UnmarshalFirst()
will decode first CBOR data item and return trailing bytes (if any). By design, it will not treat extraneous data as an error.Decoder.Decode()
andUnmarshalFirst()
are also useful for decoding CBOR Sequences (RFC 8742).