Skip to content

Commit 6612200

Browse files
committed
Take advantage of Java 25 Flexible Constructor Bodies JEP 513.
And remove the awkward constructors used to avoid Finalizer Attacks.
1 parent eba1d8c commit 6612200

3 files changed

Lines changed: 12 additions & 62 deletions

File tree

src/main/java/org/apache/datasketches/quantiles/DoublesSketchAccessor.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,16 @@ abstract class DoublesSketchAccessor extends DoublesBufferAccessor {
4444
final DoublesSketch ds,
4545
final boolean forceSize,
4646
final int level) {
47-
this(checkLvl(level), ds, forceSize, level);
48-
//SpotBugs CT_CONSTRUCTOR_THROW is false positive.
49-
//this construction scheme is compliant with SEI CERT Oracle Coding Standard for Java / OBJ11-J
50-
}
51-
52-
private DoublesSketchAccessor(
53-
@SuppressWarnings("unused") final boolean secure, //required part of Finalizer Attack prevention
54-
final DoublesSketch ds,
55-
final boolean forceSize,
56-
final int level) {
47+
checkLvl(level);
5748
ds_ = ds;
5849
forceSize_ = forceSize;
5950
setLevel(level);
6051
}
6152

62-
private static final boolean checkLvl(final int level) {
53+
private static final void checkLvl(final int level) {
6354
if ((level != BB_LVL_IDX) && (level < 0)) {
6455
throw new SketchesArgumentException("Parameter level is < 0.");
6556
}
66-
return true;
6757
}
6858

6959
/**

src/main/java/org/apache/datasketches/tuple/QuickSelectSketch.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ private QuickSelectSketch(
135135
final float samplingProbability,
136136
final SummaryFactory<S> summaryFactory,
137137
final int startingSize) {
138+
final long thetaLong = (long) (Long.MAX_VALUE * (double) samplingProbability);
138139
super(
139-
(long) (Long.MAX_VALUE * (double) samplingProbability),
140+
thetaLong,
140141
true,
141142
summaryFactory);
142143
nomEntries_ = ceilingPowerOf2(nomEntries);
@@ -182,23 +183,9 @@ private QuickSelectSketch(
182183
final MemorySegment seg,
183184
final SummaryDeserializer<S> deserializer,
184185
final SummaryFactory<S> summaryFactory) {
185-
this(new Validate<>(), seg, deserializer, summaryFactory);
186-
}
187-
188-
/*
189-
* This private constructor is used to protect against "Finalizer attacks".
190-
* The private static inner class Validate performs validation and deserialization
191-
* from the input MemorySegment and may throw exceptions. In order to protect against the attack, we must
192-
* perform this validation prior to the constructor's super reaches the Object class.
193-
* Making QuickSelectSketch final won't work here because UpdatableSketch is a subclass.
194-
* Using an empty final finalizer() is not recommended and is deprecated as of Java9.
195-
*/
196-
private QuickSelectSketch(
197-
final Validate<S> val,
198-
final MemorySegment seg,
199-
final SummaryDeserializer<S> deserializer,
200-
final SummaryFactory<S> summaryFactory) {
201-
super(val.validate(seg, deserializer), val.myEmpty, summaryFactory);
186+
//this(new Validate<>(), seg, deserializer, summaryFactory);
187+
final Validate<S> val = new Validate<>();
188+
final long thetaLong = val.validate(seg, deserializer);
202189
nomEntries_ = val.myNomEntries;
203190
lgResizeFactor_ = val.myLgResizeFactor;
204191
samplingProbability_ = val.mySamplingProbability;
@@ -207,6 +194,7 @@ private QuickSelectSketch(
207194
rebuildThreshold_ = val.myRebuildThreshold;
208195
hashTable_ = val.myHashTable;
209196
summaryTable_ = val.mySummaryTable;
197+
super(thetaLong, val.myEmpty, summaryFactory);
210198
}
211199

212200
private static final class Validate<S> {

src/main/java/org/apache/datasketches/tuple/arrayofdoubles/DirectArrayOfDoublesQuickSelectSketch.java

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,7 @@ class DirectArrayOfDoublesQuickSelectSketch extends ArrayOfDoublesQuickSelectSke
7979
final int numValues,
8080
final long seed,
8181
final MemorySegment dstSeg) {
82-
this(checkMemorySegment(nomEntries, lgResizeFactor, numValues, dstSeg),
83-
//SpotBugs CT_CONSTRUCTOR_THROW is false positive.
84-
//this construction scheme is compliant with SEI CERT Oracle Coding Standard for Java / OBJ11-J
85-
nomEntries,
86-
lgResizeFactor,
87-
samplingProbability,
88-
numValues,
89-
seed,
90-
dstSeg);
91-
}
92-
93-
private DirectArrayOfDoublesQuickSelectSketch(
94-
@SuppressWarnings("unused") final boolean secure, //required part of Finalizer Attack prevention
95-
final int nomEntries,
96-
final int lgResizeFactor,
97-
final float samplingProbability,
98-
final int numValues,
99-
final long seed,
100-
final MemorySegment dstSeg) {
82+
checkMemorySegment(nomEntries, lgResizeFactor, numValues, dstSeg);
10183
super(numValues, seed);
10284
seg_ = dstSeg;
10385
final int startingCapacity = Util.getStartingCapacity(nomEntries, lgResizeFactor);
@@ -126,14 +108,13 @@ private DirectArrayOfDoublesQuickSelectSketch(
126108
setRebuildThreshold();
127109
}
128110

129-
private static final boolean checkMemorySegment(
111+
private static final void checkMemorySegment(
130112
final int nomEntries,
131113
final int lgResizeFactor,
132114
final int numValues,
133115
final MemorySegment dstSeg) {
134116
final int startingCapacity = Util.getStartingCapacity(nomEntries, lgResizeFactor);
135117
checkMemorySegmentSize(dstSeg, startingCapacity, numValues);
136-
return true;
137118
}
138119

139120
/**
@@ -144,15 +125,7 @@ private static final boolean checkMemorySegment(
144125
DirectArrayOfDoublesQuickSelectSketch(
145126
final MemorySegment seg,
146127
final long seed) {
147-
this(checkSerVer(seg), seg, seed);
148-
//SpotBugs CT_CONSTRUCTOR_THROW is false positive.
149-
//this construction scheme is compliant with SEI CERT Oracle Coding Standard for Java / OBJ11-J
150-
}
151-
152-
private DirectArrayOfDoublesQuickSelectSketch(
153-
@SuppressWarnings("unused") final boolean secure, //required part of Finalizer Attack prevention
154-
final MemorySegment seg,
155-
final long seed) {
128+
checkSerVer(seg);
156129
super(seg.get(JAVA_BYTE, NUM_VALUES_BYTE), seed);
157130
seg_ = seg;
158131
SerializerDeserializer.validateFamily(seg.get(JAVA_BYTE, FAMILY_ID_BYTE),
@@ -170,13 +143,12 @@ private DirectArrayOfDoublesQuickSelectSketch(
170143
setRebuildThreshold();
171144
}
172145

173-
private static final boolean checkSerVer(final MemorySegment seg) {
146+
private static final void checkSerVer(final MemorySegment seg) {
174147
final byte version = seg.get(JAVA_BYTE, SERIAL_VERSION_BYTE);
175148
if (version != serialVersionUID) {
176149
throw new SketchesArgumentException("Serial version mismatch. Expected: " + serialVersionUID
177150
+ ", actual: " + version);
178151
}
179-
return true;
180152
}
181153

182154
@Override

0 commit comments

Comments
 (0)