From 3b16a1d28c1c5a66f44b4f648d316d2bf2e4092e Mon Sep 17 00:00:00 2001 From: Cody Henthorne Date: Mon, 16 May 2022 11:32:23 -0400 Subject: [PATCH] Fix NPE in PartProvider. --- .../securesms/providers/PartProvider.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/providers/PartProvider.java b/app/src/main/java/org/thoughtcrime/securesms/providers/PartProvider.java index 86c043e26b..3bbe180bce 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/providers/PartProvider.java +++ b/app/src/main/java/org/thoughtcrime/securesms/providers/PartProvider.java @@ -102,7 +102,7 @@ public final class PartProvider extends BaseContentProvider { public String getType(@NonNull Uri uri) { Log.i(TAG, "getType() called: " + uri); - if (uriMatcher.match(uri) == SINGLE_ROW) { + if (uriMatcher.match(uri) == SINGLE_ROW && SignalDatabase.getInstance() != null) { PartUriParser partUriParser = new PartUriParser(uri); DatabaseAttachment attachment = SignalDatabase.attachments().getAttachment(partUriParser.getPartId()); @@ -157,9 +157,20 @@ public final class PartProvider extends BaseContentProvider { ParcelFileDescriptor[] reliablePipe = ParcelFileDescriptor.createReliablePipe(); SignalExecutors.BOUNDED_IO.execute(() -> { + SignalDatabase signalDatabase = SignalDatabase.getInstance(); + if (signalDatabase == null) { + Log.w(TAG, "Database is not available"); + try { + reliablePipe[1].closeWithError("Unable to access database"); + } catch (IOException e) { + Log.w(TAG, "Unable to close pipe after no database", e); + } + return; + } + Throwable error = null; try (OutputStream out = new FileOutputStream(reliablePipe[1].getFileDescriptor())) { - try (InputStream in = SignalDatabase.attachments().getAttachmentStream(attachmentId, 0)) { + try (InputStream in = signalDatabase.getAttachments().getAttachmentStream(attachmentId, 0)) { StreamUtil.copy(in, out); } catch (IOException e) { Log.w(TAG, "Error providing file", e);