sphair/ClanLib

Sus GL3ShaderObjectProvider::create()

Closed this issue · 2 comments

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;
	}

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());

Fixed