XTLS/Xray-core · error

invalid body length:

Error message

invalid body length: 

What it means

Returned by find() when an entry's body length, decoded from its varint, converts to a non-positive int (bodyL <= 0). A zero length means the entry header matched but declares an empty body (invalid for GeoIP/GeoSite entries); a negative value means the varint exceeded maxint64 (e.g. 2^63), which cannot be a real body size. Both indicate a corrupted or misaligned dat stream.

Source

Thrown at common/geodata/geodat_loader.go:105

		return nil, errors.New("empty code")
	}

	br := bufio.NewReaderSize(r, 64*1024)
	need := 2 + codeL // TODO: if code too long
	prefixBuf := make([]byte, need)

	for {
		if _, err := br.ReadByte(); err != nil {
			return nil, err
		}

		x, err := decodeVarint(br)
		if err != nil {
			return nil, err
		}
		bodyL := int(x)
		if bodyL <= 0 {
			return nil, errors.New("invalid body length: ", bodyL)
		}

		prefixL := bodyL
		if prefixL > need {
			prefixL = need
		}
		prefix := prefixBuf[:prefixL]
		if _, err := io.ReadFull(br, prefix); err != nil {
			return nil, err
		}

		match := false
		if bodyL >= need {
			if int(prefix[1]) == codeL && bytes.Equal(prefix[2:need], code) {
				if !readBody {
					return nil, nil
				}
				match = true

View on GitHub (pinned to 7d214f8b09)

Solutions

  1. Re-download the dat file from official releases and verify size/checksum before deploying.
  2. Deploy dat updates atomically (temp file + rename) so readers never see partially written files.
  3. Keep the xray asset directory free of unrelated files and confirm filenames.

Example fix

# before
$ sha256sum /etc/xray/geosite.dat # differs from published hash

# after
$ curl -sL -o /tmp/dlc.dat https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat
$ sha256sum /tmp/dlc.dat # verify against release notes, then:
$ mv /tmp/dlc.dat /etc/xray/geosite.dat
Defensive patterns

Strategy: retry

Validate before calling

// Pre-verify integrity via checksum; also sanity-check file size against the published release size

Try / catch

// On "invalid body length", treat the dat as corrupt: re-download to temp, verify hash, rename, retry load

Prevention

When it happens

Trigger: Scanning a dat file whose framing bytes are corrupt so that a length varint decodes to 0 or to a huge value that overflows int — bad downloads, files damaged mid-transfer, or a non-geodata file fed to the loader.

Common situations: Truncated dat downloads where scanning continues past valid data; dat files corrupted by sync tools; code strings that accidentally match inside body bytes of a misaligned stream (long/short code handling is bounded by 'need').

Related errors


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