vitessio/vitess · error

%s: %w

Error message

%s: %w

What it means

BufDecodeStringSQL decodes a single-quoted SQL string literal into a strings.Builder. If the input is shorter than 2 chars or does not start and end with a single quote, it returns the original value prefixed to ErrInvalidEncodedString via this '%s: %w' wrapper.

Source

Thrown at go/sqltypes/value.go:994

// We do need all characters here, since we do accept
// escaped double quotes in single quote strings and
// double quoted strings.
var decodeRef = map[byte]byte{
	'\x00': '0',
	'\'':   '\'',
	'"':    '"',
	'\b':   'b',
	'\n':   'n',
	'\r':   'r',
	'\t':   't',
	26:     'Z', // ctl-Z
	'\\':   '\\',
}

// BufDecodeStringSQL decodes the string into a strings.Builder
func BufDecodeStringSQL(buf *strings.Builder, val string) error {
	if len(val) < 2 || val[0] != '\'' || val[len(val)-1] != '\'' {
		return fmt.Errorf("%s: %w", val, ErrInvalidEncodedString)
	}
	in := hack.StringBytes(val[1 : len(val)-1])
	idx := 0
	for {
		if idx >= len(in) {
			return nil
		}
		ch := in[idx]
		if ch == '\'' {
			idx++
			if idx >= len(in) {
				return fmt.Errorf("%s: %w", val, ErrInvalidEncodedString)
			}
			if in[idx] != '\'' {
				return fmt.Errorf("%s: %w", val, ErrInvalidEncodedString)
			}
			buf.WriteByte(ch)
			idx++

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Ensure the input is a complete quoted literal like 'abc' before decoding
  2. If already decoded, skip DecodeStringSQL and use the value as-is
  3. Check upstream code for an unintended earlier unquote step

Example fix

// before
sqltypes.DecodeStringSQL("abc")
// after
sqltypes.DecodeStringSQL("'abc'")
Defensive patterns

Strategy: validation

Validate before calling

func isQuotedLiteral(s string) bool {
  return len(s) >= 2 && s[0] == '\'' && s[len(s)-1] == '\''
}

Try / catch

if !isQuotedLiteral(val) {
  // use val as-is, already decoded
} else if err := sqltypes.BufDecodeStringSQL(&sb, val); err != nil {
  return vterrors.Wrapf(err, "decode %q", val)
}

Prevention

When it happens

Trigger: Calling BufDecodeStringSQL (or DecodeStringSQL) with a string missing the surrounding quotes, an empty string, or an already-decoded value that no longer carries quotes.

Common situations: Passing values extracted from bind variables or configs that were already unquoted; double-decoding an unquoted literal; truncation that stripped the closing quote.

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/7d612ffdee5c714b. Report an issue: GitHub.