Skip to content

Commit

Permalink
Fix NPE in DocVector ramBytesUsed (elastic#100575)
Browse files Browse the repository at this point in the history
This commit fixes a potential NPE in DocVector::ramBytesUsed.
  • Loading branch information
ChrisHegarty authored Oct 10, 2023
1 parent c8ca0d1 commit 99f0b5c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ public boolean equals(Object obj) {
return shards.equals(other.shards) && segments.equals(other.segments) && docs.equals(other.docs);
}

private static long ramBytesOrZero(int[] array) {
return array == null ? 0 : RamUsageEstimator.shallowSizeOf(array);
}

public static long ramBytesEstimated(
IntVector shards,
IntVector segments,
Expand All @@ -211,7 +215,7 @@ public static long ramBytesEstimated(
int[] shardSegmentDocMapBackwards
) {
return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(shards) + RamUsageEstimator.sizeOf(segments) + RamUsageEstimator.sizeOf(docs)
+ RamUsageEstimator.shallowSizeOf(shardSegmentDocMapForwards) + RamUsageEstimator.shallowSizeOf(shardSegmentDocMapBackwards);
+ ramBytesOrZero(shardSegmentDocMapForwards) + ramBytesOrZero(shardSegmentDocMapBackwards);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ public void testCannotDoubleRelease() {
assertThat(e.getMessage(), containsString("can't build page out of released blocks"));
}

public void testRamBytesUsedWithout() {
DocVector docs = new DocVector(
IntBlock.newConstantBlockWith(0, 1).asVector(),
IntBlock.newConstantBlockWith(0, 1).asVector(),
IntBlock.newConstantBlockWith(0, 1).asVector(),
false
);
assertThat(docs.singleSegmentNonDecreasing(), is(false));
docs.ramBytesUsed(); // ensure non-singleSegmentNonDecreasing handles nulls in ramByteUsed
}

IntVector intRange(int startInclusive, int endExclusive) {
return IntVector.range(startInclusive, endExclusive, BlockFactory.getNonBreakingInstance());
}
Expand Down

0 comments on commit 99f0b5c

Please sign in to comment.