Skip to content

Commit

Permalink
Merge pull request #240 from zeux/fix-glb-overflow
Browse files Browse the repository at this point in the history
Fix integer overflow during GLB chunk length validation
  • Loading branch information
jkuhlmann authored Dec 6, 2023
2 parents ab348cb + 35e0cea commit 8731d31
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cgltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ cgltf_result cgltf_parse(const cgltf_options* options, const void* data, cgltf_s
// JSON chunk: length
uint32_t json_length;
memcpy(&json_length, json_chunk, 4);
if (GlbHeaderSize + GlbChunkHeaderSize + json_length > size)
if (json_length > size - GlbHeaderSize - GlbChunkHeaderSize)
{
return cgltf_result_data_too_short;
}
Expand All @@ -1164,15 +1164,15 @@ cgltf_result cgltf_parse(const cgltf_options* options, const void* data, cgltf_s
const void* bin = NULL;
cgltf_size bin_size = 0;

if (GlbHeaderSize + GlbChunkHeaderSize + json_length + GlbChunkHeaderSize <= size)
if (GlbChunkHeaderSize <= size - GlbHeaderSize - GlbChunkHeaderSize - json_length)
{
// We can read another chunk
const uint8_t* bin_chunk = json_chunk + json_length;

// Bin chunk: length
uint32_t bin_length;
memcpy(&bin_length, bin_chunk, 4);
if (GlbHeaderSize + GlbChunkHeaderSize + json_length + GlbChunkHeaderSize + bin_length > size)
if (bin_length > size - GlbHeaderSize - GlbChunkHeaderSize - json_length - GlbChunkHeaderSize)
{
return cgltf_result_data_too_short;
}
Expand Down

0 comments on commit 8731d31

Please sign in to comment.