Skip to content

Commit ab25eb5

Browse files
jreckAndroid Build Coastguard Worker
authored andcommitted
Fix a potential thread safety issue in VectorDrawable
Bug: 158839504 Bug: 185178568 Test: speculative Change-Id: Id9f229f08fe5897dda25441fbaa15c98f8130de9 (cherry picked from commit 304f3af54526f3d80cc037e18f4cf89f1053737c)
1 parent 1f5c49f commit ab25eb5

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

graphics/java/android/graphics/drawable/VectorDrawable.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,19 @@ public class VectorDrawable extends Drawable {
348348
private final Rect mTmpBounds = new Rect();
349349

350350
public VectorDrawable() {
351-
this(new VectorDrawableState(null), null);
351+
this(null, null);
352352
}
353353

354354
/**
355355
* The one constructor to rule them all. This is called by all public
356356
* constructors to set the state and initialize local properties.
357357
*/
358-
private VectorDrawable(@NonNull VectorDrawableState state, @Nullable Resources res) {
359-
mVectorState = state;
358+
private VectorDrawable(@Nullable VectorDrawableState state, @Nullable Resources res) {
359+
// As the mutable, not-thread-safe native instance is stored in VectorDrawableState, we
360+
// need to always do a defensive copy even if mutate() isn't called. Otherwise
361+
// draw() being called on 2 different VectorDrawable instances could still hit the same
362+
// underlying native object.
363+
mVectorState = new VectorDrawableState(state);
360364
updateLocalState(res);
361365
}
362366

0 commit comments

Comments
 (0)