Skip to content

[networking] fix reading custom payloads of 0 or 32767 bytes in 11w48a- - #120

Closed
WalkerRodgersArchive wants to merge 1 commit into
OrnitheMC:gen2from
WalkerRodgersArchive:networking-fix-custom-payload-read
Closed

WalkerRodgersArchive wants to merge 1 commit into
OrnitheMC:gen2from
WalkerRodgersArchive:networking-fix-custom-payload-read

Conversation

@WalkerRodgersArchive

Copy link
Copy Markdown

In 11w48a and older, Networking uses its own CustomPayloadPacket (in the mca0.1.0-mca0.2.1, mca0.2.2-mca0.2.8, mca1.0.16-mca1.2.6 and mcb1.0-mc13w39b modules). Its read only reads the payload when 0 < size < Short.MAX_VALUE, while the constructor and write accept payloads of up to and including 32767 bytes. This causes two problems:

  • 32767-byte payload: write sends the size and all 32767 bytes, but read skips them. The bytes stay in the stream and are parsed as the next packets, so the connection desyncs.
  • 0-byte payload: read leaves data as null. getSize() then throws a NullPointerException, and listeners get null instead of an empty array.

Fix

read now throws an IOException for a negative size and otherwise always reads size bytes (an empty array for 0). The size comes from readShort, so it can't be larger than Short.MAX_VALUE and needs no upper-bound check. The same change is applied to all four copies. 11w49a and newer use the vanilla packet and are not changed.

Testing

  • ./gradlew :libraries:networking:build and build for all six networking version modules, with JDK 21: passes.

  • Round-trip check on the built mca0.1.0-mca0.2.1 jar against the intermediary a0.2.1 server jar: write a payload, append a 4-byte marker, read the packet back, then read the marker. Same check on the published 0.12.1 jar for comparison:

    payload size 0.12.1 this PR
    0 data null, getSize() NPE OK (empty array)
    1, 32766 OK OK
    32767 payload not read: marker read from payload bytes, 32767 bytes left in stream, getSize() NPE OK
    -1 (hand-written) no error, data null IOException

    The check was a throwaway harness and is not part of this PR (the repo has no test setup).

AI disclosure

The code change and this description were written by an AI coding assistant (Claude Code, using Claude Opus 5.5). A human reviewed the diff before it was submitted.

🤖 Generated with Claude Code

CustomPayloadPacket.read only read the payload when 0 < size < 32767,
while the constructor and write allow up to 32767 bytes. A 32767-byte
payload was written but not read, desyncing the stream, and a 0-byte
payload left data null, so getSize() threw a NullPointerException.

Now a negative size throws an IOException and any other size is read
in full (an empty array for 0).

This change was written by an AI coding assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@SpaceWalkerRS

Copy link
Copy Markdown
Member

Hi, thanks for reporting the issue. I will close this PR though, as we do not accept AI-generated code/code changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants