Skip to content

Commit

Permalink
Add buffer size check to Image.load_tga_from_buffer(). Fixes godoteng…
Browse files Browse the repository at this point in the history
…ine#67985

(cherry picked from commit 5cb0748)
  • Loading branch information
Klowner authored and Riordan-DC committed Jan 23, 2023
1 parent 7157e95 commit 9960f78
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion modules/tga/image_loader_tga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,21 @@ Error ImageLoaderTGA::load_image(Ref<Image> p_image, FileAccess *f, bool p_force
err = FAILED;
}

uint64_t color_map_size;
if (has_color_map) {
if (tga_header.color_map_length > 256 || (tga_header.color_map_depth != 24) || tga_header.color_map_type != 1) {
err = FAILED;
}
color_map_size = tga_header.color_map_length * (tga_header.color_map_depth >> 3);
} else {
if (tga_header.color_map_type) {
err = FAILED;
}
color_map_size = 0;
}

if ((src_image_len - f->get_position()) < (tga_header.id_length + color_map_size)) {
err = FAILED; // TGA data appears to be truncated (fewer bytes than expected).
}

if (tga_header.image_width <= 0 || tga_header.image_height <= 0) {
Expand All @@ -289,7 +296,6 @@ Error ImageLoaderTGA::load_image(Ref<Image> p_image, FileAccess *f, bool p_force
PoolVector<uint8_t> palette;

if (has_color_map) {
size_t color_map_size = tga_header.color_map_length * (tga_header.color_map_depth >> 3);
err = palette.resize(color_map_size);
if (err == OK) {
PoolVector<uint8_t>::Write palette_w = palette.write();
Expand Down

0 comments on commit 9960f78

Please sign in to comment.