jkuhlmann / cgltf

:diamond_shape_with_a_dot_inside: Single-file glTF 2.0 loader and writer written in C99
MIT License
1.44k stars 136 forks source link

cgltf_parse_file can crash with custom file_open, should not call memory_free #159

Closed pixeljetstream closed 3 years ago

pixeljetstream commented 3 years ago

cgltf_parse_file crashes when the parsing fails with a custom read/release callback in use. A pointer acquired from file_read cannot be passed to memory_free, but that is done here.

    void* file_data = NULL;
    cgltf_size file_size = 0;
    cgltf_result result = file_read(&options->memory, &options->file, path, &file_size, &file_data);
    if (result != cgltf_result_success)
    {
        return result;
    }

    result = cgltf_parse(options, file_data, file_size, out_data);

    if (result != cgltf_result_success)
    {
                // BUG? this should be file_release as the file_data was acquired by file_read
        memory_free(options->memory.user_data, file_data); 
        return result;
    }
jkuhlmann commented 3 years ago

Can you confirm that PR #160 would fix this?

NBickford-NV commented 3 years ago

To help out with this, I looked through all the occurrences of options->file.release and options->memory.free in cgltf.h. Assuming file.release and memory.free are not intended to be interchangeable (based on the issue #94 discussion here), then there may be two issues:

Also - thanks very much; I'll test out the PR and hopefully it should fix the cgltf_parse_file() issue!

jkuhlmann commented 3 years ago

Funny that this all comes up now. Your second bullet point is the same as #158, is that correct?

NBickford-NV commented 3 years ago

Oh, yep, looks like that is indeed the same issue!

Also, I can confirm that PR #160 fixes the cgltf_parse_file() issue with custom file callbacks and invalid glTF files - thank you, Johannes!

jkuhlmann commented 3 years ago

I think in that case we don't have much of a choice except for tracking where the data comes from. I've added another change to #160. Does that help?

pixeljetstream commented 3 years ago

we didn't run into #158 but your fixes looks good