jeudi 4 février 2016

Misuse of GL info log null terminating character in std::string?

I have a fairly simple log() method for a GL shader and program convenience classes, since the respective compile and link methods only return a bool, while hiding all the GL calls; e.g.,

std::string
glShader::log () const
{
    std::string info;
    GLint len = 0;

    if (!glIsShader(gl_shader_obj))
        info = "(invalid shader object)\n";
    else
        glGetShaderiv(gl_shader_obj, GL_INFO_LOG_LENGTH, & len);

    if (len != 0)
    {
        info.resize(static_cast<std::string::size_type>(len));
        glGetShaderInfoLog(gl_shader_obj, len, NULL, & info[0]);
    }

    return info;
}

Is this a misuse of the std::string::resize (size_type) argument? I known that C++11 mandates a null terminating character when queried, i.e., c_str(); but does it guarantee its presence in the storage? This might be a 'reasonable' way to implement string to simplify C string access, but not a requirement.

However, GL_INFO_LOG_LENGTH includes \0 in the number of characters in the log, provided there is no log, in which case the log length is simply zero.

Am I potentially writing past the end of the string's reserved buffer in this fashion? Should I be using (len - 1) in the InfoLog call? Or do I have the idea about C++11 strings wrong? That is, can I safely overwrite with the null terminator?

Aucun commentaire:

Enregistrer un commentaire