From 1b8ca2772eef033edc19257ec43d924faeb397fa Mon Sep 17 00:00:00 2001 From: Oscar Mira Date: Wed, 18 Sep 2024 18:01:37 +0200 Subject: [PATCH] Fix double call to DocumentFile.getName to avoid potential race --- .../securesms/util/BackupUtil.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/BackupUtil.java b/app/src/main/java/org/thoughtcrime/securesms/util/BackupUtil.java index 5d2351ee46..c149258b66 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/BackupUtil.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/BackupUtil.java @@ -155,8 +155,9 @@ private static List getAllBackupsNewestFirstApi29() { List backups = new ArrayList<>(files.length); for (DocumentFile file : files) { - if (file.isFile() && file.getName() != null && file.getName().endsWith(".backup")) { - long backupTimestamp = getBackupTimestamp(file.getName()); + String backupName = file.getName(); + if (file.isFile() && hasBackupExtension(backupName)) { + long backupTimestamp = getBackupTimestamp(backupName); if (backupTimestamp != -1) { backups.add(new BackupInfo(backupTimestamp, file.length(), file.getUri())); @@ -169,6 +170,10 @@ private static List getAllBackupsNewestFirstApi29() { return backups; } + private static boolean hasBackupExtension(String fileName) { + return fileName != null && fileName.endsWith(".backup"); + } + public static @Nullable BackupInfo getBackupInfoFromSingleUri(@NonNull Context context, @NonNull Uri singleUri) throws BackupFileException { DocumentFile documentFile = Objects.requireNonNull(DocumentFile.fromSingleUri(context, singleUri)); @@ -180,7 +185,7 @@ private static List getAllBackupsNewestFirstApi29() { BackupFileState backupFileState = getBackupFileState(documentFile); if (backupFileState.isSuccess()) { - long backupTimestamp = getBackupTimestamp(Objects.requireNonNull(documentFile.getName())); + long backupTimestamp = getBackupTimestamp(documentFile.getName()); return new BackupInfo(backupTimestamp, documentFile.length(), documentFile.getUri()); } else { Log.w(TAG, "Could not load backup info."); @@ -242,8 +247,9 @@ public static boolean hasBackupFiles(@NonNull Context context) { } } - private static long getBackupTimestamp(@NonNull String backupName) { - if (backupName.startsWith(BuildConfig.BACKUP_FILENAME) && + private static long getBackupTimestamp(@Nullable String backupName) { + if (backupName != null && + backupName.startsWith(BuildConfig.BACKUP_FILENAME) && backupName.endsWith(".backup")) { String ts = backupName.substring(BuildConfig.BACKUP_FILENAME.length(), backupName.length() - ".backup".length()); @@ -275,7 +281,7 @@ private static BackupFileState getBackupFileState(@NonNull DocumentFile document return BackupFileState.NOT_FOUND; } else if (!documentFile.canRead()) { return BackupFileState.NOT_READABLE; - } else if (Util.isEmpty(documentFile.getName()) || !documentFile.getName().endsWith(".backup")) { + } else if (!hasBackupExtension(documentFile.getName())) { return BackupFileState.UNSUPPORTED_FILE_EXTENSION; } else { return BackupFileState.READABLE;