diff --git a/native/graphics/jni/imagedecoder.cpp b/native/graphics/jni/imagedecoder.cpp index 2e4d2140809ed..d1946b085df4c 100644 --- a/native/graphics/jni/imagedecoder.cpp +++ b/native/graphics/jni/imagedecoder.cpp @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -70,6 +71,14 @@ static int createFromStream(std::unique_ptr stream, AImageDe return ResultToErrorCode(result); } + // AImageDecoderHeaderInfo_getWidth/Height return an int32_t. Ensure that + // the conversion is safe. + const auto& info = androidCodec->getInfo(); + if (info.width() > std::numeric_limits::max() + || info.height() > std::numeric_limits::max()) { + return ANDROID_IMAGE_DECODER_INVALID_INPUT; + } + *outDecoder = reinterpret_cast(new ImageDecoder(std::move(androidCodec))); return ANDROID_IMAGE_DECODER_SUCCESS; }