#853 prevent decoding SVG to large region

This commit is contained in:
Thibault Deckers 2023-12-24 12:01:59 +01:00
parent b010e2f86e
commit eb004d8eca
2 changed files with 14 additions and 2 deletions

View file

@ -32,7 +32,15 @@ class SvgRegionFetcher internal constructor(
) { ) {
if (!MemoryUtils.canAllocate(sizeBytes)) { if (!MemoryUtils.canAllocate(sizeBytes)) {
// opening an SVG that large would yield an OOM during parsing from `com.caverock.androidsvg.SVGParser` // opening an SVG that large would yield an OOM during parsing from `com.caverock.androidsvg.SVGParser`
result.error("fetch-read-large", "SVG too large at $sizeBytes bytes, for uri=$uri regionRect=$regionRect", null) result.error("fetch-read-large-file", "SVG too large at $sizeBytes bytes, for uri=$uri regionRect=$regionRect", null)
return
}
// use `Long` as rect size could be unexpectedly large and go beyond `Int` max
val targetBitmapSizeBytes: Long = ARGB_8888_BYTE_SIZE.toLong() * regionRect.width() * regionRect.height()
if (!MemoryUtils.canAllocate(targetBitmapSizeBytes)) {
// decoding a region that large would yield an OOM when creating the bitmap
result.error("fetch-read-large-region", "SVG region too large for uri=$uri regionRect=$regionRect", null)
return return
} }
@ -106,4 +114,8 @@ class SvgRegionFetcher internal constructor(
val uri: Uri, val uri: Uri,
val svg: SVG, val svg: SVG,
) )
companion object {
const val ARGB_8888_BYTE_SIZE = 4
}
} }

View file

@ -92,7 +92,7 @@ object MimeTypes {
// as of `metadata-extractor` v2.14.0 // as of `metadata-extractor` v2.14.0
fun canReadWithMetadataExtractor(mimeType: String) = when (mimeType) { fun canReadWithMetadataExtractor(mimeType: String) = when (mimeType) {
DJVU, WBMP -> false DJVU, SVG, WBMP -> false
MKV, MP2T, MP2TS, OGV, WEBM -> false MKV, MP2T, MP2TS, OGV, WEBM -> false
else -> true else -> true
} }