Skip to content

fix(cbor): replace invalid UTF-8 in text strings - #853

Open
YouKnowChatta wants to merge 1 commit into
rs:masterfrom
YouKnowChatta:fix/cbor-invalid-utf8
Open

YouKnowChatta wants to merge 1 commit into
rs:masterfrom
YouKnowChatta:fix/cbor-invalid-utf8

Conversation

@YouKnowChatta

Copy link
Copy Markdown

With -tags binary_log, log.Log().Str("value", "caf\u00e9\xff\xfe").Send() produces a record that cbor2 rejects with CBORDecodeError: error decoding text string.

AppendString wrote the original bytes under a CBOR text string header, which requires valid UTF-8. Replace each invalid byte with U+FFFD before calculating the length. The same record now decodes to {"value":"caf�"}, matching the value from the default JSON build.

TestAppendInvalidUTF8String fails before the fix and passes with it. The race suites pass in both build modes on Linux with Go 1.23.12, 1.24.13, and 1.27.4. go vet passes in both modes on Go 1.27.4.

The invalid UTF-8 row moves out of encodeStringTests, which expected the raw byte in the encoded output, into TestDecodeInvalidUTF8String, so the decoder's handling of malformed text keeps its test. The UTF-8 check adds time to every string write: with -tags binary_log on an i7-10750H, BenchmarkAppendString/NoEncoding goes from 7.4 to 26.0 ns/op and BenchmarkInfo from 14.4 to 23.5 ns/op, with no new allocations.

This branch has not been deployed

No deployments
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.

1 participant