Skip to content

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

In review

Description

@jbonofre

Describe the bug, including details regarding any error messages, version, and platform.

BufferLedger.retain(int) adds increment to the reference count without checking for overflow. A retain that takes the count past Integer.MAX_VALUE silently wraps it to a negative value:

try (RootAllocator allocator = new RootAllocator()) {
  ArrowBuf buf = allocator.buffer(512);
  buf.getReferenceManager().retain(Integer.MAX_VALUE); // count wraps to Integer.MIN_VALUE, no error
}
// closing the allocator then reports the 512 bytes as leaked

Once the count is negative:

  • the ledger can never be released back to 0, so the memory leaks;
  • ArrowBuf.ensureAccessible() only rejects a count of exactly 0, so the buffer stays accessible;
  • every later retain() is rejected, as if the ledger had been released.

The retain should be rejected without changing the count, as #906 now does for a released ledger.

Two smaller follow-ups from #1543 in the same code:

  • In debug mode, retain records the event in the ledger history before checking the count, so a rejected retain on a released ledger shows up in the history as if it had succeeded.
  • testRetainOnReleasedBufferIsRejectedRepeatedly expects setInt on a released buffer to throw. That only holds when bounds checking is enabled, so the test fails with ARROW_ENABLE_UNSAFE_MEMORY_ACCESS=true.

Activity

  1. added
    Type: bug
    on Dec 2, 2026
  2. added this to the 20.1.0 milestone on Dec 2, 2026
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

    Type: bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions