Fix bug with APNGParser over reading larger files and invalidating the stream.

This commit is contained in:
Cody Henthorne
2021-06-08 16:46:01 -04:00
committed by Greyson Parrelli
parent 2ba206b9db
commit 958331a8ea
2 changed files with 31 additions and 1 deletions

View File

@@ -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;
}

View 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)
}
}