From 00dcec8a25e90790404bd69bf1489055143a2b37 Mon Sep 17 00:00:00 2001 From: baldurk Date: Mon, 5 Jun 2023 16:21:19 +0100 Subject: [PATCH] Catch and try to more gracefully handle invalid DDS files --- renderdoc/common/dds_readwrite.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/renderdoc/common/dds_readwrite.cpp b/renderdoc/common/dds_readwrite.cpp index 006b467a3..996196243 100644 --- a/renderdoc/common/dds_readwrite.cpp +++ b/renderdoc/common/dds_readwrite.cpp @@ -1215,12 +1215,20 @@ RDResult load_dds_from_file(StreamReader *reader, read_dds_data &ret) } } - if(uint64_t(ret.slices) > fileSize || uint64_t(ret.mips) > fileSize || - uint64_t(ret.slices) * ret.mips > fileSize) + // catch any invalid dimensions here, including the total dimension with a very conservative 1/16 + // byte per pixel + if(uint64_t(ret.width) > fileSize || uint64_t(ret.height) > fileSize || + uint64_t(ret.depth) > fileSize || uint64_t(ret.slices) > fileSize || + uint64_t(ret.mips) > fileSize || uint64_t(ret.slices) * ret.mips > fileSize || + (uint64_t(ret.width) * uint64_t(ret.height) * uint64_t(ret.depth) * + RDCMAX(uint64_t(ret.slices), uint64_t(ret.depth))) / + 16 > + fileSize) { - RETURN_ERROR_RESULT(ResultCode::ImageUnsupported, - "Invalid slice count %u or mip count %u loaded from DDS of size %llu", - ret.slices, ret.mips, fileSize); + RETURN_ERROR_RESULT( + ResultCode::ImageUnsupported, + "Invalid dimension (%ux%ux%u) slice count %u or mip count %u loaded from DDS of size %llu", + ret.width, ret.height, ret.depth, ret.slices, ret.mips, fileSize); } // we reserve space for a full mip-chain (twice the size of the top mip) just to be conservative