Uploaded image for project: 'Minecraft: Java Edition'
  1. Minecraft: Java Edition
  2. MC-247438

Chunk Data packet allocates an extra byte for SingleValuePalettes

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • None
    • 1.18.1
    • None
    • Confirmed
    • Networking
    • Low

      The data buffer created by ClientboundLevelChunkPacketData contains an extra byte for each LevelChunkSection using a SingleValuePalette in the column.

      This is caused by the incorrect getSerializedSize() calculation in the Data class of PalettedContainer:

      public int getSerializedSize() {
          return 1 + this.palette.getSerializedSize() + FriendlyByteBuf.getVarIntSize(this.storage.getSize()) + this.storage.getRaw().length * 8;
      }
      

      The this.storage.getSize() call returns the number of states in storage, which is 4096, instead of the size of the storage array, zero. This allocates an extra byte for the varint. I'm unsure if this can cause bugs for other palette types, but definitely it's calculating a size for a different value than the one being serialized which seems dangerous.

      Fixing the bug is trivial, use the same length to calculate the varint as you do to calculate the array size, which is what gets serialized later:

      public int getSerializedSize() {
          return 1 + this.palette.getSerializedSize() + FriendlyByteBuf.getVarIntSize(this.storage.getRaw().length) + this.storage.getRaw().length * 8;
      }
      

      This is similar in nature but distinct from https://bugs.mojang.com/browse/MC-131684

            Unassigned Unassigned
            nickelpro Vito
            Votes:
            4 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              CHK: