Skip to content

Commit 08d85af

Browse files
committed
mitigate "If an error is generated, no change is made to the contents"
Most glGet* functions don't change change the contents of the buffer if an error is generated. Buffers created with `alloca` have undefined contents, which if used causes undefined behaviour (eg: allocating a huge chunk of memory if a size is peeked, overwriting unspecified memory if a pointer is peeked). This patch mitigates the issue to some extent by using `with 0` and `with nullPtr` instead of `alloca`, so at least the content of the buffer is defined. There are still some places unchanged, this patch concentrates on cases where the result would be used as a size for memory allocations or as a pointer. The initial bug report was here: https://www.haskell.org/pipermail/hopengl/2015-January/001152.html
1 parent 7dda1ac commit 08d85af

File tree

17 files changed

+34
-32
lines changed

17 files changed

+34
-32
lines changed

src/Graphics/Rendering/OpenGL/GL/BufferObjects.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ module Graphics.Rendering.OpenGL.GL.BufferObjects (
4242
) where
4343

4444
import Data.Maybe
45-
import Foreign.Marshal.Alloc
4645
import Foreign.Marshal.Array
46+
import Foreign.Marshal.Utils
4747
import Foreign.Ptr
4848
import Foreign.Storable
4949
import Graphics.Rendering.OpenGL.GL.Exception
@@ -278,15 +278,15 @@ marshalGetBufferPName x = case x of
278278
GetBufferMapped -> gl_BUFFER_MAPPED
279279

280280
getBufferParameter :: BufferTarget -> (GLenum -> a) -> GetBufferPName -> IO a
281-
getBufferParameter t f p = alloca $ \buf -> do
281+
getBufferParameter t f p = with 0 $ \buf -> do
282282
glGetBufferParameteriv (marshalBufferTarget t)
283283
(marshalGetBufferPName p) buf
284284
peek1 (f . fromIntegral) buf
285285

286286
--------------------------------------------------------------------------------
287287

288288
getBufferPointer :: BufferTarget -> IO (Ptr a)
289-
getBufferPointer t = alloca $ \buf -> do
289+
getBufferPointer t = with nullPtr $ \buf -> do
290290
glGetBufferPointerv (marshalBufferTarget t) gl_BUFFER_MAP_POINTER buf
291291
peek buf
292292

src/Graphics/Rendering/OpenGL/GL/Evaluators.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ module Graphics.Rendering.OpenGL.GL.Evaluators (
4747
import Control.Monad
4848
import Data.List
4949
import Foreign.ForeignPtr
50-
import Foreign.Marshal.Alloc
5150
import Foreign.Marshal.Array
51+
import Foreign.Marshal.Utils
5252
import Foreign.Ptr
5353
import Foreign.Storable
5454
import Graphics.Rendering.OpenGL.GL.Capability
@@ -198,7 +198,7 @@ getMap1 dummyControlPoint = do
198198
domain <- allocaArray 2 $ \ptr -> do
199199
glGetMapv target (marshalGetMapQuery Domain) ptr
200200
peek2 (,) ptr
201-
order <- alloca $ \ptr -> do
201+
order <- with 0 $ \ptr -> do
202202
glGetMapiv target (marshalGetMapQuery Order) ptr
203203
fmap fromIntegral $ peek ptr
204204
withNewMap1 (MapDescriptor domain (numComponents dummyControlPoint) order numComp) $
@@ -297,7 +297,7 @@ getMap2 dummyControlPoint = do
297297
(uDomain, vDomain) <- allocaArray 4 $ \ptr -> do
298298
glGetMapv target (marshalGetMapQuery Domain) ptr
299299
peek4 (\u1 u2 v1 v2 -> ((u1, u2), (v1, v2))) ptr
300-
(uOrder, vOrder) <- allocaArray 2 $ \ptr -> do
300+
(uOrder, vOrder) <- withArray [0,0] $ \ptr -> do
301301
glGetMapiv target (marshalGetMapQuery Order) ptr
302302
peek2 (,) ptr
303303
let vStride = numComponents dummyControlPoint

src/Graphics/Rendering/OpenGL/GL/FramebufferObjects/FramebufferObjectAttachment.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ instance FramebufferAttachment BufferMode where
9898

9999
getFBAParameteriv :: FramebufferAttachment fba => FramebufferTarget -> fba
100100
-> (GLint -> a) -> GLenum -> IO a
101-
getFBAParameteriv fbt fba f p = alloca $ \buf -> do
101+
getFBAParameteriv fbt fba f p = with 0 $ \buf -> do
102102
glGetFramebufferAttachmentParameteriv (marshalFramebufferTarget fbt)
103103
mfba p buf
104104
peek1 f buf

src/Graphics/Rendering/OpenGL/GL/FramebufferObjects/RenderbufferTarget.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ marshalRenderbufferTarget x = case x of
3434

3535
getRBParameteriv :: RenderbufferTarget -> (GLint -> a) -> GLenum -> IO a
3636
getRBParameteriv rbt f p =
37-
alloca $ \buf -> do
37+
with 0 $ \buf -> do
3838
glGetRenderbufferParameteriv (marshalRenderbufferTarget rbt) p buf
3939
peek1 f buf
4040
-----------------------------------------------------------------------------

src/Graphics/Rendering/OpenGL/GL/PixelRectangles/ColorTable.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ colorTableFormat ct =
213213

214214
getColorTableParameteri :: (GLint -> a) -> ColorTable -> ColorTablePName -> IO a
215215
getColorTableParameteri f ct p =
216-
alloca $ \buf -> do
216+
with 0 $ \buf -> do
217217
glGetColorTableParameteriv
218218
(marshalColorTable ct)
219219
(marshalColorTablePName p)

src/Graphics/Rendering/OpenGL/GL/PixelRectangles/Convolution.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ convolutionParameteri t p =
184184
getConvolutionParameteri ::
185185
(GLint -> a) -> ConvolutionTarget -> ConvolutionParameter -> IO a
186186
getConvolutionParameteri f t p =
187-
alloca $ \buf -> do
187+
with 0 $ \buf -> do
188188
glGetConvolutionParameteriv
189189
(marshalConvolutionTarget t) (marshalConvolutionParameter p) buf
190190
peek1 f buf

src/Graphics/Rendering/OpenGL/GL/PixelRectangles/Histogram.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Graphics.Rendering.OpenGL.GL.PixelRectangles.Histogram (
1818
histogramRGBASizes, histogramLuminanceSize
1919
) where
2020

21-
import Foreign.Marshal.Alloc
21+
import Foreign.Marshal.Utils
2222
import Graphics.Rendering.OpenGL.GL.Capability
2323
import Graphics.Rendering.OpenGL.GL.PeekPoke
2424
import Graphics.Rendering.OpenGL.GL.PixelData
@@ -63,7 +63,7 @@ getHistogram' proxy = do
6363
getHistogramParameteri ::
6464
(GLint -> a) -> Proxy -> GetHistogramParameterPName -> IO a
6565
getHistogramParameteri f proxy p =
66-
alloca $ \buf -> do
66+
with 0 $ \buf -> do
6767
glGetHistogramParameteriv
6868
(marshalHistogramTarget (proxyToHistogramTarget proxy))
6969
(marshalGetHistogramParameterPName p)

src/Graphics/Rendering/OpenGL/GL/PixelRectangles/Minmax.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module Graphics.Rendering.OpenGL.GL.PixelRectangles.Minmax (
1717
minmax, getMinmax, resetMinmax
1818
) where
1919

20-
import Foreign.Marshal.Alloc
20+
import Foreign.Marshal.Utils
2121
import Graphics.Rendering.OpenGL.GL.Capability
2222
import Graphics.Rendering.OpenGL.GL.PeekPoke
2323
import Graphics.Rendering.OpenGL.GL.PixelData
@@ -81,7 +81,7 @@ marshalGetMinmaxParameterPName x = case x of
8181

8282
getMinmaxParameteri :: (GLint -> a) -> GetMinmaxParameterPName -> IO a
8383
getMinmaxParameteri f p =
84-
alloca $ \buf -> do
84+
with 0 $ \buf -> do
8585
glGetMinmaxParameteriv
8686
(marshalMinmaxTarget Minmax)
8787
(marshalGetMinmaxParameterPName p)

src/Graphics/Rendering/OpenGL/GL/PixellikeObject.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Graphics.Rendering.OpenGL.GL.PixellikeObject (
1515
PixellikeObjectTarget(pixellikeObjTarParam),
1616
) where
1717

18-
import Foreign.Marshal.Alloc
18+
import Foreign.Marshal.Utils
1919
import Graphics.Rendering.OpenGL.GL.FramebufferObjects.FramebufferObjectAttachment
2020
import Graphics.Rendering.OpenGL.GL.FramebufferObjects.FramebufferTarget
2121
import Graphics.Rendering.OpenGL.GL.FramebufferObjects.RenderbufferTarget
@@ -77,6 +77,6 @@ instance QueryableTextureTarget t => PixellikeObjectTarget (TextureTargetFull t)
7777
DepthSize -> gl_TEXTURE_DEPTH_SIZE
7878
StencilSize -> gl_TEXTURE_STENCIL_SIZE
7979
pixObjTarQueryFunc (TextureTargetFull t level) p =
80-
alloca $ \buf -> do
80+
with 0 $ \buf -> do
8181
glGetTexLevelParameteriv (marshalQueryableTextureTarget t) level p buf
8282
peek1 id buf

src/Graphics/Rendering/OpenGL/GL/QueryObjects.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module Graphics.Rendering.OpenGL.GL.QueryObjects (
3131
) where
3232

3333
import Foreign.Marshal.Alloc
34+
import Foreign.Marshal.Utils
3435
import Foreign.Ptr
3536
import Foreign.Storable
3637
import Graphics.Rendering.OpenGL.GL.Exception
@@ -111,7 +112,7 @@ queryCounterBits = getQueryi fromIntegral QueryCounterBits
111112
getQueryi :: (GLint -> a) -> GetQueryPName -> QueryTarget -> GettableStateVar a
112113
getQueryi f p t =
113114
makeGettableStateVar $
114-
alloca $ \buf -> do
115+
with 0 $ \buf -> do
115116
getQueryiv' t p buf
116117
peek1 f buf
117118

src/Graphics/Rendering/OpenGL/GL/QueryUtils/VertexAttrib.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Graphics.Rendering.OpenGL.GL.QueryUtils.VertexAttrib (
1919
) where
2020

2121
import Foreign.Marshal.Alloc
22+
import Foreign.Marshal.Utils
2223
import Foreign.Ptr
2324
import Foreign.Storable
2425
import Graphics.Rendering.OpenGL.GL.PeekPoke
@@ -55,7 +56,7 @@ marshalGetVertexAttribPName x = case x of
5556
--------------------------------------------------------------------------------
5657

5758
getVertexAttribInteger1 :: (GLint -> b) -> AttribLocation -> GetVertexAttribPName -> IO b
58-
getVertexAttribInteger1 f (AttribLocation location) n = alloca $ \buf -> do
59+
getVertexAttribInteger1 f (AttribLocation location) n = with 0 $ \buf -> do
5960
glGetVertexAttribiv location (marshalGetVertexAttribPName n) buf
6061
peek1 f buf
6162

@@ -92,6 +93,6 @@ marshalGetVertexAttribPointerPName x = case x of
9293
--------------------------------------------------------------------------------
9394

9495
getVertexAttribPointer :: AttribLocation -> GetVertexAttribPointerPName -> IO (Ptr a)
95-
getVertexAttribPointer (AttribLocation location) n = alloca $ \buf -> do
96+
getVertexAttribPointer (AttribLocation location) n = with nullPtr $ \buf -> do
9697
glGetVertexAttribPointerv location (marshalGetVertexAttribPointerPName n) buf
9798
peek buf

src/Graphics/Rendering/OpenGL/GL/Shaders/Program.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Graphics.Rendering.OpenGL.GL.Shaders.Program (
2020
programVar1, programVar3
2121
) where
2222

23-
import Foreign.Marshal.Alloc
23+
import Foreign.Marshal.Utils
2424
import Foreign.Ptr
2525
import Graphics.Rendering.OpenGL.GL.GLboolean
2626
import Graphics.Rendering.OpenGL.GL.ObjectName
@@ -109,6 +109,6 @@ programVar3 = programVarN . peek3
109109
programVarN :: (Ptr GLint -> IO a) -> GetProgramPName -> Program -> GettableStateVar a
110110
programVarN f p program =
111111
makeGettableStateVar $
112-
alloca $ \buf -> do
112+
with 0 $ \buf -> do
113113
glGetProgramiv (programID program) (marshalGetProgramPName p) buf
114114
f buf

src/Graphics/Rendering/OpenGL/GL/Shaders/ShaderObjects.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ marshalGetShaderPName x = case x of
153153
shaderVar :: (GLint -> a) -> GetShaderPName -> Shader -> GettableStateVar a
154154
shaderVar f p shader =
155155
makeGettableStateVar $
156-
alloca $ \buf -> do
156+
with 0 $ \buf -> do
157157
glGetShaderiv (shaderID shader) (marshalGetShaderPName p) buf
158158
peek1 f buf
159159

src/Graphics/Rendering/OpenGL/GL/Shaders/Variables.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Graphics.Rendering.OpenGL.GL.Shaders.Variables (
1919
) where
2020

2121
import Control.Monad
22-
import Foreign.Marshal.Alloc
22+
import Foreign.Marshal.Utils
2323
import Foreign.Ptr
2424
import Foreign.Storable
2525
import Graphics.Rendering.OpenGL.GL.ByteString
@@ -150,9 +150,9 @@ activeVars numVars maxLength getter unmarshalType p@(Program program) =
150150
makeGettableStateVar $ do
151151
numActiveVars <- get (numVars p)
152152
maxLen <- get (maxLength p)
153-
alloca $ \nameLengthBuf ->
154-
alloca $ \sizeBuf ->
155-
alloca $ \typeBuf ->
153+
with 0 $ \nameLengthBuf ->
154+
with 0 $ \sizeBuf ->
155+
with 0 $ \typeBuf ->
156156
let ixs = if numActiveVars > 0 then [0 .. numActiveVars-1] else []
157157
in forM ixs $ \i -> do
158158
n <- createAndTrimByteString maxLen $ \nameBuf -> do

src/Graphics/Rendering/OpenGL/GL/SyncObjects.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module Graphics.Rendering.OpenGL.GL.SyncObjects (
2525
SyncStatus(..), syncStatus
2626
) where
2727

28-
import Foreign.Marshal.Alloc
28+
import Foreign.Marshal.Utils
2929
import Foreign.Ptr
3030
import Graphics.Rendering.OpenGL.GL.GLboolean
3131
import Graphics.Rendering.OpenGL.GL.ObjectName
@@ -108,6 +108,6 @@ unmarshalSyncStatus x
108108
syncStatus :: SyncObject -> GettableStateVar SyncStatus
109109
syncStatus syncObject =
110110
makeGettableStateVar $
111-
alloca $ \buf -> do
111+
with 0 $ \buf -> do
112112
glGetSynciv (syncID syncObject) gl_SYNC_STATUS 1 nullPtr buf
113113
peek1 (unmarshalSyncStatus . fromIntegral) buf

src/Graphics/Rendering/OpenGL/GL/Texturing/Queries.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Graphics.Rendering.OpenGL.GL.Texturing.Queries (
2020
) where
2121

2222
import Control.Monad
23-
import Foreign.Marshal.Alloc
23+
import Foreign.Marshal.Utils
2424
import Graphics.Rendering.OpenGL.GL.GLboolean
2525
import Graphics.Rendering.OpenGL.GL.PeekPoke
2626
import Graphics.Rendering.OpenGL.GL.PixelRectangles
@@ -157,6 +157,6 @@ getTexLevelParameteriNoProxy f = getTexLevelParameteri f . marshalQueryableTextu
157157

158158
getTexLevelParameteri :: (GLint -> a) -> GLenum -> Level -> TexLevelParameter -> IO a
159159
getTexLevelParameteri f t level p =
160-
alloca $ \buf -> do
160+
with 0 $ \buf -> do
161161
glGetTexLevelParameteriv t level (marshalTexLevelParameter p) buf
162162
peek1 f buf

src/Graphics/Rendering/OpenGL/GL/VertexArrays.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module Graphics.Rendering.OpenGL.GL.VertexArrays (
3535
vertexAttribPointer, vertexAttribArray,
3636
) where
3737

38-
import Foreign.Marshal.Alloc
38+
import Foreign.Marshal.Utils
3939
import Foreign.Ptr
4040
import Foreign.Storable
4141
import Graphics.Rendering.OpenGL.GL.Capability
@@ -438,7 +438,7 @@ marshalGetPointervPName x = case x of
438438
--------------------------------------------------------------------------------
439439

440440
getPointer :: GetPointervPName -> IO (Ptr a)
441-
getPointer n = alloca $ \buf -> do
441+
getPointer n = with nullPtr $ \buf -> do
442442
glGetPointerv (marshalGetPointervPName n) buf
443443
peek buf
444444

0 commit comments

Comments
 (0)