Skip to content

GH-1544: Reject reference count overflow in BufferLedger.retain - #1545

Open
jbonofre wants to merge 1 commit into
apache:mainfrom
jbonofre:gh-1544-ledger-retain-overflow
Open

jbonofre wants to merge 1 commit into
apache:mainfrom
jbonofre:gh-1544-ledger-retain-overflow

Conversation

@jbonofre

@jbonofre jbonofre commented Dec 2, 2026

Copy link
Copy Markdown
Member

What's Changed

Follow-up to #1543, which made BufferLedger.retain(int) check the count before its compare-and-set so that a released ledger is never revived.

That loop still adds increment without checking for overflow, so a retain past Integer.MAX_VALUE wraps the count negative. A negative count can never be released back to 0, so the memory leaks. ArrowBuf.ensureAccessible() only rejects a count of exactly 0, so the buffer also stays accessible. The loop now also checks increment <= Integer.MAX_VALUE - originalReferenceCount before the compare-and-set. A retain that would overflow throws IllegalArgumentException and leaves the count unchanged, like a retain on a released ledger.

Two smaller changes in the same code:

  • In debug mode, the retain event is now recorded in the ledger history after the compare-and-set succeeds instead of before the check. A rejected retain on a released ledger no longer shows up in the history as if it had revived the ledger.
  • Both error messages now include the increment and the current count. A retain on an over-released ledger (count -1) is no longer reported the same as one at 0.

testRetainOnReleasedBufferIsRejectedRepeatedly now only expects setInt on a released buffer to throw when bounds checking is enabled. ArrowBuf does not check the reference count otherwise, so with ARROW_ENABLE_UNSAFE_MEMORY_ACCESS=true the test wrote into freed memory instead of throwing.

The new testRetainRejectsReferenceCountOverflow fails on main at its first assertThrows. The memory-core, memory-unsafe and memory-netty suites pass.

Closes #1544.

Check that the increment fits before the compare-and-set, so a retain
that would wrap the count negative is rejected and leaves it unchanged.
Record retains in the debug history only once they take effect, and
include the increment and the current count in the error messages.

Only assert that setInt on a released buffer throws when bounds checking
is enabled, since ArrowBuf does not check the reference count otherwise.

Closes apache#1544.
@jbonofre jbonofre added the bug-fix PRs that fix a big. label Dec 2, 2026
@github-actions github-actions Bot added this to the 20.1.0 milestone Dec 2, 2026
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.

BufferLedger.retain can overflow the reference count to a negative value

2 participants