Skip to content

GH-1230: fix out-of-bounds write in ClobConsumer buffer growth - #1231

Merged
jbonofre merged 1 commit into
apache:mainfrom
Arawoof06:clob-consumer-oob-write
Nov 15, 2026
Merged

jbonofre merged 1 commit into
apache:mainfrom
Arawoof06:clob-consumer-oob-write

Conversation

@Arawoof06

Copy link
Copy Markdown
Contributor

What's Changed

ClobConsumer grows the VarCharVector data buffer with a guard on dataBuffer.writerIndex() + bytes.length > capacity(), but it writes each chunk at startIndex + totalBytes through a raw MemoryUtil.copyToMemory that never advances writerIndex(), so the guard stays stuck at offset 0 and reallocDataBuffer() is never called for a CLOB whose bytes exceed the initial ~32KB allocation. The unchecked write then runs past the buffer into adjacent off-heap memory. Both consume paths now check capacity() < startIndex + totalBytes + bytes.length and re-read the buffer after a realloc, matching the sibling BinaryConsumer.

Closes #1230.

@github-actions

This comment has been minimized.

@lidavidm lidavidm added the bug-fix PRs that fix a big. label Jul 15, 2026
@github-actions github-actions Bot added this to the 20.0.0 milestone Jul 15, 2026
@Arawoof06

Copy link
Copy Markdown
Contributor Author

gentle ping

@lidavidm

Copy link
Copy Markdown
Member

@Arawoof06 please rebase

@Arawoof06
Arawoof06 force-pushed the clob-consumer-oob-write branch from 5a1bf1b to b3eb9d4 Compare August 25, 2026 11:11
@Arawoof06

Copy link
Copy Markdown
Contributor Author

Rebased onto main.

@Arawoof06
Arawoof06 force-pushed the clob-consumer-oob-write branch from b3eb9d4 to 333174d Compare October 5, 2026 19:19
@Arawoof06

Copy link
Copy Markdown
Contributor Author

Rebased onto current main again. Test/Dev/RC never started any jobs on the August push, and they're waiting on workflow approval for this one.

@jbonofre jbonofre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! LGTM

I checked it locally:

  • The new test fails on the previous code. The out-of-bounds write was overwriting the offset buffer, and the read then failed with a NegativeArraySizeException. It passes with this change.
  • I also ran both the nullable and non-nullable consumers on multiple rows, where a later row starts mid-buffer and crosses capacity, with both ASCII and multi-byte data. All the values come back intact.

@jbonofre
jbonofre merged commit a1bffc2 into apache:main Nov 15, 2026
1 check passed
jbonofre added a commit that referenced this pull request Nov 16, 2026
## What's Changed

Follow-up to #1231. `ClobConsumer` reads a CLOB in chunks of 256
characters and encodes each chunk to UTF-8 on its own, so a character
outside the BMP split between two chunks was encoded as `??`.

- A chunk that ends with a high surrogate, while characters remain, now
leaves it for the next chunk, so the pair is encoded together.
- The read position advances by the length of the chunk instead of the
requested size. The shorter chunk needs this, and it also stops skipping
characters when a driver returns fewer than requested.
- An empty chunk before the end of the CLOB throws an `SQLException`
instead of looping forever.

`ClobConsumerTest` covers emojis across chunk boundaries for both
consumers. The test fails without the fix: 255 `a` followed by 😀 read
back as 257 bytes instead of 259.

This changes other lines of `ClobConsumer` than #1479, and the two
branches merge cleanly.

Closes #1478.
jbonofre added a commit that referenced this pull request Nov 16, 2026
## What's Changed

Follow-up to #1231. A NULL row in `ClobConsumer` doesn't write its end
offset, so the next value read its start offset from that unwritten slot
(`0`) and was written over the earlier values: rows `"AAAA", NULL, "BB"`
read back as `"BBAA", NULL, "BB"`, with decreasing offsets.

Both consumers now call `vector.fillEmpties(currentIndex)` before
writing a value, as `setSafe()` does. It gives the NULL rows since the
last value empty offsets, and also grows the validity and offset buffers
when `currentIndex` is past the value capacity, as `BinaryConsumer`
does. Before, consuming more rows than the value capacity (3970 by
default) failed with an `IndexOutOfBoundsException`.

`ClobConsumerTest` covers NULL rows before, between and after values for
both consumers, and twice the default value capacity of rows. The three
new tests fail without the fix.

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

Labels

bug-fix PRs that fix a big.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Java][JDBC] ClobConsumer writes past VarCharVector data buffer for large CLOBs

3 participants