golang/go · error

malformed prefix %q: escape sequence must contain two hex di

Error message

malformed prefix %q: escape sequence must contain two hex digits

What it means

`PrefixToPath` is the inverse of `PathToPrefix` — it decodes `%xx` escape sequences in symbol table names back to their original characters. This specific error fires when a `%` character is found too close to the end of the string: fewer than two characters remain after it, making a complete two-hex-digit escape sequence impossible.

Source

Thrown at src/cmd/internal/objabi/path.go:65

// PrefixToPath is the inverse of PathToPrefix, replacing escape sequences with
// the original character.
func PrefixToPath(s string) (string, error) {
	percent := strings.IndexByte(s, '%')
	if percent == -1 {
		return s, nil
	}

	p := make([]byte, 0, len(s))
	for i := 0; i < len(s); {
		if s[i] != '%' {
			p = append(p, s[i])
			i++
			continue
		}
		if i+2 >= len(s) {
			// Not enough characters remaining to be a valid escape
			// sequence.
			return "", fmt.Errorf("malformed prefix %q: escape sequence must contain two hex digits", s)
		}

		b, err := strconv.ParseUint(s[i+1:i+3], 16, 8)
		if err != nil {
			// Not a valid escape sequence.
			return "", fmt.Errorf("malformed prefix %q: escape sequence %q must contain two hex digits", s, s[i:i+3])
		}

		p = append(p, byte(b))
		i += 3
	}
	return string(p), nil
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Ensure the string passed to `PrefixToPath` was generated by `PathToPrefix` and has not been truncated.
  2. If reading from an archive, verify the archive is not corrupt or truncated.
  3. Use `PathToPrefix` consistently for all encoding — do not hand-build `%xx` sequences.

Example fix

// before — manual truncation of encoded name
prefix := PathToPrefix("some/path")
truncated := prefix[:len(prefix)-2]  // may cut a %xx sequence
path, err := PrefixToPath(truncated)  // fails: trailing % with no digits

// after — preserve full encoded string
path, err := PrefixToPath(prefix)  // works
Defensive patterns

Strategy: validation

Validate before calling

// Validate encoded prefix has complete escape sequences before decoding
func validateEncodedPrefix(s string) error {
    for i := 0; i < len(s); i++ {
        if s[i] == '%' {
            if i+2 >= len(s) {
                return fmt.Errorf("incomplete escape at position %d in %q", i, s)
            }
        }
    }
    return nil
}

Try / catch

// Decode safely with error handling
func safePrefixToPath(s string) string {
    path, err := objabi.PrefixToPath(s)
    if err != nil {
        return s // fallback to raw string
    }
    return path
}

Prevention

When it happens

Trigger: Calling `objabi.PrefixToPath(s)` where `s` contains a `%` in the last one or two positions of the string (e.g., `"foo%"` or `"foo%3"`). This happens when symbol table names are truncated, corrupted, or manually constructed without valid escaping.

Common situations: Corrupt Go object files or archives where symbol names are truncated. Manually splitting or substring-slicing encoded symbol names. A bug in a tool that generates symbol names without using `PathToPrefix`. Reading a truncated or partially-written archive.

Understand the failure class

Related errors


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