golang/go · error

import path contains NUL

Error message

import path contains NUL

What it means

Thrown by checkImportPath when the import path contains a NUL byte (\x00). NUL is rejected early because it would corrupt C-string handling and filesystem APIs; it is an immediate, unconditional failure regardless of allowSpace.

Source

Thrown at src/cmd/compile/internal/noder/import.go:308

	pos := len(data) - len(fingerprint)
	if pos < 0 {
		return fmt.Errorf("missing linker fingerprint in exportdata, but found %q", data)
	}
	buf := []byte(data[pos:])

	copy(fingerprint[:], buf)
	base.Ctxt.AddImport(path, fingerprint)

	return nil
}

func checkImportPath(path string, allowSpace bool) error {
	if path == "" {
		return errors.New("import path is empty")
	}

	if strings.Contains(path, "\x00") {
		return errors.New("import path contains NUL")
	}

	for ri := range base.ReservedImports {
		if path == ri {
			return fmt.Errorf("import path %q is reserved and cannot be used", path)
		}
	}

	for _, r := range path {
		switch {
		case r == utf8.RuneError:
			return fmt.Errorf("import path contains invalid UTF-8 sequence: %q", path)
		case r < 0x20 || r == 0x7f:
			return fmt.Errorf("import path contains control character: %q", path)
		case r == '\\':
			return fmt.Errorf("import path contains backslash; use slash: %q", path)
		case !allowSpace && unicode.IsSpace(r):
			return fmt.Errorf("import path contains space character: %q", path)

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Sanitize/trim NUL bytes from any external string before it becomes an import path.
  2. Regenerate the offending source file if it is corrupted.
  3. Audit string-handling code (C-string interop, fixed buffers) that may introduce NULs.

Example fix

// before
path := "pkg\x00evil"
// after
path := strings.Trim(path, "\x00")
// or reject entirely:
if strings.Contains(path, "\x00") { return errors.New("bad path") }
Defensive patterns

Strategy: validation

Validate before calling

// Reject import paths containing NUL or other control bytes.
import ("strings")
func cleanImportPath(p string) bool { return !strings.Contains(p, "\x00") }

Prevention

When it happens

Trigger: An import path string containing '\x00' is passed to checkImportPath. Typically the result of a corrupted file, a truncation bug, or unsafe string slicing that leaves an embedded NUL.

Common situations: Corrupted source files or archives; buggy code generation that concatenates without bounds checks; tooling that reads fixed-width records and leaves NUL padding; security-sensitive input that must never contain NUL.

Related errors


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