Skip to content

Commit 3c86b50

Browse files
authored
Merge pull request #117 from zeux/gltf-errors
Fix error handling for memory allocations
2 parents b7be31e + fddd481 commit 3c86b50

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

cgltf.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,13 +2384,21 @@ static int cgltf_parse_json_unprocessed_extension(cgltf_options* options, jsmnto
23842384

23852385
cgltf_size name_length = tokens[i].end - tokens[i].start;
23862386
out_extension->name = (char*)options->memory.alloc(options->memory.user_data, name_length + 1);
2387+
if (!out_extension->name)
2388+
{
2389+
return CGLTF_ERROR_NOMEM;
2390+
}
23872391
strncpy(out_extension->name, (const char*)json_chunk + tokens[i].start, name_length);
23882392
out_extension->name[name_length] = 0;
23892393
i++;
23902394

23912395
size_t start = tokens[i].start;
23922396
size_t size = tokens[i].end - start;
23932397
out_extension->data = (char*)options->memory.alloc(options->memory.user_data, size + 1);
2398+
if (!out_extension->data)
2399+
{
2400+
return CGLTF_ERROR_NOMEM;
2401+
}
23942402
strncpy(out_extension->data, (const char*)json_chunk + start, size);
23952403
out_extension->data[size] = '\0';
23962404

@@ -2413,6 +2421,11 @@ static int cgltf_parse_json_unprocessed_extensions(cgltf_options* options, jsmnt
24132421
*out_extensions_count = 0;
24142422
*out_extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
24152423

2424+
if (!*out_extensions)
2425+
{
2426+
return CGLTF_ERROR_NOMEM;
2427+
}
2428+
24162429
++i;
24172430

24182431
for (int j = 0; j < extensions_size; ++j)
@@ -2529,6 +2542,11 @@ static int cgltf_parse_json_primitive(cgltf_options* options, jsmntok_t const* t
25292542
out_prim->extensions_count = 0;
25302543
out_prim->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
25312544

2545+
if (!out_prim->extensions)
2546+
{
2547+
return CGLTF_ERROR_NOMEM;
2548+
}
2549+
25322550
++i;
25332551
for (int k = 0; k < extensions_size; ++k)
25342552
{
@@ -3053,6 +3071,11 @@ static int cgltf_parse_json_texture_view(cgltf_options* options, jsmntok_t const
30533071
out_texture_view->extensions_count = 0;
30543072
out_texture_view->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
30553073

3074+
if (!out_texture_view->extensions)
3075+
{
3076+
return CGLTF_ERROR_NOMEM;
3077+
}
3078+
30563079
++i;
30573080

30583081
for (int k = 0; k < extensions_size; ++k)
@@ -3503,6 +3526,11 @@ static int cgltf_parse_json_material(cgltf_options* options, jsmntok_t const* to
35033526
out_material->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
35043527
out_material->extensions_count= 0;
35053528

3529+
if (!out_material->extensions)
3530+
{
3531+
return CGLTF_ERROR_NOMEM;
3532+
}
3533+
35063534
for (int k = 0; k < extensions_size; ++k)
35073535
{
35083536
CGLTF_CHECK_KEY(tokens[i]);
@@ -4288,6 +4316,11 @@ static int cgltf_parse_json_node(cgltf_options* options, jsmntok_t const* tokens
42884316
out_node->extensions_count= 0;
42894317
out_node->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
42904318

4319+
if (!out_node->extensions)
4320+
{
4321+
return CGLTF_ERROR_NOMEM;
4322+
}
4323+
42914324
++i;
42924325

42934326
for (int k = 0; k < extensions_size; ++k)
@@ -4884,6 +4917,11 @@ static int cgltf_parse_json_root(cgltf_options* options, jsmntok_t const* tokens
48844917
out_data->data_extensions_count = 0;
48854918
out_data->data_extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
48864919

4920+
if (!out_data->data_extensions)
4921+
{
4922+
return CGLTF_ERROR_NOMEM;
4923+
}
4924+
48874925
++i;
48884926

48894927
for (int k = 0; k < extensions_size; ++k)

0 commit comments

Comments
 (0)