XTLS/Xray-core · error

target length leaves an invalid record length

Error message

target length %d leaves an invalid record length %d

What it means

writePaddingTurnWithBuffer invariant check: the selected variant's target length minus prefixLength left less than one byte for the record body. The chosen variant is shorter than (or equal to) the prefix already sent, so the padding record would be empty or negative.

Solutions

  1. Choose variants whose total length exceeds prefixLength by at least 1
  2. Increase the variant/turn target length relative to the prefix
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at transport/internet/finalmask/xmc/padding.go:154 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of XTLS/Xray-core@7d214f8b09 (2026-08-15). Data as JSON: /api/errors/5274023b0342d9b8. Report an issue: GitHub.

Appendix: source

Thrown at transport/internet/finalmask/xmc/padding.go:154

	return writePaddingTurnWithBuffer(w, turn, prefixLength, sleep, nil)
}

func writePaddingTurnWithBuffer(w io.Writer, turn paddingTurn, prefixLength int, sleep func(time.Duration), reusableBuffer *[]byte) error {
	startDelay, err := randomPaddingDelay(turn.startDelay)
	if err != nil {
		return fmt.Errorf("select padding start delay: %w", err)
	}
	if startDelay > 0 {
		sleep(startDelay)
	}

	targetLength, chunks, delays, err := selectPaddingVariant(turn, prefixLength)
	if err != nil {
		return err
	}
	recordLength := targetLength - prefixLength
	if recordLength < 1 {
		return fmt.Errorf("target length %d leaves an invalid record length %d", targetLength, recordLength)
	}

	encodedLength := Varint(recordLength)
	var header bytes.Buffer
	if err = encodedLength.writeTo(&header); err != nil {
		return fmt.Errorf("write padding header: %w", err)
	}
	if len(chunks) == 0 {
		writeChunkLength := turn.writeChunkLength
		if writeChunkLength == 0 {
			writeChunkLength = paddingBufferLength
		} else if turn.writeChunkMinLength > 0 {
			writeChunkLength, err = randomPaddingTarget(turn.writeChunkMinLength, writeChunkLength)
			if err != nil {
				return fmt.Errorf("select padding write chunk length: %w", err)
			}
		}
		chunks = defaultPaddingChunks(recordLength, writeChunkLength)

View on GitHub (pinned to 7d214f8b09)