{"record":{"id":"1f33c4bb6158d2a2","repo":"golang/go","slug":"invalid-utf-8-in-s-value","errorCode":null,"errorMessage":"invalid UTF-8 in %s=... value","messagePattern":"invalid UTF-8 in (.+?)=\\.\\.\\. value","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/envcmd/env.go","lineNumber":694,"sourceCode":"\t\t}\n\tcase \"CC\", \"CXX\":\n\t\tif val == \"\" {\n\t\t\tbreak\n\t\t}\n\t\targs, err := quoted.Split(val)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"invalid %s: %v\", key, err)\n\t\t}\n\t\tif len(args) == 0 {\n\t\t\treturn fmt.Errorf(\"%s entry cannot contain only space\", key)\n\t\t}\n\t\tif !filepath.IsAbs(args[0]) && args[0] != filepath.Base(args[0]) {\n\t\t\treturn fmt.Errorf(\"%s entry is relative; must be absolute path: %q\", key, args[0])\n\t\t}\n\t}\n\n\tif !utf8.ValidString(val) {\n\t\treturn fmt.Errorf(\"invalid UTF-8 in %s=... value\", key)\n\t}\n\tif strings.Contains(val, \"\\x00\") {\n\t\treturn fmt.Errorf(\"invalid NUL in %s=... value\", key)\n\t}\n\tif strings.ContainsAny(val, \"\\v\\r\\n\") {\n\t\treturn fmt.Errorf(\"invalid newline in %s=... value\", key)\n\t}\n\treturn nil\n}\n\nfunc readEnvFileLines(mustExist bool) []string {\n\tfile, _, err := cfg.EnvFile()\n\tif file == \"\" {\n\t\tif mustExist {\n\t\t\tbase.Fatalf(\"go: cannot find go env config: %v\", err)\n\t\t}\n\t\treturn nil\n\t}","sourceCodeStart":676,"sourceCodeEnd":712,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/envcmd/env.go#L676-L712","documentation":"Returned by checkEnvWrite as a final guard for any value written via `go env -w`: the value must be valid UTF-8. The go env config file is line-oriented text, so non-UTF-8 bytes would corrupt parsing on later reads.","triggerScenarios":"Any `go env -w KEY=...` whose value byte sequence is not valid UTF-8 — e.g. raw latin-1/ISO-8859 bytes, binary garbage, or a value built from a non-UTF-8 source.","commonSituations":"Pasting paths from Windows terminals using legacy code pages; binary data accidentally fed via $env interpolation; reading values from files in non-UTF-8 encodings.","solutions":["Re-encode the value as UTF-8 before passing it to `go env -w`.","Sanitize input via utf8.ValidString before invoking the command.","Configure your terminal/editor/shell to use UTF-8."],"exampleFix":"// before — value loaded from a latin-1 file\nv := readLatin1(path) // contains 0xE9 etc.\ngoEnvW(key, v) // -> invalid UTF-8 in KEY=... value\n// after — transcode to UTF-8 first\nenc := charmap.ISO8859_1.NewDecoder()\nv, _ = enc.String(v)\nif !utf8.ValidString(v) { return fmt.Errorf(\"non-utf8\") }\ngoEnvW(key, v)","handlingStrategy":"validation","validationCode":"import \"unicode/utf8\"\n\nif !utf8.ValidString(val) {\n    return fmt.Errorf(\"value for %s is not valid UTF-8\", key)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate utf8.ValidString for every external string before `go env -w`.","Transcode legacy-encoded input to UTF-8 at the trust boundary.","Configure terminals/editors to UTF-8."],"tags":["config","env","validation","encoding","utf8"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}