Correctly update size of the Stack before doing any validation in Recycler (#9731)

Motivation:

We null out the element in the array after we decrement the current size of the Stack but not directly write back the updated size to the stored field. This is problematic as we do some validation before we write it back and so may never do so if the validation fails. This then later can lead to have null objects returned where not expected

Modifications:

Update size directly after null out object

Result:

No more unexpected null value possible
This commit is contained in:
Norman Maurer 2019-10-31 09:52:07 +01:00 committed by GitHub
parent 9976ab7fe8
commit 7c85c9ea0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -518,12 +518,16 @@ public abstract class Recycler<T> {
size --;
DefaultHandle ret = elements[size];
elements[size] = null;
// As we already set the element[size] to null we also need to store the updated size before we do
// any validation. Otherwise we may see a null value when later try to pop again without a new element
// added before.
this.size = size;
if (ret.lastRecycledId != ret.recycleId) {
throw new IllegalStateException("recycled multiple times");
}
ret.recycleId = 0;
ret.lastRecycledId = 0;
this.size = size;
return ret;
}