jackc/pgx · error

invalid length for lseg

Error message

invalid length for lseg: %v

What it means

The textual lseg format '[(x1,y1),(x2,y2)]' has a minimum length of 11 bytes. This guard fires before parsing when the source is shorter, meaning it cannot possibly contain two coordinate pairs with the required delimiters.

Solutions

  1. Verify the stored value matches the '[(x1,y1),(x2,y2)]' text format
  2. Fix any custom code that wrote a truncated string
  3. Use the binary format to avoid text parsing
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/lseg.go:183 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /api/errors/477cbccbdb4af5b9. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/lseg.go:183

		P: [2]Vec2{
			{math.Float64frombits(x1), math.Float64frombits(y1)},
			{math.Float64frombits(x2), math.Float64frombits(y2)},
		},
		Valid: true,
	})
}

type scanPlanTextAnyToLsegScanner struct{}

func (scanPlanTextAnyToLsegScanner) Scan(src []byte, dst any) error {
	scanner := (dst).(LsegScanner)

	if src == nil {
		return scanner.ScanLseg(Lseg{})
	}

	if len(src) < 11 {
		return fmt.Errorf("invalid length for lseg: %v", len(src))
	}

	// Expected format: [(x1,y1),(x2,y2)]
	sp1, sp2, found := strings.Cut(string(src[2:len(src)-2]), "),(")
	if !found {
		return fmt.Errorf("invalid format for lseg")
	}

	sx1, sy1, found := strings.Cut(sp1, ",")
	if !found {
		return fmt.Errorf("invalid format for lseg")
	}
	sx2, sy2, found := strings.Cut(sp2, ",")
	if !found {
		return fmt.Errorf("invalid format for lseg")
	}

	x1, err := strconv.ParseFloat(sx1, 64)

View on GitHub (pinned to ec1a0befd2)