Godot Engine – Multi-platform 2D and 3D game engine
Fix out-of-bounds write when loading corrupt HDR images
Rémi Verscheldedd274579a6f5
diff --git a/modules/hdr/image_loader_hdr.cpp b/modules/hdr/image_loader_hdr.cppindex 1b69fe5302d..f9c97dbc832 100644--- a/modules/hdr/image_loader_hdr.cpp+++ b/modules/hdr/image_loader_hdr.cpp@@ -60,6 +60,10 @@ Error ImageLoaderHDR::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitFieldint width = f->get_line().to_int();+ ERR_FAIL_COND_V_MSG(width <= 0 || height <= 0, ERR_FILE_CORRUPT, "Invalid image size, corrupt HDR.");+ ERR_FAIL_COND_V_MSG(width > Image::MAX_WIDTH || height > Image::MAX_HEIGHT || (int64_t)width * height > Image::MAX_PIXELS, ERR_UNAVAILABLE,+ vformat("The HDR image size (%dx%d) exceeds the maximum image size.", width, height));+Vector<uint8_t> imgdata;imgdata.resize(height * width * (int)sizeof(uint32_t));@@ -107,11 +111,13 @@ Error ImageLoaderHDR::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField// Runint value = f->get_8();count -= 128;+ ERR_FAIL_COND_V_MSG(count > width - i, ERR_FILE_CORRUPT, "Invalid run length, corrupt HDR.");for (int z = 0; z < count; ++z) {ptr[(j * width + i++) * 4 + k] = uint8_t(value);}} else {// Dump+ ERR_FAIL_COND_V_MSG(count == 0 || count > width - i, ERR_FILE_CORRUPT, "Invalid dump length, corrupt HDR.");f->get_buffer(temp_read_ptr, count);for (int z = 0; z < count; ++z) {ptr[(j * width + i++) * 4 + k] = temp_read_ptr[z];