sphair / ClanLib

ClanLib is a cross platform C++ toolkit library.
Other
344 stars 76 forks source link

Sus GL3ShaderObjectProvider::create() #126

Closed rombust closed 3 months ago

rombust commented 8 months ago

In https://github.com/sphair/ClanLib/blob/master/Sources/GL/GL3/gl3_shader_object_provider.cpp

The following code is suspect. Nothing deletes array_sources and array_source_lengths (excluding the caught exception) In addition, Visual Studio code analysis says "Buffer overrun while writing to array_source_length" in the for loop, but it all looks good to me

    GLchar ** array_sources = nullptr;
    GLint *array_source_lengths = nullptr;
    try
    {
        array_sources = new GLchar*[sources.size()];
        array_source_lengths = new GLint[sources.size()];
        for (std::vector<std::string>::size_type i = 0; i < sources.size(); i++)
        {
            array_source_lengths[i] = sources[i].length();
            array_sources[i] = (GLchar*)sources[i].c_str();
        }
        glShaderSource(handle, sources.size(), (const GLchar**)array_sources, array_source_lengths);
    }
    catch (...)
    {
        delete[] array_source_lengths;
        delete[] array_sources;
        throw;
    }
dpjudas commented 5 months ago

Classic example of old code.

Today I'd write it like this:

std::vector<GLint> array_lengths(sources.size());
std::vector<GLcchar*> array_sources(sources.size());
for (size_t i = 0; i < sources.size(); i++)
{
    array_lengths[i] = sources[i].length();
    array_sources[i] = (GLchar*)sources[i].c_str();
}
glShaderSource(handle, sources.size(), array_sources.data(), array_lengths.data());
rombust commented 3 months ago

Fixed