Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crash on conversion of invalid data in Image #84782

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/io/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/core/io/test_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading