Skip to content

Fix buffer limit in PlanB Filter - clear() missing after flip()#5548

Open
stroomworks4092 wants to merge 1 commit into
gchq:masterfrom
stroomworks:lmdb-size-error
Open

Fix buffer limit in PlanB Filter - clear() missing after flip()#5548
stroomworks4092 wants to merge 1 commit into
gchq:masterfrom
stroomworks:lmdb-size-error

Conversation

@stroomworks4092
Copy link
Copy Markdown
Contributor

@stroomworks4092 stroomworks4092 commented May 19, 2026

Summary of Changes

In stroom-planb/stroom-planb-impl/src/main/java/stroom/planb/impl/pipeline/PlanBFilter.java:

  • Modified the getVal() method specifically for the XML case.
  • Added value.clear() after the ValXml is created from the flipped ByteBuffer.

Why this was necessary
The stagingValueOutputStream (a ByteBufferPoolOutput) is reused for every XML fragment in the stream. When getByteBuffer() is called and then flip() is used to read the data, the buffer's limit is set to the current position
(the size of the data written).

Without a subsequent clear(), the buffer's limit remains restricted to that small size. When the stream is reset for the next fragment and more data is written, the underlying buffer's restricted limit eventually causes a
crash or failure because the stream expects to be able to use the full capacity of the buffer.
'
1 // Before
2 case XML -> {
3 final ByteBuffer value = stagingValueOutputStream.getByteBuffer();
4 value.flip();
5 yield ValXml.create(ByteBufferUtils.getBytes(value));
6 }
7
8 // After
9 case XML -> {
10 final ByteBuffer value = stagingValueOutputStream.getByteBuffer();
11 value.flip();
12 final Val val = ValXml.create(ByteBufferUtils.getBytes(value));
13 value.clear(); // Correctly resets the limit and position for reuse
14 yield val;
15 }
'
The data safety is guaranteed because ValXml.create(ByteBufferUtils.getBytes(value)) creates a complete copy of the bytes into a new byte[] array before the source buffer is cleared.

See https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/nio/ByteBuffer.html#flip() for details on flip() and clear().

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.

1 participant