golang/go · error

invalid NUL in %s=... value

Error message

invalid NUL in %s=... value

What it means

Returned by checkEnvWrite when the value written via `go env -w` contains a NUL byte (\x00). NUL is illegal in environment values on most platforms and would silently truncate the value or break file parsing.

Source

Thrown at src/cmd/go/internal/envcmd/env.go:697

			break
		}
		args, err := quoted.Split(val)
		if err != nil {
			return fmt.Errorf("invalid %s: %v", key, err)
		}
		if len(args) == 0 {
			return fmt.Errorf("%s entry cannot contain only space", key)
		}
		if !filepath.IsAbs(args[0]) && args[0] != filepath.Base(args[0]) {
			return fmt.Errorf("%s entry is relative; must be absolute path: %q", key, args[0])
		}
	}

	if !utf8.ValidString(val) {
		return fmt.Errorf("invalid UTF-8 in %s=... value", key)
	}
	if strings.Contains(val, "\x00") {
		return fmt.Errorf("invalid NUL in %s=... value", key)
	}
	if strings.ContainsAny(val, "\v\r\n") {
		return fmt.Errorf("invalid newline in %s=... value", key)
	}
	return nil
}

func readEnvFileLines(mustExist bool) []string {
	file, _, err := cfg.EnvFile()
	if file == "" {
		if mustExist {
			base.Fatalf("go: cannot find go env config: %v", err)
		}
		return nil
	}
	data, err := os.ReadFile(file)
	if err != nil && (!os.IsNotExist(err) || mustExist) {
		base.Fatalf("go: reading go env config: %v", err)

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Strip NUL bytes from the value before invoking `go env -w`.
  2. Validate the input source — values should be human-readable text.
  3. Reject binary input at the trust boundary of your config tooling.

Example fix

// before
v := string(binaryBuf) // may contain \x00
goEnvW(key, v)
// after — strip and validate
v = strings.ReplaceAll(v, "\x00", "")
if strings.ContainsRune(v, 0) { return fmt.Errorf("nul") }
goEnvW(key, v)
Defensive patterns

Strategy: validation

Validate before calling

import "strings"

if strings.ContainsRune(val, '\x00') {
    return fmt.Errorf("value for %s contains NUL", key)
}

Prevention

When it happens

Trigger: Any `go env -w KEY=...` value containing a literal NUL byte, typically from binary input, embedded C strings, or a corrupt env value.

Common situations: Scripts forwarding bytes from binary blobs; values constructed by string concatenation over fixed-size buffers; malicious/accidental control characters in templated config.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/ce18b62707024d04. Report an issue: GitHub.