Skip to content

Commit

Permalink
Fix a potential NPE race conditions (#1959)
Browse files Browse the repository at this point in the history
This PR addresses the issue #1958 , although the crash is pretty hard to reproducible, the enhancement will definitely make it more robust.

The issue was in com.airbnb.lottie.LottieDrawable#buildCompositionLayer method.
The related code is:

  private void buildCompositionLayer() {
    compositionLayer = new CompositionLayer(
        this, LayerParser.parse(composition), composition.getLayers(), composition);
    if (outlineMasksAndMattes) {
      compositionLayer.setOutlineMasksAndMattes(true);
    }
  }
The crash exception stack prints:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.airbnb.lottie.LottieComposition.getLayers()' on a null object reference
       at com.airbnb.lottie.LottieDrawable.buildCompositionLayer(LottieDrawable.java:298)
       ...
As we can see, the exception occurs at composition.getLayers(), but before executing this statement, the LayerParser.parse(composition) statement also refers to the composition object, which was fine, but after that this object had been set to null, thus caused NPE.

I think this issue is very similar to the issues resolved in PR #1917
  • Loading branch information
chinalwb committed Dec 13, 2021
1 parent 3b5387a commit f38d0df
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ public boolean isApplyingOpacityToLayersEnabled() {
}

private void buildCompositionLayer() {
LottieComposition composition = this.composition;
if (composition == null) {
return;
}
compositionLayer = new CompositionLayer(
this, LayerParser.parse(composition), composition.getLayers(), composition);
if (outlineMasksAndMattes) {
Expand Down

0 comments on commit f38d0df

Please sign in to comment.