Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LottieCompositionFactory: Avoid NPE when animation contains a Font Family and Context is null. #2530

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ public static LottieResult<LottieComposition> fromZipStreamSync(@Nullable Contex
}

@WorkerThread
private static LottieResult<LottieComposition> fromZipStreamSyncInternal(Context context, ZipInputStream inputStream, @Nullable String cacheKey) {
private static LottieResult<LottieComposition> fromZipStreamSyncInternal(@Nullable Context context, ZipInputStream inputStream, @Nullable String cacheKey) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern here is that a lot of animations are loaded from the server and this may lead to runtime crashes where the engineer doesn't pass context and then somebody else at some point in the future sends an animation that has fonts and then it leads to crashes. It's a tough situation to recover from because they can fix it but won't be able to add the animation because it'll continue to crash older apps.

Given that this can be an application context, what is the use case in which you can't get access to at least the application context?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This crash would also happen right now with the current code that we have since the line after:

File tempFile = new File(context.getCacheDir(), fileName);

would just crash with a NPE. I just saw this and wanted to improve the behavior by at least telling you why it didn't succeed. Do you want me to throw instead of returning a LottieResult?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. IIRC, the intention here was that context is @NonNull, There is an overloaded version that omits the context parameter and clearly states in the docs that fonts will not be loaded.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clearly states in the docs that fonts will not be loaded.

This isn't the case though as it will crash.

LottieComposition composition = null;
Map<String, Bitmap> images = new HashMap<>();
Map<String, Typeface> fonts = new HashMap<>();
Expand Down Expand Up @@ -566,6 +566,11 @@ private static LottieResult<LottieComposition> fromZipStreamSyncInternal(Context
String[] splitName = entryName.split("/");
String fileName = splitName[splitName.length - 1];
String fontFamily = fileName.split("\\.")[0];

if (context == null) {
return new LottieResult<>(new IllegalStateException("Unable to extract font " + fontFamily + " please pass a non-null Context parameter"));
}

File tempFile = new File(context.getCacheDir(), fileName);
FileOutputStream fos = new FileOutputStream(tempFile);
try {
Expand Down
Loading