[HOpenGL] two bugs in ShaderObjects.hs

Claude Heiland-Allen claude at mathr.co.uk
Mon Jan 19 19:58:06 UTC 2015


Hi all,

I don't have a github.com account, so reporting here.  Hope that's ok. 
I found these bugs after Cale posted a link to some problematic 
behaviour (which probably failed initially due to lack of an OpenGL 
context):
http://lpaste.net/118704


The first bug is lack of error checking in createShader:

---8<---
createShader :: ShaderType -> IO Shader
createShader = fmap Shader . glCreateShader . marshalShaderType
---8<---
http://hackage.haskell.org/package/OpenGL-2.10.0.0/docs/src/Graphics-Rendering-OpenGL-GL-Shaders-ShaderObjects.html#createShader

glCreateShader returns 0 on error, and using that "not a shader" id for 
other calls is bound to cause chaos.
https://www.opengl.org/sdk/docs/man/html/glCreateShader.xhtml


The second bug is much more serious, a lack of error checking in shaderVar:

---8<---
shaderVar :: (GLint -> a) -> GetShaderPName -> Shader -> GettableStateVar a
shaderVar f p shader =
    makeGettableStateVar $
       alloca $ \buf -> do
          glGetShaderiv (shaderID shader) (marshalGetShaderPName p) buf
          peek1 f buf
---8<---
http://hackage.haskell.org/package/OpenGL-2.10.0.0/docs/src/Graphics-Rendering-OpenGL-GL-Shaders-ShaderObjects.html#shaderVar

glGetShaderiv doesn't modifiy the contents of buf on error.  This means 
uninitialized memory is read by peek1 and then presumably used by f, 
which can cause a crash (in the best case) or wrong/undefined behaviour 
(in the worst case).
https://www.opengl.org/sdk/docs/man/html/glGetShader.xhtml


Thanks for reading,


Claude
-- 
http://mathr.co.uk


More information about the HOpenGL mailing list