Skip to content

[rmodels] Ensure that GLTF buffers are deallocated before using them #4998

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
44 changes: 42 additions & 2 deletions src/rmodels.c
Original file line number Diff line number Diff line change
Expand Up @@ -5532,6 +5532,10 @@ static Model LoadGLTF(const char *fileName)

// WARNING: SPECS: POSITION accessor MUST have its min and max properties defined

// make sure we don't leak this buffer if there are multiple attributes of the same type
if (model.meshes[meshIndex].vertices)
MemFree(model.meshes[meshIndex].vertices);

if ((attribute->type == cgltf_type_vec3) && (attribute->component_type == cgltf_component_type_r_32f))
{
// Init raylib mesh vertices to copy glTF attribute data
Expand All @@ -5557,6 +5561,10 @@ static Model LoadGLTF(const char *fileName)
{
cgltf_accessor *attribute = mesh->primitives[p].attributes[j].data;

// make sure we don't leak this buffer if there are multiple attributes of the same type
if (model.meshes[meshIndex].normals)
MemFree(model.meshes[meshIndex].normals);

if ((attribute->type == cgltf_type_vec3) && (attribute->component_type == cgltf_component_type_r_32f))
{
// Init raylib mesh normals to copy glTF attribute data
Expand All @@ -5581,6 +5589,10 @@ static Model LoadGLTF(const char *fileName)
{
cgltf_accessor *attribute = mesh->primitives[p].attributes[j].data;

// make sure we don't leak this buffer if there are multiple attributes of the same type
if (model.meshes[meshIndex].tangents)
MemFree(model.meshes[meshIndex].tangents);

if ((attribute->type == cgltf_type_vec4) && (attribute->component_type == cgltf_component_type_r_32f))
{
// Init raylib mesh tangent to copy glTF attribute data
Expand Down Expand Up @@ -5651,8 +5663,21 @@ static Model LoadGLTF(const char *fileName)
else TRACELOG(LOG_WARNING, "MODEL: [%s] Texcoords attribute data format not supported, use vec2 float", fileName);

int index = mesh->primitives[p].attributes[j].index;
if (index == 0) model.meshes[meshIndex].texcoords = texcoordPtr;
else if (index == 1) model.meshes[meshIndex].texcoords2 = texcoordPtr;
if (index == 0)
{
// make sure we don't leak this buffer if there are multiple attributes of the same type
if (model.meshes[meshIndex].texcoords)
MemFree(model.meshes[meshIndex].texcoords);

model.meshes[meshIndex].texcoords = texcoordPtr;
}
else if (index == 1)
{
// make sure we don't leak this buffer if there are multiple attributes of the same type
if (model.meshes[meshIndex].texcoords2)
MemFree(model.meshes[meshIndex].texcoords2);
model.meshes[meshIndex].texcoords2 = texcoordPtr;
}
else
{
TRACELOG(LOG_WARNING, "MODEL: [%s] No more than 2 texture coordinates attributes supported", fileName);
Expand All @@ -5664,6 +5689,9 @@ static Model LoadGLTF(const char *fileName)
cgltf_accessor *attribute = mesh->primitives[p].attributes[j].data;

// WARNING: SPECS: All components of each COLOR_n accessor element MUST be clamped to [0.0, 1.0] range
// make sure we don't leak this buffer if there are multiple attributes of the same type
if (model.meshes[meshIndex].colors)
MemFree(model.meshes[meshIndex].colors);

if (attribute->type == cgltf_type_vec3) // RGB
{
Expand Down Expand Up @@ -5782,6 +5810,10 @@ static Model LoadGLTF(const char *fileName)

model.meshes[meshIndex].triangleCount = (int)attribute->count/3;

// make sure we don't leak this buffer if there are multiple attributes of the same type
if (model.meshes[meshIndex].indices)
MemFree(model.meshes[meshIndex].indices);

if (attribute->component_type == cgltf_component_type_r_16u)
{
// Init raylib mesh indices to copy glTF attribute data
Expand Down Expand Up @@ -5896,6 +5928,10 @@ static Model LoadGLTF(const char *fileName)
// if data is provided in any other format, it is converted to supported format but
// it could imply data loss (a warning message is issued in that case)

// make sure we don't leak this buffer if there are multiple attributes of the same type
if (model.meshes[meshIndex].boneIds)
MemFree(model.meshes[meshIndex].boneIds);

if (attribute->type == cgltf_type_vec4)
{
if (attribute->component_type == cgltf_component_type_r_8u)
Expand Down Expand Up @@ -5939,6 +5975,10 @@ static Model LoadGLTF(const char *fileName)
{
cgltf_accessor *attribute = mesh->primitives[p].attributes[j].data;

// make sure we don't leak this buffer if there are multiple attributes of the same type
if (model.meshes[meshIndex].boneWeights)
MemFree(model.meshes[meshIndex].boneWeights);

if (attribute->type == cgltf_type_vec4)
{
if (attribute->component_type == cgltf_component_type_r_8u)
Expand Down
Loading