{"record":{"id":"f7a070ebadd3e740","repo":"cloudflare/cloudflared","slug":"payload-length-is-too-small-to-fit-the-datagram-he","errorCode":null,"errorMessage":"payload length is too small to fit the datagram header","messagePattern":"payload length is too small to fit the datagram header","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"quic/v3/datagram_errors.go","lineNumber":12,"sourceCode":"package v3\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n)\n\nvar (\n\tErrInvalidDatagramType                 error = errors.New(\"invalid datagram type expected\")\n\tErrDatagramHeaderTooSmall              error = fmt.Errorf(\"datagram should have at least %d byte\", datagramTypeLen)\n\tErrDatagramPayloadTooLarge             error = errors.New(\"payload length is too large to be bundled in datagram\")\n\tErrDatagramPayloadHeaderTooSmall       error = errors.New(\"payload length is too small to fit the datagram header\")\n\tErrDatagramPayloadInvalidSize          error = errors.New(\"datagram provided is an invalid size\")\n\tErrDatagramResponseMsgInvalidSize      error = errors.New(\"datagram response message is an invalid size\")\n\tErrDatagramResponseInvalidSize         error = errors.New(\"datagram response is an invalid size\")\n\tErrDatagramResponseMsgTooLargeMaximum  error = fmt.Errorf(\"datagram response error message length exceeds the length of the datagram maximum: %d\", maxResponseErrorMessageLen)\n\tErrDatagramResponseMsgTooLargeDatagram error = fmt.Errorf(\"datagram response error message length exceeds the length of the provided datagram\")\n\tErrDatagramICMPPayloadTooLarge         error = fmt.Errorf(\"datagram icmp payload exceeds %d bytes\", maxICMPPayloadLen)\n\tErrDatagramICMPPayloadMissing          error = errors.New(\"datagram icmp payload is missing\")\n)\n\nfunc wrapMarshalErr(err error) error {\n\treturn fmt.Errorf(\"datagram marshal error: %w\", err)\n}\n\nfunc wrapUnmarshalErr(err error) error {\n\treturn fmt.Errorf(\"datagram unmarshal error: %w\", err)\n}\n","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/quic/v3/datagram_errors.go#L1-L29","documentation":"ErrDatagramPayloadHeaderTooSmall is returned by MarshalPayloadHeaderTo when the destination buffer is smaller than DatagramPayloadHeaderLen. The function writes the datagram type byte, request ID and payload length fields into the provided slice, so a buffer that cannot hold the header would be overrun. It is a caller-supplied buffer sizing error, wrapped as a marshal error.","triggerScenarios":"Calling v3.MarshalPayloadHeaderTo(requestID, payload) with len(payload) < DatagramPayloadHeaderLen. In tests this is reproduced with a 16-byte buffer when the required header is longer.","commonSituations":"Preallocating a buffer from the payload size without accounting for the header bytes; reusing a small scratch buffer; manually building datagram frames instead of letting MarshalBinary allocate the correctly sized buffer.","solutions":["Use MarshalBinary() on the datagram struct instead of manually sizing the buffer — it allocates the right size automatically","Allocate payload buffer as len(udpPayload) + DatagramPayloadHeaderLen before calling MarshalPayloadHeaderTo","Add a length assertion or helper that reserves header space before writing payloads"],"exampleFix":"// before\npayload := makePayload(16) // too small for header\nerr := v3.MarshalPayloadHeaderTo(testRequestID, payload)\n// after\npayload := make([]byte, DatagramPayloadHeaderLen+len(udpPayload))\nerr := v3.MarshalPayloadHeaderTo(testRequestID, payload)","handlingStrategy":"validation","validationCode":"if len(buf) < DatagramPayloadHeaderLen {\n    buf = make([]byte, DatagramPayloadHeaderLen+len(udpPayload))\n}","typeGuard":null,"tryCatchPattern":"if err := v3.MarshalPayloadHeaderTo(id, buf); errors.Is(err, v3.ErrDatagramPayloadHeaderTooSmall) {\n    return fmt.Errorf(\"buffer too small for datagram header: need %d bytes\", DatagramPayloadHeaderLen)\n}","preventionTips":["Prefer MarshalBinary over manual buffer construction","Always add DatagramPayloadHeaderLen when sizing payload buffers","Centralize buffer allocation in one helper"],"tags":["quic","datagram","buffer-size"],"backgroundTag":"insufficient-buffer-size","analyzedSha":"2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f","analyzedAt":"2026-09-06T04:14:33.757Z","contentChangedAt":"2026-09-06T04:14:33.757Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}