Skip to content

Commit 25497b0

Browse files
committed
Add bounds check. Fixes #197
1 parent d5afe84 commit 25497b0

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

tinyexr.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5752,7 +5752,7 @@ static bool isValidTile(const EXRHeader* exr_header,
57525752

57535753
static bool ReconstructTileOffsets(OffsetData& offset_data,
57545754
const EXRHeader* exr_header,
5755-
const unsigned char* head, const unsigned char* marker, const size_t /*size*/,
5755+
const unsigned char* head, const unsigned char* marker, const size_t size,
57565756
bool isMultiPartFile,
57575757
bool isDeep) {
57585758
int numXLevels = offset_data.num_x_levels;
@@ -5761,11 +5761,20 @@ static bool ReconstructTileOffsets(OffsetData& offset_data,
57615761
for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) {
57625762
tinyexr::tinyexr_uint64 tileOffset = tinyexr::tinyexr_uint64(marker - head);
57635763

5764+
57645765
if (isMultiPartFile) {
5766+
if ((marker + sizeof(int)) >= (head + size)) {
5767+
return false;
5768+
}
5769+
57655770
//int partNumber;
57665771
marker += sizeof(int);
57675772
}
57685773

5774+
if ((marker + 4 * sizeof(int)) >= (head + size)) {
5775+
return false;
5776+
}
5777+
57695778
int tileX;
57705779
memcpy(&tileX, marker, sizeof(int));
57715780
tinyexr::swap4(&tileX);
@@ -5787,6 +5796,9 @@ static bool ReconstructTileOffsets(OffsetData& offset_data,
57875796
marker += sizeof(int);
57885797

57895798
if (isDeep) {
5799+
if ((marker + 2 * sizeof(tinyexr::tinyexr_int64)) >= (head + size)) {
5800+
return false;
5801+
}
57905802
tinyexr::tinyexr_int64 packed_offset_table_size;
57915803
memcpy(&packed_offset_table_size, marker, sizeof(tinyexr::tinyexr_int64));
57925804
tinyexr::swap8(reinterpret_cast<tinyexr::tinyexr_uint64*>(&packed_offset_table_size));
@@ -5800,8 +5812,16 @@ static bool ReconstructTileOffsets(OffsetData& offset_data,
58005812
// next Int64 is unpacked sample size - skip that too
58015813
marker += packed_offset_table_size + packed_sample_size + 8;
58025814

5815+
if (marker >= (head + size)) {
5816+
return false;
5817+
}
5818+
58035819
} else {
58045820

5821+
if ((marker + sizeof(int)) >= (head + size)) {
5822+
return false;
5823+
}
5824+
58055825
int dataSize;
58065826
memcpy(&dataSize, marker, sizeof(int));
58075827
tinyexr::swap4(&dataSize);
@@ -5818,6 +5838,19 @@ static bool ReconstructTileOffsets(OffsetData& offset_data,
58185838
if (level_idx < 0) {
58195839
return false;
58205840
}
5841+
5842+
if (size_t(level_idx) >= offset_data.offsets.size()) {
5843+
return false;
5844+
}
5845+
5846+
if (size_t(tileY) >= offset_data.offsets[size_t(level_idx)].size()) {
5847+
return false;
5848+
}
5849+
5850+
if (size_t(tileX) >= offset_data.offsets[size_t(level_idx)][size_t(tileY)].size()) {
5851+
return false;
5852+
}
5853+
58215854
offset_data.offsets[size_t(level_idx)][size_t(tileY)][size_t(tileX)] = tileOffset;
58225855
}
58235856
}

0 commit comments

Comments
 (0)