From 0a0ab8d5e7a3e7fd3894dcf4e736b5007d5b3174 Mon Sep 17 00:00:00 2001 From: olly Date: Tue, 27 Aug 2019 18:05:41 +0100 Subject: [PATCH] Avoid infinite recursion if cache file modified underneath cache This generalizes our "does file still exist" check to also check that the file is the expected length. If it's not, we don't trust it. This avoids infinite recursion in CacheDataSource if a cache file is truncated underneath the cache. Issue: #6165 PiperOrigin-RevId: 265707928 --- .../exoplayer2/upstream/cache/SimpleCache.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java index 81212b731f..e618fcad75 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java @@ -697,9 +697,9 @@ public final class SimpleCache implements Cache { } while (true) { SimpleCacheSpan span = cachedContent.getSpan(position); - if (span.isCached && !span.file.exists()) { - // The file has been deleted from under us. It's likely that other files will have been - // deleted too, so scan the whole in-memory representation. + if (span.isCached && span.file.length() != span.length) { + // The file has been modified or deleted underneath us. It's likely that other files will + // have been modified too, so scan the whole in-memory representation. removeStaleSpans(); continue; } @@ -739,14 +739,14 @@ public final class SimpleCache implements Cache { } /** - * Scans all of the cached spans in the in-memory representation, removing any for which files no - * longer exist. + * Scans all of the cached spans in the in-memory representation, removing any for which the + * underlying file lengths no longer match. */ private void removeStaleSpans() { ArrayList spansToBeRemoved = new ArrayList<>(); for (CachedContent cachedContent : contentIndex.getAll()) { for (CacheSpan span : cachedContent.getSpans()) { - if (!span.file.exists()) { + if (span.file.length() != span.length) { spansToBeRemoved.add(span); } }