mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-20 08:39:22 +01:00
Fix bug with APNGParser over reading larger files and invalidating the stream.
This commit is contained in:
committed by
Greyson Parrelli
parent
2ba206b9db
commit
958331a8ea
@@ -18,6 +18,9 @@ import java.nio.ByteBuffer;
|
||||
|
||||
public class ApngStreamCacheDecoder implements ResourceDecoder<InputStream, APNGDecoder> {
|
||||
|
||||
/** Set to match {@link com.bumptech.glide.load.data.InputStreamRewinder}'s read limit */
|
||||
private static final int READ_LIMIT = 5 * 1024 * 1024;
|
||||
|
||||
private final ResourceDecoder<ByteBuffer, APNGDecoder> byteBufferDecoder;
|
||||
|
||||
public ApngStreamCacheDecoder(ResourceDecoder<ByteBuffer, APNGDecoder> byteBufferDecoder) {
|
||||
@@ -27,7 +30,7 @@ public class ApngStreamCacheDecoder implements ResourceDecoder<InputStream, APNG
|
||||
@Override
|
||||
public boolean handles(@NonNull InputStream source, @NonNull Options options) {
|
||||
if (options.get(ApngOptions.ANIMATE)) {
|
||||
return APNGParser.isAPNG(new StreamReader(source));
|
||||
return APNGParser.isAPNG(new LimitedReader(new StreamReader(source), READ_LIMIT));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
27
app/src/main/java/org/thoughtcrime/securesms/glide/cache/LimitedReader.kt
vendored
Normal file
27
app/src/main/java/org/thoughtcrime/securesms/glide/cache/LimitedReader.kt
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
package org.thoughtcrime.securesms.glide.cache
|
||||
|
||||
import org.signal.glide.common.io.Reader
|
||||
import java.io.IOException
|
||||
import kotlin.jvm.Throws
|
||||
|
||||
/**
|
||||
* Restrict the number of bytes that can be read to prevent breaking the input stream in Glide's
|
||||
* eyes.
|
||||
*/
|
||||
class LimitedReader(private val reader: Reader, private val readLimit: Int) : Reader by reader {
|
||||
@Throws(IOException::class)
|
||||
override fun read(buffer: ByteArray?, start: Int, byteCount: Int): Int {
|
||||
if (position() + byteCount >= readLimit) {
|
||||
throw IOException("Read limit exceeded")
|
||||
}
|
||||
return reader.read(buffer, start, byteCount)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun skip(total: Long): Long {
|
||||
if (position() + total >= readLimit) {
|
||||
throw IOException("Read limit exceeded")
|
||||
}
|
||||
return reader.skip(total)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user