Skip to content

Commit bd40b45

Browse files
committed
Fix race condition in Range.hashCode()
1 parent 03aa5a0 commit bd40b45

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ The <action> type attribute can be add,update,fix,remove.
7575
<action issue="LANG-1800" type="fix" dev="ggregory" due-to="IcoreE">Incorrect grammar and unclear wording in RandomStringUtils#random method #1520.</action>
7676
<action issue="LANG-1802" type="fix" dev="ggregory" due-to="Gary Gregory, IcoreE">Fix collision in CharRange.hashCode().</action>
7777
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix race condition in Fraction.hashCode().</action>
78+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix race condition in Range.hashCode().</action>
7879
<!-- ADD -->
7980
<!-- UPDATE -->
8081
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 92 to 93 #1498.</action>

src/main/java/org/apache/commons/lang3/Range.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*
3131
* @param <T> The type of range values.
3232
* @since 3.0
33+
* @since 3.20.1 {@code serialVersionUID} changed from {@code 1L} to {@code 2L}.
3334
*/
3435
public class Range<T> implements Serializable {
3536

@@ -54,8 +55,9 @@ public int compare(final Object obj1, final Object obj2) {
5455
* Serialization version.
5556
*
5657
* @see java.io.Serializable
58+
* @since 3.20.1 {@code serialVersionUID} changed from {@code 1L} to {@value}.
5759
*/
58-
private static final long serialVersionUID = 1L;
60+
private static final long serialVersionUID = 2L;
5961

6062
/**
6163
* Creates a range with the specified minimum and maximum values (both inclusive).
@@ -191,7 +193,7 @@ public static <T> Range<T> of(final T fromInclusive, final T toInclusive, final
191193
/**
192194
* Cached output hashCode (class is immutable).
193195
*/
194-
private transient int hashCode;
196+
private final int hashCode;
195197

196198
/**
197199
* The maximum value in this range (inclusive).
@@ -233,6 +235,7 @@ public static <T> Range<T> of(final T fromInclusive, final T toInclusive, final
233235
this.minimum = element2;
234236
this.maximum = element1;
235237
}
238+
this.hashCode = Objects.hash(minimum, maximum);
236239
}
237240

238241
/**
@@ -383,15 +386,7 @@ public T getMinimum() {
383386
*/
384387
@Override
385388
public int hashCode() {
386-
int result = hashCode;
387-
if (hashCode == 0) {
388-
result = 17;
389-
result = 37 * result + getClass().hashCode();
390-
result = 37 * result + minimum.hashCode();
391-
result = 37 * result + maximum.hashCode();
392-
hashCode = result;
393-
}
394-
return result;
389+
return hashCode;
395390
}
396391

397392
/**

0 commit comments

Comments
 (0)