Skip to content

GH-1098: [Java][Vector] Fix always-true precondition check in LargeListVector.setValueCount#1099

Open
LuciferYang wants to merge 1 commit intoapache:mainfrom
LuciferYang:fix-large-list-vector-precondition-logic
Open

GH-1098: [Java][Vector] Fix always-true precondition check in LargeListVector.setValueCount#1099
LuciferYang wants to merge 1 commit intoapache:mainfrom
LuciferYang:fix-large-list-vector-precondition-logic

Conversation

@LuciferYang
Copy link
Copy Markdown

What's Changed

Fix the precondition check in LargeListVector.setValueCount() where || made the condition always true (childValueCount <= Integer.MAX_VALUE || childValueCount >= Integer.MIN_VALUE is a tautology for any long value), allowing silent data corruption when childValueCount exceeds Integer.MAX_VALUE or is negative.

Changes:

  • Replace || with && and Integer.MIN_VALUE with 0 to correctly bound childValueCount to [0, Integer.MAX_VALUE], consistent with LargeListViewVector
  • Add rejection tests for negative and overflow childValueCount
  • Add positive boundary tests for zero, valid, and Integer.MAX_VALUE values

Closes #1098.

@github-actions

This comment has been minimized.

@LuciferYang
Copy link
Copy Markdown
Author

Could a maintainer please add the bug-fix label to this PR? I don't have label permissions on this repository. Thank you!

@lidavidm lidavidm added the bug-fix PRs that fix a big. label Mar 30, 2026
@github-actions github-actions bot added this to the 20.0.0 milestone Mar 30, 2026
Copy link
Copy Markdown
Contributor

@ennuite ennuite left a comment

Choose a reason for hiding this comment

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

Good one. I looked into this: the bug has been there since the class was created, see 4deba44#diff-c3066ea815144f3e05b277c87cae8a7701f0f1481e15da508261664ab017d944R984
The PR fixes the bug and has test coverage for all equivalence classes. The correct values are in the interval [0, Integer.MAX_Value]; there are tests for a negative value, 0, a positive value smaller than Integer.MAX_Value, Integer.MAX_Value and a positive value greater than Integer.MAX_Value.

Thank you

*/
Preconditions.checkArgument(
childValueCount <= Integer.MAX_VALUE || childValueCount >= Integer.MIN_VALUE,
childValueCount <= Integer.MAX_VALUE && childValueCount >= 0,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: I'd use childValueCount >= 0 && childValueCount <= Integer.MAX_VALUE for the sake of readability.

vector.setLastSet(0);

assertThrows(
IllegalArgumentException.class,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The error message throwing here is a bit confusing: IllegalArgument LargeListVector doesn't yet support 64-bit allocations: -1

// The Preconditions check should accept this value (not throw IllegalArgumentException).
// The child vector may throw OversizedAllocationException due to memory limits,
// which is expected and unrelated to the precondition validation.
vector.getOffsetBuffer().setLong(LargeListVector.OFFSET_WIDTH, (long) Integer.MAX_VALUE);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Casting 'Integer.MAX_VALUE' to 'long' is redundant

// when trying to allocate Integer.MAX_VALUE elements — this is fine,
// the precondition check itself passed.
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Maybe this can be replaced with

try {
        vector.setValueCount(1);
      } catch (IllegalArgumentException e) {
        fail("setValueCount should not reject childValueCount = Integer.MAX_VALUE", e);
      } catch (OversizedAllocationException | OutOfMemoryException e) {
        // OversizedAllocationException or other allocation errors are expected
        // when trying to allocate Integer.MAX_VALUE elements — this is fine,
        // the precondition check itself passed.
      }

@axreldable
Copy link
Copy Markdown
Contributor

Thank you for the bug-fix @LuciferYang ! I've left some comments. My main concern is the confusing error message for small negative values.

@LuciferYang
Copy link
Copy Markdown
Author

@axreldable Tbanks for your review.I am on vacation, I will provide fixes and feedback on your suggestions in 3 days

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][Vector] Always-true precondition check in LargeListVector.setValueCount allows silent data corruption

4 participants