go-delve/delve · error

string too long for comparison

Error message

string too long for comparison

What it means

When comparing two strings with ==, !=, <, etc., Delve must load the full string contents from the debugged process into constant.Value form. To bound memory usage it truncates very long strings during load; if either operand of a string comparison was truncated (loaded length differs from the string's true Len), Delve refuses to give a potentially wrong comparison result and throws this error.

Source

Thrown at pkg/proc/eval.go:2629

	case reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
		return constantCompare(op, xv.Value, yv.Value)
	case reflect.String:
		if xv.Len != yv.Len {
			switch op {
			case token.EQL:
				return false, nil
			case token.NEQ:
				return true, nil
			}
		}
		if xv.Kind == reflect.String {
			xv.loadValue(loadFullValueLongerStrings)
		}
		if yv.Kind == reflect.String {
			yv.loadValue(loadFullValueLongerStrings)
		}
		if int64(len(constant.StringVal(xv.Value))) != xv.Len || int64(len(constant.StringVal(yv.Value))) != yv.Len {
			return false, errors.New("string too long for comparison")
		}
		return constantCompare(op, xv.Value, yv.Value)
	}

	if op != token.EQL && op != token.NEQ {
		return false, fmt.Errorf("operator %s not defined on %s", op.String(), xv.Kind.String())
	}

	var eql bool
	var err error

	if xv == nilVariable {
		switch op {
		case token.EQL:
			return yv.isNil(), nil
		case token.NEQ:
			return !yv.isNil(), nil
		}

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Increase the string limit before comparing: in the CLI use config max-string-len <n> (e.g. 'config max-string-len 4096'), or set cfg.MaxStringLen in API/LoadConfig usage
  2. Compare lengths/shorter substrings instead: 'print len(s1) == len(s2)'
  3. Compare string pointers/addresses if identity, not content, matters
  4. For RPC clients, pass a larger MaxStringLen in the LoadConfig used for EvalExpression

Example fix

// before (dlv CLI)
(dlv) print s == expected   // strings > default limit -> error

// after
(dlv) config max-string-len 8192
(dlv) print s == expected
Defensive patterns

Strategy: validation

Validate before calling

// before comparing strings in dlv
(dlv) print len(s1)
(dlv) print len(s2)
// if either length exceeds max-string-len, raise it first:
(dlv) config max-string-len 8192

Type guard

func comparableStrings(s1, s2 string, maxLen int) bool {
    return len(s1) <= maxLen && len(s2) <= maxLen
}

Try / catch

// RPC client pattern
val, err := client.EvalVariable(scope, "s1 == s2", cfg)
if err != nil && strings.Contains(err.Error(), "string too long for comparison") {
    cfg.MaxStringLen *= 4
    val, err = client.EvalVariable(scope, "s1 == s2", cfg)
}

Prevention

When it happens

Trigger: Evaluating a comparison of two string variables (e.g. 'print s1 == s2', 'print s == "literal"') where at least one string is longer than the configured MaxStringLen and got truncated during loadValue.

Common situations: Comparing large payloads, log lines, or buffers inside the debugger with default config; hitting the error because MaxStringLen (default 64 in some paths) is smaller than the strings being compared.

Related errors


AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31). Data as JSON: /api/errors/9075d6fd1fdbe35b. Report an issue: GitHub.