cloudflare/cloudflared · error

ErrDatagramResponseMsgTooLargeDatagram

ErrDatagramResponseMsgTooLargeDatagram

Error message

datagram response error message length exceeds the length of the provided datagram

What it means

ErrDatagramResponseMsgTooLargeDatagram is returned when unmarshaling a UDPSessionRegistrationResponseDatagram whose declared error-message length (read at bytes 18:20) exceeds the bytes actually remaining after the fixed 20-byte header (data[20:]). Unlike the maximum-size variant, the length is protocol-legal but the datagram is truncated: the message body does not fit in the provided buffer. UnmarshalBinary wraps it via wrapUnmarshalErr; match with errors.Is.

Source

Thrown at quic/v3/datagram_errors.go:17

package v3

import (
	"errors"
	"fmt"
)

var (
	ErrInvalidDatagramType                 error = errors.New("invalid datagram type expected")
	ErrDatagramHeaderTooSmall              error = fmt.Errorf("datagram should have at least %d byte", datagramTypeLen)
	ErrDatagramPayloadTooLarge             error = errors.New("payload length is too large to be bundled in datagram")
	ErrDatagramPayloadHeaderTooSmall       error = errors.New("payload length is too small to fit the datagram header")
	ErrDatagramPayloadInvalidSize          error = errors.New("datagram provided is an invalid size")
	ErrDatagramResponseMsgInvalidSize      error = errors.New("datagram response message is an invalid size")
	ErrDatagramResponseInvalidSize         error = errors.New("datagram response is an invalid size")
	ErrDatagramResponseMsgTooLargeMaximum  error = fmt.Errorf("datagram response error message length exceeds the length of the datagram maximum: %d", maxResponseErrorMessageLen)
	ErrDatagramResponseMsgTooLargeDatagram error = fmt.Errorf("datagram response error message length exceeds the length of the provided datagram")
	ErrDatagramICMPPayloadTooLarge         error = fmt.Errorf("datagram icmp payload exceeds %d bytes", maxICMPPayloadLen)
	ErrDatagramICMPPayloadMissing          error = errors.New("datagram icmp payload is missing")
)

func wrapMarshalErr(err error) error {
	return fmt.Errorf("datagram marshal error: %w", err)
}

func wrapUnmarshalErr(err error) error {
	return fmt.Errorf("datagram unmarshal error: %w", err)
}

View on GitHub (pinned to 2253eeeb25)

Solutions

  1. Before unmarshaling, check len(data) >= 20 + declared message length and reject truncated buffers.
  2. Use errors.Is(err, v3.ErrDatagramResponseMsgTooLargeDatagram) to detect truncated datagrams and request retransmission or drop.
  3. Audit the read path that produced the buffer for off-by-one slicing that dropped trailing bytes.
  4. Check for network equipment (MTU, UDP fragmentation) truncating datagrams.

Example fix

// before
err := resp.UnmarshalBinary(buf)
// after
if len(buf) >= 20 {
    msgLen := int(binary.BigEndian.Uint16(buf[18:20]))
    if len(buf[20:]) < msgLen {
        return fmt.Errorf("truncated response datagram: need %d, have %d", msgLen, len(buf)-20)
    }
}
err := resp.UnmarshalBinary(buf)
Defensive patterns

Strategy: validation

Validate before calling

func isCompleteResponseDatagram(data []byte) bool {
    if len(data) < 20 {
        return false
    }
    msgLen := int(binary.BigEndian.Uint16(data[18:20]))
    return len(data[20:]) >= msgLen
}

Try / catch

if errors.Is(err, v3.ErrDatagramResponseMsgTooLargeDatagram) {
    logger.Warn().Msg("truncated response datagram; discarding")
    return nil
}

Prevention

When it happens

Trigger: Calling UnmarshalBinary on a UDPSessionRegistrationResponseDatagram where int(errMsgLen) > len(data[20:]) — the datagram was cut short after the header, or the length field was corrupted upward.

Common situations: MTU/truncation issues chopping datagrams in transit; buffer-slicing bugs upstream passing a partial payload; version mismatch where the peer writes a longer message format than expected; test fixtures with deliberately truncated payloads.

Related errors


AI-assisted analysis of cloudflare/cloudflared@2253eeeb25 (2026-09-06). Data as JSON: /api/errors/14e52dde5adca4ad. Report an issue: GitHub.