This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push: new ce39ac33 Spotbugs LI_LAZY_INIT_UPDATE_STATIC ce39ac33 is described below commit ce39ac33e69b73fb806e518de1a4b2e588a0737e Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat Oct 28 09:05:20 2023 -0400 Spotbugs LI_LAZY_INIT_UPDATE_STATIC LI: Incorrect lazy initialization and update of static field (LI_LAZY_INIT_UPDATE_STATIC) This method contains an unsynchronized lazy initialization of a static field. After the field is set, the object stored into that location is further updated or accessed. The setting of the field is visible to other threads as soon as it is set. If the futher accesses in the method that set the field serve to initialize the object, then you have a very serious multithreading bug, unless something else prevents any other thread from accessing the stored object until it is fully initialized. Even if you feel confident that the method is never called by multiple threads, it might be better to not set the static field until the value you are setting it to is fully populated/initialized. --- src/changes/changes.xml | 1 + .../commons/compress/harmony/pack200/CodecEncoding.java | 17 ++++++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 359e0a5f..d805b925 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -71,6 +71,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory" issue="COMPRESS-649">Improve performance in BlockLZ4CompressorOutputStream.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Null-guard Lister.main(String[]) for programmatic invocation.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">NPE in pack200.NewAttributeBands.Reference.addAttributeToBand(NewAttribute, InputStream).</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">Incorrect lazy initialization and update of static field in pack200.CodecEncoding.getSpecifier(Codec, Codec).</action> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Dependabot">Bump org.slf4j:slf4j-api from 2.0.8 to 2.0.9 #413.</action> <action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-io:commons-io from 2.13.0 to 2.15.0.</action> diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/CodecEncoding.java b/src/main/java/org/apache/commons/compress/harmony/pack200/CodecEncoding.java index 832d8a93..5d2efeb0 100644 --- a/src/main/java/org/apache/commons/compress/harmony/pack200/CodecEncoding.java +++ b/src/main/java/org/apache/commons/compress/harmony/pack200/CodecEncoding.java @@ -66,6 +66,14 @@ public class CodecEncoding { new BHSDCodec(4, 240, 1, 1), new BHSDCodec(4, 248, 0, 1), new BHSDCodec(4, 248, 1, 1)}; private static Map<BHSDCodec, Integer> canonicalCodecsToSpecifiers; + + static { + final HashMap<BHSDCodec, Integer> reverseMap = new HashMap<>(canonicalCodec.length); + for (int i = 0; i < canonicalCodec.length; i++) { + reverseMap.put(canonicalCodec[i], Integer.valueOf(i)); + } + canonicalCodecsToSpecifiers = reverseMap; + } public static BHSDCodec getCanonicalCodec(final int i) { return canonicalCodec[i]; @@ -178,15 +186,6 @@ public class CodecEncoding { } public static int[] getSpecifier(final Codec codec, final Codec defaultForBand) { - // lazy initialization - if (canonicalCodecsToSpecifiers == null) { - final HashMap<BHSDCodec, Integer> reverseMap = new HashMap<>(canonicalCodec.length); - for (int i = 0; i < canonicalCodec.length; i++) { - reverseMap.put(canonicalCodec[i], Integer.valueOf(i)); - } - canonicalCodecsToSpecifiers = reverseMap; - } - if (canonicalCodecsToSpecifiers.containsKey(codec)) { return new int[] { canonicalCodecsToSpecifiers.get(codec).intValue() }; }