aosen-xiong opened a new pull request, #1811:
URL: https://github.com/apache/commons-lang/pull/1811

   Jira: [LANG-1837](https://issues.apache.org/jira/browse/LANG-1837)
   
   `Fraction.toString()`, `Fraction.toProperString()`, `Range.toString()` and 
`CharRange.toString()` cache their result in a non-volatile field and read that 
field twice: once for the null check and once for the return.
   
   ```java
   if (toString == null) {
       toString = ...;
   }
   return toString;
   ```
   
   If the first read sees a value written by another thread, the Java Memory 
Model does not order the second read after that write, so the second read may 
still return `null` ([JLS 
17.4](https://docs.oracle.com/javase/specs/jls/se21/html/jls-17.html#jls-17.4)) 
and the method can return `null`. All three classes are documented as immutable.
   
   This change reads each field once into a local variable, then tests and 
returns the local. There is no functional change for single-threaded callers.
   
   ### Precedent
   
   - In this project: a64817368d, "Fix race condition in Fraction.hashCode()". 
This pull request does the same for the remaining lazily initialized fields in 
these classes.
   - In OpenJDK, the same double read was filed and fixed as a bug four times:
     - [JDK-8302822](https://bugs.openjdk.org/browse/JDK-8302822), 
"Method/Field/Constructor/RecordComponent::getGenericInfo() is not thread safe" 
([review](https://github.com/openjdk/jdk/pull/12643)). Its description: "the 
genericInfo field is read twice, and the second read returned may be null under 
race conditions".
     - [JDK-8291061](https://bugs.openjdk.org/browse/JDK-8291061), "Improve 
thread safety of FileTime.toString and toInstant" 
([review](https://github.com/openjdk/jdk/pull/9608)), for an immutable class 
with two lazily initialized fields.
     - [JDK-8261404](https://bugs.openjdk.org/browse/JDK-8261404), 
"Class.getReflectionFactory() is not thread-safe" 
([review](https://github.com/openjdk/jdk/pull/6870)).
     - [JDK-8166842](https://bugs.openjdk.org/browse/JDK-8166842), 
"String.hashCode() has a non-benign data race".
   
   ### Testing
   
   The existing `FractionTest`, `RangeTest` and `CharRangeTest` cover the 
string formats and pass. I did not add a test that fails without the change: 
the stale read is permitted by the memory model but cannot be triggered on 
demand.
   
   ### How this was found
   
   By a static analysis for this pattern, confirmed by reading the code. I have 
not observed a failure.
   
   <!--
     Licensed to the Apache Software Foundation (ASF) under one
     or more contributor license agreements.  See the NOTICE file
     distributed with this work for additional information
     regarding copyright ownership.  The ASF licenses this file
     to you under the Apache License, Version 2.0 (the
     "License"); you may not use this file except in compliance
     with the License.  You may obtain a copy of the License at
   
       https://www.apache.org/licenses/LICENSE-2.0
   
     Unless required by applicable law or agreed to in writing,
     software distributed under the License is distributed on an
     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     KIND, either express or implied.  See the License for the
     specific language governing permissions and limitations
     under the License.
   -->
   
   Thanks for your contribution to [Apache 
Commons](https://commons.apache.org/)! Your help is appreciated!
   
   Before you push a pull request, review this list:
   
   - [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
   - [x] Read the [ASF Generative Tooling 
Guidance](https://www.apache.org/legal/generative-tooling.html) if you use 
Artificial Intelligence (AI).
   - [x] I used AI to create any part of, or all of, this pull request. Which 
AI tool was used to create this pull request, and to what extent did it 
contribute? **Claude Opus 5.5. It helped draft the four-method patch and this 
description; I reviewed both. The commit carries a `Co-Authored-By` line.**
   - [x] Run a successful build using the default 
[Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command 
line by itself. **BUILD SUCCESS on JDK 21: 85,511 tests, 0 failures, 0 
Checkstyle violations.**
   - [ ] Write unit tests that match behavioral changes, where the tests fail 
if the changes to the runtime are not applied. This may not always be possible, 
but it is a best practice. **Not possible here; see "Testing" above.**
   - [x] Write a pull request description that is detailed enough to understand 
what the pull request does, how, and why.
   - [x] Each commit in the pull request should have a meaningful subject line 
and body. Note that a maintainer may squash commits during the merge process.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to