Skip to content

Commit 9ef24de

Browse files
mohammadmseet-huepixelflinger
authored andcommitted
Fix integer overflow in computeBindingSize for glTF accessors
Add overflow validation to computeBindingSize(), consistent with the existing overflow check already present in decodeMeshoptCompression() in the same file (lines 100-104). The multiplication accessor->stride * (accessor->count - 1) can overflow when both stride and count are large values from a glTF file. The result is truncated to uint32_t, producing an undersized value that is used for buffer allocation in ResourceLoader. Uses postcondition checks as requested in review.
1 parent defba15 commit 9ef24de

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

libs/gltfio/src/Utility.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,20 @@ bool primitiveHasVertexColor(cgltf_primitive* inPrim) {
168168
// private but its implementation file is available in this cpp file.
169169
uint32_t computeBindingSize(cgltf_accessor const* accessor) {
170170
cgltf_size element_size = cgltf_calc_size(accessor->type, accessor->component_type);
171-
return uint32_t(accessor->stride * (accessor->count - 1) + element_size);
171+
// Validate against overflow, consistent with the check in decodeMeshoptCompression above.
172+
FILAMENT_CHECK_POSTCONDITION(accessor->count > 0)
173+
<< "gltfio: accessor count must be > 0";
174+
cgltf_size const countMinus1 = accessor->count - 1;
175+
if (accessor->stride > 0) {
176+
cgltf_size const maxCount = std::numeric_limits<cgltf_size>::max() / accessor->stride;
177+
FILAMENT_CHECK_POSTCONDITION(countMinus1 <= maxCount)
178+
<< "gltfio: binding size overflow (stride=" << accessor->stride
179+
<< ", count=" << accessor->count << ")";
180+
}
181+
cgltf_size const result = accessor->stride * countMinus1 + element_size;
182+
FILAMENT_CHECK_POSTCONDITION(result <= std::numeric_limits<uint32_t>::max())
183+
<< "gltfio: binding size exceeds uint32_t max";
184+
return uint32_t(result);
172185
}
173186

174187
void convertBytesToShorts(uint16_t* dst, uint8_t const* src, size_t count) {

0 commit comments

Comments
 (0)