Skip to content

Commit c738041

Browse files
trancexpressiloveeclipse
authored andcommitted
Make InnerClassInfo thread-safe
This change adjusts fields in InnerClassInfo to be volatile, if those fields can cause concurrency problems. In particular, the following methods result in issues when ran with Java 25 in a multi-threade environment: * getEnclosingTypeName * getName * getSourceName They follow the pattern of checking whether some value is initialized with a boolean variable, then setting the value if its not initialized yet. When those methods are invoked from multiple threads on the same object, there is no guarantee that the initialized value will be read by all threads. By making both the flag and the value fields are volatile, threads will read the correct value; the value could still be initialized multiple times, but we assume that all threads will be setting the same value. This change additionally makes some fields final, to make the code easier to read and analyze. Fixes: eclipse-jdt#4199
1 parent fa6f194 commit c738041

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/classfmt/ClassFileStruct.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
abstract public class ClassFileStruct {
1717
byte[] reference;
1818
int[] constantPoolOffsets;
19-
int structOffset;
19+
final int structOffset;
2020
public ClassFileStruct(byte[] classFileBytes, int[] offsets, int offset) {
2121
this.reference = classFileBytes;
2222
this.constantPoolOffsets = offsets;

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/classfmt/InnerClassInfo.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
*/
2323

2424
public class InnerClassInfo extends ClassFileStruct implements IBinaryNestedType {
25-
int innerClassNameIndex = -1;
26-
int outerClassNameIndex = -1;
27-
int innerNameIndex = -1;
28-
private char[] innerClassName;
29-
private char[] outerClassName;
30-
private char[] innerName;
31-
private int accessFlags = -1;
32-
private boolean readInnerClassName;
33-
private boolean readOuterClassName;
34-
private boolean readInnerName;
25+
final int innerClassNameIndex;
26+
final int outerClassNameIndex;
27+
final int innerNameIndex;
28+
private volatile char[] innerClassName;
29+
private volatile char[] outerClassName;
30+
private volatile char[] innerName;
31+
private volatile int accessFlags = -1;
32+
private volatile boolean readInnerClassName;
33+
private volatile boolean readOuterClassName;
34+
private volatile boolean readInnerName;
3535

3636
public InnerClassInfo(byte classFileBytes[], int offsets[], int offset) {
3737
super(classFileBytes, offsets, offset);

0 commit comments

Comments
 (0)