From d71a2e5ee6f07eed2933e5d9ca1ba7ab86d030ee Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Thu, 16 Jan 2025 14:28:47 +0100 Subject: [PATCH] HHH-19047 Prevent GC nepotism from affecting `StandardStack` --- .../internal/util/collections/StandardStack.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/collections/StandardStack.java b/hibernate-core/src/main/java/org/hibernate/internal/util/collections/StandardStack.java index c27f5a973578..013097e96847 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/collections/StandardStack.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/collections/StandardStack.java @@ -19,6 +19,7 @@ * @author Sanne Grinovero * @author Marco Belladelli */ +@SuppressWarnings("unchecked") public final class StandardStack implements Stack { private Object[] elements; @@ -107,8 +108,8 @@ public boolean isEmpty() { @Override public void clear() { - for ( int i = 0; i < top; i++ ) { - elements[i] = null; + if ( elements != null ) { + Arrays.fill( elements, 0, top, null ); } top = 0; } @@ -145,7 +146,10 @@ public X findCurrentFirstWithParameter(Y parameter, BiFunction b private void grow() { final int oldCapacity = elements.length; final int jump = ( oldCapacity < 64 ) ? ( oldCapacity + 2 ) : ( oldCapacity >> 1 ); - elements = Arrays.copyOf( elements, oldCapacity + jump ); + final Object[] newElements = Arrays.copyOf( elements, oldCapacity + jump ); + // Prevents GC nepotism on the old array elements, see HHH-19047 + Arrays.fill( elements, null ); + elements = newElements; } }