Skip to content

Releases

v2.10.0

Choose a tag to compare

@fxamacker fxamacker released this 28 Oct 15:59
cc2e8fb

v2.10 speeds up encoding by up to 2.88× and decoding by up to 1.87× with zero memory regressions (B/op, allocs/op), reduces memory use, adds the toindefarray struct tag option, and fixes bugs. The Contributing Guidelines, Security Policy, and templates were extensively updated.

Encoding sorted nested maps of dynamic JSON-like data to CBOR drops from 70 allocs/op to 1 and from 7.2 KiB/op to 4.0 KiB/op in the benchmarks below. Encoding time.Time, big.Int, Tag and Marshaler values, and decoding into time.Time, big.Int and Tag, use 1 to 2 fewer allocs/op (20% to 50% fewer).

This release does not use Go's unsafe package. Performance in user apps depends on configuration and data.

Tests were refactored and coverage increased to 97.7%. Fuzz tests (not yet public) were also extended. Internal checklists and procedures were updated to reduce potential mistakes.

Performance and Benchmarks

Across all 215 benchmarks, there are zero memory regressions (B/op, allocs/op). Just one benchmark showed a speed dip: Marshal/struct_many_flds_all_omitempty_all_empty/map is ~4-5% slower, but only when a struct has 32 fields that are both omitzero and empty (same scenario with an 8-field struct is now faster).

Benchmarks ran with Go 1.26.10 on linux/amd64 (benchstat n=10, p=0.000):

  • CBOR Speedup by Data Type: groups of different benchmarks summarized per row
  • CBOR Encoding Reflection-Free Speedup: dynamic JSON-like data types and common types

CBOR Speedup by Data Type

Unmarshal Speedup ΔSec/op
Single values (bool, int, float, byte string, text string) 1.10× to 1.51×¹ -8.87% to -33.92%¹
Epoch time, bignum, other tags 1.18× to 1.64× -15.33% to -38.91%
Arrays and maps to non-struct 1.33× to 1.87× -24.63% to -46.49%
Arrays and maps to structs 1.06× to 1.52× -5.46% to -34.40%
Real-world (COSE, CWT, SenML, WebAuthn) 1.35× to 1.64× -26.06% to -39.19%

¹ 2.54× speedup (-60.64%) when decoding into a named []byte type that has methods.

Marshal Speedup ΔSec/op
Single values (bool, int, float, byte slice, string) 1.32× to 1.55× -24.49% to -35.41%
time.Time, big.Int, Tag, Marshaler² 1.15× to 1.63× -12.94% to -38.84%
Slices and maps 1.35× to 2.88× -25.97% to -65.26%
Structs 1.57× to 2.17× -36.37% to -53.96%
Structs with omitempty fields 0.95׳ to 1.35× +5.01%³ to -25.96%
Real-world (COSE, CWT, SenML, WebAuthn) 1.12× to 1.33× -10.66% to -24.77%

² For addressable values (reached through a pointer, struct field, slice element or map value).

³ Only when every field is omitempty and empty: 7.6 ns slower for a 32-field struct, 2.37 ns faster for an 8-field struct. This is the only performance regression in all 215 benchmarks.

CBOR Encoding Reflection-Free Speedup

PR #870 optimized common types: int, int64, uint, uint64, float64, bool, string, nil, []byte, []any, map[any]any, map[string]any, and map[string]string.

Marshal Benchmark
v2.9.6 → v2.10
Speedup Time
ΔSec/op
Memory
ΔB/op
Memory
ΔAllocs/op
[]any 2.50× -607.5 ns (-60.03%) - -
map[string]any 2.60× -821.9 ns (-61.47%) - -
map[string]string 2.59× -335.9 ns (-61.34%) - -
nested_map[any]any 2.88× -16.542 µs (-65.26%) - -
nested_map[string]any 2.78× -14.201 µs (-64.01%) - -
nested_map[any]any, bw_lex 2.53× -22.36 µs (-60.54%) -3314 (-44.72%) -69 (-98.57%)
nested_map[string]any, bw_lex 2.41× -19.52 µs (-58.54%) -3323 (-44.79%) -69 (-98.57%)
  • bw_lex = SortBytewiseLexical. Memory improvements are from #774 and #860.

Upgrade Notes

  • Go 1.24 or newer is required (#773). All tests (97.7% coverage) pass on Go 1.24-1.27.
  • Pointer-receiver marshal methods (user-implemented methods) are called on the original value instead of a copy when the value is addressable (#866).
  • Marshal configured to use TagsForbidden returns an error for a big.Int outside [-2^64, 2^64-1], and EncMode creation returns an error for settings that would emit tags if TagsForbidden is also set (#868).
  • Decoder advances past well-formed data items rejected by DecOptions value or serialization restrictions (e.g. NaN, IndefLength), and Skip skips them without an error (#872).
  • Marshal configured to emit text-based time returns an error for time.Time outside RFC 3339 limits (#873).

TinyGo (experimental)

TinyGo support for v2.10.0 is available in tag tinygo-beta/v2.10.0-tinygo0.42 (branch feature/cbor-tinygo-beta). It requires TinyGo 0.42.0 or newer. It is experimental and not fuzz tested. For details, see TinyGo (https://github.com/fxamacker/cbor#tinygo) in the README.

What's Changed

⭐ Features

  • feat: add toindefarray struct tag option for CBOR indefinite-length arrays by @tdnguyenND in #770

🚀 Optimizations

  • Optimize encoding speed primarily for slices, maps, and structs by @fxamacker in #806
  • Reduce decoding time up to 17% for all CBOR data types by @fxamacker in #807
  • Reduce decoding time by inlining well-formedness checks by @fxamacker in #810
  • Optimize decoding CBOR byte string by @fxamacker in #811
  • Optimize decoding CBOR text string by up to 16% by @fxamacker in #812
  • Reduce allocs and bound memory of keyValue pool by @fxamacker in #860
  • Reduce allocations when encoding addressable values by @fxamacker in #866
  • Add reflection-free encoding for common types (up to 2.8x faster) by @fxamacker in #870
  • Reduce allocs to speed up time.Time, Tag, and big.Int by @fxamacker in #886

🛠 Improvements

🐞 Bug Fixes

  • Enforce TagsForbidden for bignums and ByteSliceLaterFormat by @fxamacker in #868
  • Advance Decoder past restricted CBOR data items by @fxamacker in #872
  • Reject marshaling out-of-range time.Time to RFC 3339 by @fxamacker in #873
  • Fix some error messages returned by EncMode() and DecMode() by @fxamacker in #883
  • Fix float range check when decoding Unix time by @fxamacker in #889

🧪 Testing

📖 Documentation

📖 Contributing Guidelines, Security Policy, and Templates

  • Update PR template to clarify approval requirement by @fxamacker in #849
  • Rewrite security policy and reporting guidelines by @fxamacker in #864
  • Rewrite PR template to reduce risks from AI and uncoordinated PRs by @fxamacker in #865
  • Rewrite bug report form to reduce security risks by @fxamacker in #867
  • Rewrite feature request form to help triage and reduce risks by @fxamacker in #869
  • Replace docs issue template with an issue form by @fxamacker in #874
  • Update security policy, PR template, and issue forms by @fxamacker in #875
  • Update CONTRIBUTING.md to reduce risks by @fxamacker in #862
  • Improve issue forms for triage and previews by @fxamacker in #880

CI / GitHub Actions

🔎 Details
Read more

v2.9.6

Choose a tag to compare

@fxamacker fxamacker released this 06 Oct 19:39
8604f5f

v2.9.6 fixes 12 bugs, including an important fix for apps that decode CBOR maps into Go maps, and has one behavior change. API is unchanged but more errors are detected.

Most of these bugs were identified while improving tests and reviewing code for an upcoming release.

Tests and fuzz tests were extended.

Upgrading to v2.9.6 from all prior versions is recommended.

🐞 Important Fix

For use cases outside the "not affected by this bug" list below, decoding a CBOR null or undefined map key or value (including 55799(null)) into a Go map whose key or value type is of bool, integer, float, or string kind kept the previous map entry's key or value (instead of the zero value). See PR #855 for details.

Not affected by this bug:

  • Decoding data this codec encoded from the same Go map type, because this codec does not encode to CBOR null from Go values of bool, integer, float, or string kind.
  • Apps that don't decode a CBOR null or undefined map key or value into a Go map whose key or value type is of bool, integer, float, or string kind.
  • Decoding CBOR maps into structs (struct fields of map type follow the bullet above).
  • Decoding CBOR maps into map[any]any, and into any (with unchanged DefaultMapType).

⚠️ Behavior change

Decoding CBOR null or undefined into an interface that holds a value now sets it to nil, matching encoding/json (v1 and v2). Previously, the interface kept its value (PR #827), which was not the intended behavior.

🛠 Bug fixes

Some of these bugs could only be triggered by incorrect usage in user apps, such as invalid struct tags or an unsupported DefaultByteStringType setting, etc. None of these bugs were reported by a project affected by them.

Decoding

  • Skip CBOR data item when destination struct has incorrect tags by @fxamacker in #821
  • Fix decoding CBOR null/undefined to interface value by @fxamacker in #827
  • Fix decoding to unaddressable value in interface by @fxamacker in #828
  • Improve the DefaultByteStringType check by @fxamacker in #829
  • Fix decoding to self-referencing types by @fxamacker in #831
  • Fix decoding 55799(null) to pointer or interface by @fxamacker in #851
  • Fix stale Go map entry from CBOR null key/value by @fxamacker in #855

Encoding

  • Fix encoding self-referencing types by @fxamacker in #833
  • Fix panic encoding marshaler of unsupported underlying types by @fxamacker in #834
  • Fix omitzero panic encoding struct field of unsupported underlying types implementing marshaler by @fxamacker in #835
  • Fix encoding TimeUnixMicro outside 1677 to 2262 by @fxamacker in #837
    • Encoded output also changes for some times within one second of either end of that range.
  • Reject encoding time.Time when Go formats wrong year by @fxamacker in #838

📖 Documentation

Full Changelog: v2.9.4...v2.9.6

v2.9.5

Choose a tag to compare

@fxamacker fxamacker released this 06 Oct 19:09
a1b077c

This version was tagged from the default branch by mistake and is retracted.

Use v2.9.6 (https://github.com/fxamacker/cbor/releases/tag/v2.9.6) instead.

v2.9.4

Choose a tag to compare

@fxamacker fxamacker released this 14 Sep 17:15
be0da0d

This release fixes potential panics when decoding a CBOR map whose key decodes to an uncomparable Go value under certain conditions.

Not affected: user apps that don't register tag types and don't use map key types that are either named empty interfaces (e.g. type MyAny any) or contain interfaces.

These potential panics can be encountered when one of these two conditions is met:

  • user application registers a tag type (via TagSet interface) that is an array or struct with uncomparable or interface{} element or field type, or
  • user application specifies a destination Go map whose key type is a named empty interface type or a type containing an interface.

This was identified while improving tests and reviewing code for an upcoming release.

Tests and fuzz tests were extended to cover this bug.

Upgrading to v2.9.4 is recommended.

What's Changed

  • Fix panic decoding uncomparable map keys under certain conditions by @fxamacker in #817

Full Changelog: v2.9.3...v2.9.4

v2.9.3

Choose a tag to compare

@fxamacker fxamacker released this 18 Aug 01:13
f0c90b6

This release fixes a potential panic when decoding into a time.Time from a CBOR byte string, map, or array under certain conditions.

Not affected: standard CBOR time data (RFC 8949 tag 0 or tag 1), and decoders configured with timeTag = DecTagRequired.

Upgrading to v2.9.3 is recommended.

The panic stack trace was publicly reported on 2026-08-17, and the fix was released the same day. Fuzz testing was extended to cover this class of bug, and fuzzing of v2.9.3 is ongoing.

What's Changed

  • Skip data item when decoding to time.Time fails under certain conditions by @fxamacker in #803

Full Changelog: v2.9.2...v2.9.3

v2.9.2

Choose a tag to compare

@fxamacker fxamacker released this 04 May 12:16
45589ab

This release refactors and hardens the streaming encoder by adding stricter checks for encoding CBOR indefinite-length data. Other changes include minor bugfixes, defensive checks, and more tests.

Projects that don't use CBOR indefinite-length data may also want to upgrade (summary of prior releases).

The stricter checks in the encoder prevent improper use of the library and bad inputs from producing malformed CBOR indefinite-length data that would be rejected by the decoder.

This release passed fuzz tests (billions of execs) and it is production quality.

What's Changed

  • Reject encoding indefinite-length map with odd item count by @fxamacker in #764
  • Reject encoding indefinite-length data item as a chunk inside indefinite-length byte string or text string by @fxamacker in #765
  • Make TagSet.Remove a no-op when contentType is nil by @fxamacker in #766
  • Refactor indefinite-length encoding and improve chunk validation during encoding by @fxamacker in #767
  • Add more tests, fix a nit in unreachable panic message, update docs & ci by @fxamacker in #768

CI / GitHub Actions and Docs

🔎 Details...

Full Changelog: v2.9.1...v2.9.2

v2.9.1

Choose a tag to compare

@fxamacker fxamacker released this 30 Mar 13:31
63d1c66

This release includes important bugfixes, defensive checks, improved code quality, and more tests. Although not public, the fuzzer was also improved by adding more fuzz tests.

🐞 Bug fixes related to the keyasint feature

These changes only affect Go struct fields tagged with keyasint:

  • [Decoding] Reject integer keys that exceed math.MaxInt64 when decoding CBOR map to a struct with keyasint field (PR #757)
  • [Decoding] Prevent string representation of an integer key from matching the struct field tagged by keyasint (PR #757)
  • [Encoding & Decoding] Deduplicate struct fields with the same normalized keyasint tag values (PR #757)

🐞 Other bug fixes and defensive checks

Some of the bugs fixed are related to decoding extreme values that cannot be encoded with this library. For example, the decoder checks if epoch time encoded as CBOR float value representing hundreds of billions of years overflows int64(seconds).

NOTE: It is generally good practice to avoid using floating point to store epoch time (even when not using CBOR).

  • [Decoding] Reject decoding epoch time encoded as floats that overflow int64 (PR #753)
  • [Encoding] Return a cloned slice for an empty RawMessage from RawMessage.MarshalCBOR (PR #753)
  • [Encoding] Reject encoding nil inside indefinite-length strings (PR #750)
  • [Diagnostic] Accept valid U+FFFD replacement character (PR #753)

What's Changed

  • 🆕 Add TimeMode encoding option TimeRFC3339NanoUTC by @fxamacker in #688
  • Use actual negative zero in tests by @makew0rld in #708
  • Reject encoding nil inside CBOR indefinite-length string by @fxamacker in #750
  • Refactor and add tests by @fxamacker in #752
  • Small bugfixes, defensive checks, and improvements by @fxamacker in #753
  • Refactor parseMapToStruct(), getDecodingStructType(), getEncodingStructType(), and field struct by @fxamacker in #754
  • Fix several issues related to keyasint tag option by @fxamacker in #757

CI / GitHub Actions and Docs

🔎 Details...

New Contributors

Full Changelog: v2.9.0...v2.9.1

v2.9.0

Choose a tag to compare

@fxamacker fxamacker released this 14 Jul 04:57
d29ad73

v2.9.0 adds new features, refactors tests, and improves docs. New features improve interoperability/transcoding between CBOR & JSON.

v2.9.0 passed fuzz tests and is production quality. However, the new TextUnmarshaler feature will continue being fuzz tested a bit longer due to recent changes. The recent changes are limited and don't affect other parts of the codec that passed ~2 billion execs fuzzing.

What's Changed

  • Refactor to use Go standard library functions by @fxamacker in #663
  • Improve DupMapKeyError message by @fxamacker in #670
  • Add options to support TextMarshaler and TextUnmarshaler by @benluddy in #672
  • Add optional support for json.Marshaler and json.Unmarshaler via transcoding by @benluddy in #673
  • Refactor tests and update comments by @fxamacker in #678
  • Use TextUnmarshaler on byte strings with ByteStringToStringAllowed. by @benluddy in #682

Docs

  • README: Document struct field tag "-" by @fxamacker in #653
  • Fix IntDecConvertSignedOrBigInt doc comment by @theory in #655
  • Update docs for TimeMode, Tag, RawTag, and add example for Embedded JSON Tag for CBOR by @fxamacker in #659
  • Update README for Embedded JSON Tag for CBOR (tag 262) by @fxamacker in #662
  • Fix typos in some comments by @adeinega in #671
  • Update README for v2.9.0 and add Red Hat as a user by @fxamacker in #684

CI

🔎 Details
  • Bump github/codeql-action from 3.28.13 to 3.28.15 by @dependabot[bot] in #651
  • Bump github/codeql-action from 3.28.15 to 3.28.16 by @dependabot[bot] in #658
  • Bump github/codeql-action from 3.28.16 to 3.28.17 by @dependabot[bot] in #660
  • Bump actions/setup-go from 5.4.0 to 5.5.0 by @dependabot[bot] in #661
  • Bump github/codeql-action from 3.28.17 to 3.28.18 by @dependabot[bot] in #664
  • Bump github/codeql-action from 3.28.18 to 3.28.19 by @dependabot[bot] in #667
  • Bump github/codeql-action from 3.28.19 to 3.29.0 by @dependabot[bot] in #674
  • Bump github/codeql-action from 3.29.0 to 3.29.2 by @dependabot[bot] in #680

Special Thanks

Many thanks to @benluddy for adding these new features! 🎉

  • Add opt-in support for encoding.TextMarshaler and encoding.TextUnmarshaler to encode and decode from CBOR text string.
  • Add opt-in support for json.Marshaler and json.Unmarshaler via user-provided transcoding function.

New Contributors

Full Changelog: v2.8.0...v2.9.0

v2.8.0

Choose a tag to compare

@fxamacker fxamacker released this 30 Mar 20:36
cbe7442

v2.8.0 adds omitzero struct tag option, fixes and deprecates 3 functions, and bumps requirements to go 1.20+.

Many thanks to @liggitt for contributing the omitzero support!

The "omitzero" option omits zero values from encoding, matching stdlib encoding/json behavior.
When specified in the cbor tag, the option is always honored.
When specified in the json tag, the option is honored when building with Go 1.24+.

This release fixes 3 functions (when called directly by user apps) to use same error handling on bad input as cbor.Unmarshal():

  • RawTag.UnmarshalCBOR() (thanks @thomas-fossati for reporting this!)
  • ByteString.UnmarshalCBOR()
  • SimpleValue.UnmarshalCBOR()

This release also deprecates those 3 functions because they were initially created for internal use. Please use Unmarshal() or UnmarshalFirst() instead.

To preserve backward compatibility, the deprecated functions were added to fuzz tests and will not be removed in v2.x.

What's Changed

  • go: update go.mod and ci.yml to require go1.20 or newer (was go1.17) by @fxamacker in #626
  • Replace interface{} with any by @fxamacker in #627
  • Replace reflect.Ptr with reflect.Pointer by @fxamacker in #628
  • Replace reflect.PtrTo with reflect.PointerTo by @fxamacker in #629
  • Add omitzero support by @liggitt in #644
  • Update error handling in RawTag.UnmarshalCBOR(), etc. to match cbor.Unmarshal() by @fxamacker in #645
  • Optimize internal calls to UnmarshalCBOR() for ByteString, RawTag, SimpleValue by @fxamacker in #647

Other Changes

🔍 Details

New Contributors

Full Changelog: v2.7.0...v2.8.0

v2.7.1

Choose a tag to compare

@fxamacker fxamacker released this 30 Mar 19:01
948c054

v2.7.1 fixes 3 functions (when called directly by user apps) to use same error handling on bad inputs as cbor.Unmarshal():

  • ByteString.UnmarshalCBOR()
  • RawTag.UnmarshalCBOR()
  • SimpleValue.UnmarshalCBOR()

The above 3 fixed functions are deprecated because they were initially created for internal use. Please use Unmarshal() or UnmarshalFirst() instead.

To preserve backward compatibility, the deprecated functions were added to fuzz tests and will not be removed in v2.x.

Before Upgrading to v2.7.1

v2.8.0 is being fuzz tested and will be released later today. It adds support for omitzero struct tag option.

v2.7.1 is the last version to support go 1.17-1.19. v2.8.0 and newer releases will require go 1.20+.

What's Changed

  • Update error handling in RawTag.UnmarshalCBOR(), etc. to match cbor.Unmarshal() by @fxamacker in #636
  • Optimize internal calls to UnmarshalCBOR() by @fxamacker in #648

Special Thanks

Thanks @thomas-fossati for reporting the bug in RawTag.UnmarshalCBOR() when it is called directly by user apps providing bad input data!

Full Changelog: v2.7.0...v2.7.1