diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 79b27e9d4051..39629386289c 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -165,6 +165,9 @@ Optimizations Bug Fixes --------------------- +* Fix missing null check in RamUsageEstimator.sizeOf(Accountable) to be consistent + with sizeOf(String) and sizeOf(Accountable[]). (Tim Grein) + * GITHUB#14049: Randomize KNN codec params in RandomCodec. Fixes scalar quantization div-by-zero when all values are identical. (Mike Sokolov) diff --git a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java index 08b3cd1fb3e1..9041581efa49 100644 --- a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java +++ b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java @@ -425,9 +425,12 @@ private static long sizeOfObject(Object o, int depth, long defSize) { /** * Returns the size in bytes of the {@link Accountable} object, using its {@link - * Accountable#ramBytesUsed()} method. + * Accountable#ramBytesUsed()} method. Returns 0, if the {@link Accountable} object is null. */ public static long sizeOf(Accountable accountable) { + if (accountable == null) { + return 0; + } return accountable.ramBytesUsed(); } diff --git a/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java b/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java index 8e0bd13507ea..5ffb7e9c3e01 100644 --- a/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java +++ b/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java @@ -150,6 +150,11 @@ public void testMap() { assertEquals((double) actual, (double) estimated, (double) actual * errorFactor); } + public void testAccountable() { + assertEquals(0L, RamUsageEstimator.sizeOf((Accountable) null)); + assertEquals(1L, RamUsageEstimator.sizeOf(new DummyAccountable())); + } + public void testCollection() { List list = new ArrayList<>(); list.add(1234L); @@ -252,4 +257,11 @@ private static class HolderSubclass extends Holder { private static class HolderSubclass2 extends Holder { // empty, only inherits all fields -> size should be identical to superclass } + + private static class DummyAccountable implements Accountable { + @Override + public long ramBytesUsed() { + return 1L; + } + } }