summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2023-12-12 00:17:42 +0100
committerRémi Verschelde <rverschelde@gmail.com>2023-12-12 00:17:42 +0100
commitb952b007b14dfefeebac8a49da233f421f8aebb8 (patch)
tree243706bb4eda040363cdccd4397e7dfb79111c47
parentae7c533937c3e20186fea7d2e52014b8c087a10d (diff)
parentdec2269bca8c616dac73ec987f7e670cf0451c77 (diff)
downloadredot-engine-b952b007b14dfefeebac8a49da233f421f8aebb8.tar.gz
Merge pull request #84782 from rsburke4/image-fix
Prevent crash on conversion of invalid data in `Image`
-rw-r--r--core/io/image.cpp1
-rw-r--r--tests/core/io/test_image.h23
2 files changed, 24 insertions, 0 deletions
diff --git a/core/io/image.cpp b/core/io/image.cpp
index ce08b417a8..c72064e4f7 100644
--- a/core/io/image.cpp
+++ b/core/io/image.cpp
@@ -509,6 +509,7 @@ static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p
}
void Image::convert(Format p_new_format) {
+ ERR_FAIL_INDEX_MSG(p_new_format, FORMAT_MAX, "The Image format specified (" + itos(p_new_format) + ") is out of range. See Image's Format enum.");
if (data.size() == 0) {
return;
}
diff --git a/tests/core/io/test_image.h b/tests/core/io/test_image.h
index 07c7c04e36..945a7e1ba3 100644
--- a/tests/core/io/test_image.h
+++ b/tests/core/io/test_image.h
@@ -403,6 +403,29 @@ TEST_CASE("[Image] Custom mipmaps") {
}
}
+TEST_CASE("[Image] Convert image") {
+ for (int format = Image::FORMAT_RF; format < Image::FORMAT_RGBE9995; format++) {
+ for (int new_format = Image::FORMAT_RF; new_format < Image::FORMAT_RGBE9995; new_format++) {
+ Ref<Image> image = memnew(Image(4, 4, false, (Image::Format)format));
+ image->convert((Image::Format)new_format);
+ String format_string = Image::format_names[(Image::Format)format];
+ String new_format_string = Image::format_names[(Image::Format)new_format];
+ format_string = "Error converting from " + format_string + " to " + new_format_string + ".";
+ CHECK_MESSAGE(image->get_format() == new_format, format_string);
+ }
+ }
+
+ Ref<Image> image = memnew(Image(4, 4, false, Image::FORMAT_RGBA8));
+ PackedByteArray image_data = image->get_data();
+ image->convert((Image::Format)-1);
+ CHECK_MESSAGE(image->get_data() == image_data, "Image conversion to invalid type (-1) should not alter image.");
+
+ Ref<Image> image2 = memnew(Image(4, 4, false, Image::FORMAT_RGBA8));
+ image_data = image2->get_data();
+ image2->convert((Image::Format)(Image::FORMAT_MAX + 1));
+ CHECK_MESSAGE(image2->get_data() == image_data, "Image conversion to invalid type (Image::FORMAT_MAX + 1) should not alter image.");
+}
+
} // namespace TestImage
#endif // TEST_IMAGE_H