{"record":{"id":"e093d752a24749ea","repo":"pion/webrtc","slug":"w-s-is-not-valid-utf-8","errorCode":null,"errorMessage":"%w: %s is not valid UTF-8","messagePattern":"%w: (.+?) is not valid UTF-8","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/media/oggwriter/oggwriter.go","lineNumber":822,"sourceCode":"}\n\nfunc validateUserComment(comment UserComment) error {\n\tif !isValidCommentName(comment.Comment) {\n\t\treturn fmt.Errorf(\"%w: invalid user comment name\", errInvalidOpusTags)\n\t}\n\tif err := validateOpusTagString(\"user comment value\", comment.Value); err != nil {\n\t\treturn err\n\t}\n\tif uint64(len(comment.Comment))+1+uint64(len(comment.Value)) > maxUint32Length {\n\t\treturn fmt.Errorf(\"%w: user comment is too long\", errInvalidOpusTags)\n\t}\n\n\treturn nil\n}\n\nfunc validateOpusTagString(field, value string) error {\n\tif !utf8.ValidString(value) {\n\t\treturn fmt.Errorf(\"%w: %s is not valid UTF-8\", errInvalidOpusTags, field)\n\t}\n\tif uint64(len(value)) > maxUint32Length {\n\t\treturn fmt.Errorf(\"%w: %s is too long\", errInvalidOpusTags, field)\n\t}\n\n\treturn nil\n}\n\nfunc isValidCommentName(comment string) bool {\n\tif comment == \"\" {\n\t\treturn false\n\t}\n\tfor i := 0; i < len(comment); i++ {\n\t\tb := comment[i]\n\t\tif b < 0x20 || b > 0x7d || b == '=' {\n\t\t\treturn false\n\t\t}\n\t}","sourceCodeStart":804,"sourceCodeEnd":840,"githubUrl":"https://github.com/pion/webrtc/blob/8c25dc09fa9e7c09aac4309ead88093d760432b0/pkg/media/oggwriter/oggwriter.go#L804-L840","documentation":"validateOpusTagString rejects any string field (vendor or user comment value) that is not valid UTF-8, returning this error wrapped by errInvalidOpusTags. The OpusTags / Vorbis comment spec mandates UTF-8 encoded strings, so non-UTF-8 input (e.g. Latin-1 or raw binary bytes) cannot be written into the Ogg comment header. The %s is the field name, such as \"vendor\" or \"user comment value\".","triggerScenarios":"Passing an OpusTags.Vendor string or a UserComment.Value containing invalid UTF-8 bytes to code that validates OpusTags (validateOpusTagsWithMaxHeaderLen -> validateOpusTagString). In Go, strings can hold arbitrary bytes, e.g. strings built from []byte read as Latin-1, GBK, or raw binary.","commonSituations":"Reading metadata from a legacy tag format (ID3v1, Latin-1 encoded ID3v2 frames) and passing it through unconverted; decoding filenames or user input from a non-UTF-8 locale; copying raw bytes from a corrupt file; Go code assuming string literals from external sources are valid UTF-8.","solutions":["Convert the input to valid UTF-8 before constructing OpusTags, e.g. using golang.org/x/text/encoding (charmap.ISO8859_1 / simplifiedchined GBK) decoders or strings.ToValidUTF8.","Sanitize with utf8.RuneErrorInString / strings.ToValidUTF8(s, string(utf8.RuneError)) if lossy replacement is acceptable.","Find the offending source by checking utf8.ValidString on vendor and each UserComment.Value before calling the library, and log/repair the invalid one.","Match the error with errors.Is(err, errInvalidOpusTags); the message names the field so you know whether it was the vendor string or a comment value."],"exampleFix":"// before\ntags.Vendor = string(latin1Bytes)\n// after\nimport \"golang.org/x/text/encoding/charmap\"\ndec := charmap.ISO8859_1.NewDecoder()\nout, err := dec.Bytes(latin1Bytes)\nif err != nil { return err }\ntags.Vendor = string(out) // now valid UTF-8","handlingStrategy":"validation","validationCode":"func ensureUTF8(field, s string) (string, error) {\n\tif !utf8.ValidString(s) {\n\t\treturn \"\", fmt.Errorf(\"%s is not valid UTF-8\", field)\n\t}\n\treturn s, nil\n}","typeGuard":"func isValidUTF8(s string) bool { return utf8.ValidString(s) }","tryCatchPattern":"if err := writer.WriteOpusTags(tags); err != nil {\n\tif errors.Is(err, oggwriter.ErrInvalidOpusTags) && strings.Contains(err.Error(), \"not valid UTF-8\") {\n\t\t// re-encode vendor/comments via strings.ToValidUTF8 or a charset decoder, then retry\n\t}\n\treturn err\n}","preventionTips":["Transcode non-UTF-8 metadata (Latin-1 ID3 tags, GBK text) with golang.org/x/text/encoding before use.","Run utf8.ValidString on vendor and every UserComment.Value as an input gate.","Use strings.ToValidUTF8 as a last-resort sanitizer when exact fidelity is not required.","Avoid passing raw bytes from file reads or network payloads directly as metadata strings."],"tags":["go","ogg","opus","utf-8","encoding","validation"],"backgroundTag":"invalid-utf8-string","analyzedSha":"8c25dc09fa9e7c09aac4309ead88093d760432b0","analyzedAt":"2026-09-03T21:56:56.182Z","contentChangedAt":"2026-09-03T21:56:56.182Z","schemaVersion":2},"datasetVersion":"2026-09-11T07:07:21.782Z"}