GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ProtobufVarint32FrameDecoder desynchronizes the stream when a 4-byte length prefix is split after 3 bytes · Issue #17549 · netty/netty

ProtobufVarint32FrameDecoder desynchronizes the stream when a 4-byte length prefix is split after 3 bytes #17549

New issue
Copy link
New issue
Copy link
Closed
#17550
Closed
ProtobufVarint32FrameDecoder desynchronizes the stream when a 4-byte length prefix is split after 3 bytes#17549
#17550
Copy link
Milestone
4.2.19.Final

Description

@ohchanKyu
ohchanKyu
opened on Sep 20, 2026
Issue body actions

Expected behavior

ProtobufVarint32FrameDecoder must produce the same frames no matter how the bytes are split across reads.
If only part of the varint length prefix has arrived, it has to leave the reader index untouched and wait for more data.
That is also what the contract of the internal reader says: readRawVarint32 returns a usable value only
"if buffers readerIndex has been forwarded".

Actual behavior

Since #14050 ("Branchless varint decoding"), two incomplete-prefix cases are no longer treated as "need more data":

  1. 3 continuation bytes buffered (readRawVarint24). When fewer than 4 bytes are readable the decoder falls back to readRawVarint24. After reading a third byte that still has the continuation bit set, it ends with

    return result | (tmp & 127) << 14;

    instead of resetting the reader index and returning 0. The reader index has moved by 3, so decode() believes a complete length was read and uses only the low 21 bits of the real length.

    If those 21 bits are non-zero the bug is masked by luck: in.readableBytes() < length is true, decode() resets the reader index and the next read decodes correctly. If they are zero, i.e. the message length is an exact multiple of 2 MiB (2097152, 4194304, ...), length is 0, an empty frame is emitted, the 3 prefix bytes are consumed, and the rest of the stream is parsed from the wrong position. Every following payload byte is then interpreted as a length prefix.

  2. 4 continuation bytes buffered (readRawVarint40). For a 5-byte prefix (length >= 2^28) of which only 4 bytes have arrived, the decoder throws CorruptedFrameException("malformed varint.") because of

    if (buffer.readableBytes() == 4 || (lastByte = buffer.getByte(buffer.readerIndex() + 4)) < 0) {
        throw new CorruptedFrameException("malformed varint.");
    }

    The 5th byte is simply not there yet; this is not a malformed varint.

Before #14050 both cases were handled with if (!buffer.isReadable()) { buffer.resetReaderIndex(); return 0; } ahead of the 4th and the 5th byte.

Output of the reproducer below on 4.2:

expected: 1 frame of 2097152 bytes
actual  : 2097153 frames, 1 payload bytes, first sizes: 0 1 0 0 0
4 of 5 prefix bytes: io.netty.handler.codec.CorruptedFrameException: malformed varint.

So a single 2 MiB message is delivered to the next handler as one empty frame, one 1-byte frame and then ~2 million empty frames, without any exception. The same happens for 4 MiB (first sizes: 0 2 0 0 0). Sizes such as 3 MiB or 2 MiB + 5 decode correctly for every split position, which is probably why this went unnoticed.

ProtobufVarint32FrameDecoderTest only splits 1-byte and 2-byte prefixes, so neither case is covered.

Steps to reproduce

  1. Encode a message whose length is an exact multiple of 2 MiB with ProtobufVarint32LengthFieldPrepender (prefix 80 80 80 01).
  2. Feed it to ProtobufVarint32FrameDecoder in two reads, the first one ending after the 3rd prefix byte.

For the second case, feed 80 80 80 80 as a single read.

Minimal yet complete reproducer code (or URL to code)

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;

public class VarintSplitRepro {
    public static void main(String[] args) {
        // 1) Frame a 2 MiB message with Netty's own prepender. The length prefix is 80 80 80 01.
        int size = 2 * 1024 * 1024;
        EmbeddedChannel enc = new EmbeddedChannel(new ProtobufVarint32LengthFieldPrepender());
        enc.writeOutbound(Unpooled.wrappedBuffer(new byte[size]));
        ByteBuf wire = Unpooled.buffer();
        for (ByteBuf b; (b = enc.readOutbound()) != null;) {
            wire.writeBytes(b);
            b.release();
        }

        // 2) Deliver it in two reads, the first one ending after 3 of the 4 prefix bytes.
        EmbeddedChannel dec = new EmbeddedChannel(new ProtobufVarint32FrameDecoder());
        dec.writeInbound(wire.retainedSlice(0, 3));
        dec.writeInbound(wire.retainedSlice(3, wire.readableBytes() - 3));

        int frames = 0;
        long bytes = 0;
        StringBuilder first = new StringBuilder();
        for (ByteBuf f; (f = dec.readInbound()) != null;) {
            if (frames < 5) {
                first.append(f.readableBytes()).append(' ');
            }
            frames++;
            bytes += f.readableBytes();
            f.release();
        }
        System.out.println("expected: 1 frame of " + size + " bytes");
        System.out.println("actual  : " + frames + " frames, " + bytes + " payload bytes, first sizes: " + first);

        // 3) 5-byte prefix (length >= 2^28) where only 4 bytes have arrived yet.
        EmbeddedChannel dec2 = new EmbeddedChannel(new ProtobufVarint32FrameDecoder());
        try {
            dec2.writeInbound(Unpooled.wrappedBuffer(new byte[] {(byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80}));
            System.out.println("4 of 5 prefix bytes: waits for more data");
        } catch (Exception e) {
            System.out.println("4 of 5 prefix bytes: " + e);
        }
    }
}

Netty version

4.2 branch (8af38d1487). Introduced by #14050 (09b4ea6a1f), so every release since 4.1.110.Final is affected; the current 4.1 branch still has the same code.

JVM version (e.g. java -version)

openjdk version "21.0.9" 2025-10-21

OS version (e.g. uname -a)

Darwin 25.5.0 arm64 (not platform-specific)


I can submit a PR that restores the "wait for more data" behaviour in both paths and adds a test that splits 1- to 5-byte prefixes at every position.

Reacted by Francesco Nigro

Activity

  1. franz1981 commented on Sep 20, 2026

    @franz1981
    franz1981
    on Sep 20, 2026
    Contributor
    More actions

    Yes @ohchanKyu thanks 🙏

    Just to understand how did you discovered? Bug in production?

  2. normanmaurer
    closed this as completedin #17550on Sep 20, 2026
  3. hyperxpro
    added a commit that references this issue on Sep 20, 2026

    ProtobufVarint32FrameDecoder must wait for more data when the length …

    This issue will close once commit 335aed7 is merged into the '4.2' branch.
    335aed7
  4. normanmaurer
    added a commit that references this issue on Sep 20, 2026

    Auto-port 4.1: ProtobufVarint32FrameDecoder must wait for more data w…

    This issue will close once commit 7375bc7 is merged into the '4.2' branch.
    7375bc7
  5. normanmaurer
    added this to the 4.2.19.Final milestone on Sep 20, 2026
  6. normanmaurer
    added a commit that references this issue on Sep 20, 2026

    Auto-port 5.0: ProtobufVarint32FrameDecoder must wait for more data w…

    This issue will close once commit 0c32a23 is merged into the '4.2' branch.
    0c32a23
  7. ohchanKyu commented on Sep 21, 2026

    @ohchanKyu
    ohchanKyu
    on Sep 21, 2026
    ContributorAuthor
    More actions

    Yes @ohchanKyu thanks 🙏

    Just to understand how did you discovered? Bug in production?

    @franz1981
    Thanks! 🙏 Not a production bug.
    I found it while looking into the varint decoding logic and testing how it behaves when the length prefix is split across reads.

    Reacted by Francesco Nigro
  8. lltopk
    added a commit that references this issue on Oct 24, 2026

    Auto-port 4.1: ProtobufVarint32FrameDecoder must wait for more data w…

    This issue will close once commit 5d76ebf is merged into the '4.2' branch.
    5d76ebf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      • 4.2.19.Final

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.