diff options
author | Shigeru Chiba <chibash@users.noreply.github.com> | 2021-04-26 02:27:10 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-26 02:27:10 +0900 |
commit | b4cb5cbf3abf0e35bfba05569c30c4d4dc6c2695 (patch) | |
tree | cc95095ec1a3e0e78eb8799835e15d001085141a /src | |
parent | 6786fd5fb71ae989b882a6fe400669663185b81f (diff) | |
parent | 64e15350cfdb7aa42a627e374d5c5de880fc9bed (diff) | |
download | javassist-b4cb5cbf3abf0e35bfba05569c30c4d4dc6c2695.tar.gz javassist-b4cb5cbf3abf0e35bfba05569c30c4d4dc6c2695.zip |
Merge pull request #363 from michalkurka/master
Fix a race condition in CtClassType#getClassFile3
Diffstat (limited to 'src')
-rw-r--r-- | src/main/javassist/CtClassType.java | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index ae196c4a..959ec302 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -179,6 +179,7 @@ class CtClassType extends CtClass { } public ClassFile getClassFile3(boolean doCompress) { + // quick path - no locking ClassFile cfile = classfile; if (cfile != null) return cfile; @@ -186,17 +187,29 @@ class CtClassType extends CtClass { if (doCompress) classPool.compress(); - if (rawClassfile != null) { + byte[] rcfile; + synchronized (this) { + // repeat under lock to make sure we get a consistent result (classfile might have been set by another thread) + cfile = classfile; + if (cfile != null) + return cfile; + + rcfile = rawClassfile; + } + + if (rcfile != null) { + final ClassFile cf; try { - ClassFile cf = new ClassFile(new DataInputStream( - new ByteArrayInputStream(rawClassfile))); - rawClassfile = null; - getCount = GET_THRESHOLD; - return setClassFile(cf); + cf = new ClassFile(new DataInputStream(new ByteArrayInputStream(rcfile))); } catch (IOException e) { throw new RuntimeException(e.toString(), e); } + getCount = GET_THRESHOLD; + synchronized (this) { + rawClassfile = null; + return setClassFile(cf); + } } InputStream fin = null; |