Skip to content

Commit

Permalink
Fix double call to DocumentFile.getName to avoid potential race
Browse files Browse the repository at this point in the history
  • Loading branch information
valldrac committed Sep 18, 2024
1 parent 046794f commit 1b8ca27
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/src/main/java/org/thoughtcrime/securesms/util/BackupUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ private static List<BackupInfo> getAllBackupsNewestFirstApi29() {
List<BackupInfo> 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()));
Expand All @@ -169,6 +170,10 @@ private static List<BackupInfo> 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));

Expand All @@ -180,7 +185,7 @@ private static List<BackupInfo> 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.");
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1b8ca27

Please sign in to comment.