Bug Summary

File:c:\siege\siege/src/siege/graphics/surface.c
Location:line 94, column 5
Description:Use of memory after it is freed

Annotated Source Code

1/*
2 Copyright (c) 2007 SIEGE Development Team
3 All rights reserved.
4
5 This file is part of libSIEGE.
6
7 This software is copyrighted work licensed under the terms of the
8 2-clause BSD license. Please consult the file "license.txt" for
9 details.
10
11 If you did not recieve the file with this program, please email
12 Tim Chas <darkuranium@gmail.com>.
13*/
14
15#define SG_BUILD_LIBRARY
16#include <siege/graphics/surface.h>
17#include <siege/graphics/bitmap.h>
18#include <siege/graphics/draw.h>
19#include <siege/core/window.h>
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <math.h>
24
25#include "../internal/gl.h"
26
27SGbool SG_CALL__cdecl _sgSurfaceInit(void)
28{
29 return SG_TRUE1;
30}
31SGbool SG_CALL__cdecl _sgSurfaceDeinit(void)
32{
33 return SG_TRUE1;
34}
35
36SGSurface* SG_CALL__cdecl sgSurfaceCreateBitmap(SGBitmap* bmp, SGbool delbmp)
37{
38 size_t width;
39 size_t height;
40 SGenum bpp;
41 void* data;
42
43 sgBitmapGetData(bmp, &width, &height, &bpp, &data);
44 SGSurface* surface = sgSurfaceCreateData(width, height, bpp, data);
45 if(delbmp)
46 sgBitmapDestroy(bmp);
47 return surface;
48}
49SGSurface* SG_CALL__cdecl sgSurfaceCreateStream(SGStream* stream, SGbool delstream)
50{
51 SGBitmap* bmp = sgBitmapCreateStream(stream, delstream);
52 if(!bmp) return NULL((void*)0);
53 return sgSurfaceCreateBitmap(bmp, SG_TRUE1);
54}
55SGSurface* SG_CALL__cdecl sgSurfaceCreateFile(const char* fname)
56{
57 SGStream* stream = sgStreamCreateFile(fname, "r");
58 if(!stream)
59 {
60 fprintf(stderr(&_iob[2]), "Could not load image %s\n", fname);
61 return NULL((void*)0);
62 }
63 return sgSurfaceCreateStream(stream, SG_TRUE1);
64}
65SGSurface* SG_CALL__cdecl sgSurfaceCreateData(SGuint width, SGuint height, SGenum bpp, void* data)
66{
67 SGTexture* texture = sgTextureCreateData(width, height, bpp, data);
68 if(!texture) return NULL((void*)0);
2
Assuming 'texture' is non-null
3
Taking false branch
69
70 SGSurface* surface = sgSurfaceCreateTexture(texture, SG_TRUE1);
4
Calling 'sgSurfaceCreateTexture'
71 if(!surface)
72 sgTextureDestroy(texture);
73 return surface;
74}
75SGSurface* SG_CALL__cdecl sgSurfaceCreateTexture(SGTexture* texture, SGbool deltex)
76{
77 SGSurface* surface = malloc(sizeof(SGSurface));
5
Memory is allocated
78 if(!surface) return NULL((void*)0);
6
Assuming 'surface' is non-null
7
Taking false branch
79
80 surface->fboid = malloc(sizeof(GLuint));
81 surface->rbid = malloc(sizeof(GLuint));
82 surface->texture = NULL((void*)0);
83 surface->deltex = SG_FALSE0;
84
85 glGenFramebuffersEXT(1, surface->fboid);
86 glGenRenderbuffersEXT(1, surface->rbid);
87
88 if(!sgSurfaceSetTexture(surface, texture, deltex))
8
Taking true branch
89 {
90 surface->texture = NULL((void*)0);
91 sgSurfaceDestroy(surface);
9
Calling 'sgSurfaceDestroy'
12
Returning; memory was released via 1st parameter
92 }
93
94 return surface;
13
Use of memory after it is freed
95}
96SGSurface* SG_CALL__cdecl sgSurfaceCreate(SGuint width, SGuint height, SGenum bpp)
97{
98 return sgSurfaceCreateData(width, height, bpp, NULL((void*)0));
1
Calling 'sgSurfaceCreateData'
99}
100void SG_CALL__cdecl sgSurfaceDestroy(SGSurface* surface)
101{
102 glDeleteRenderbuffersEXT(1, surface->rbid);
103 glDeleteFramebuffersEXT(1, surface->fboid);
104 free(surface->rbid);
105 free(surface->fboid);
106 if(surface->deltex && surface->texture)
10
Taking false branch
107 sgTextureDestroy(surface->texture);
108 free(surface);
11
Memory is released
109}
110
111SGbool SG_CALL__cdecl sgSurfaceSetTexture(SGSurface* surface, SGTexture* texture, SGbool deltex)
112{
113 if(!texture) return SG_FALSE0;
114 if(surface->deltex)
115 {
116 if(surface->texture != texture)
117 sgTextureDestroy(surface->texture);
118 }
119 surface->texture = texture;
120 surface->deltex = deltex;
121
122 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT0x8D40, GLFBO(surface)(*(GLuint*)(surface)->fboid));
123 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT0x8D40, GL_COLOR_ATTACHMENT0_EXT0x8CE0, GL_TEXTURE_2D0x0DE1, GLTEX(texture)(*(GLuint*)(texture)->handle), 0);
124
125 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT0x8D41, GLRB(surface)(*(GLuint*)(surface)->rbid));
126 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT0x8D41, GL_DEPTH_COMPONENT240x81A6, texture->width, texture->height);
127 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT0x8D40, GL_DEPTH_ATTACHMENT_EXT0x8D00, GL_RENDERBUFFER_EXT0x8D41, GLRB(surface)(*(GLuint*)(surface)->rbid));
128 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT0x8D41, 0);
129
130 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT0x8D40);
131
132 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT0x8D40, 0);
133 return status == GL_FRAMEBUFFER_COMPLETE_EXT0x8CD5;
134}
135SGTexture* SG_CALL__cdecl sgSurfaceGetTexture(SGSurface* surface)
136{
137 return surface->texture;
138}
139
140void SG_CALL__cdecl sgSurfaceSetData(SGSurface* surface, size_t width, size_t height, SGenum bpp, void* data)
141{
142 sgTextureSetData(surface->texture, width, height, bpp, data);
143 sgSurfaceSetTexture(surface, surface->texture, surface->deltex);
144}
145void SG_CALL__cdecl sgSurfaceSetSubData(SGSurface* surface, size_t x, size_t y, size_t width, size_t height, SGenum bpp, void* data)
146{
147 sgTextureSetSubData(surface->texture, x, y, width, height, bpp, data);
148}
149void* SG_CALL__cdecl sgSurfaceGetData(SGSurface* surface)
150{
151 return sgTextureGetData(surface->texture);
152}
153void SG_CALL__cdecl sgSurfaceFreeData(void* data)
154{
155 sgTextureFreeData(data);
156}
157
158void SG_CALL__cdecl sgSurfaceDrawRads3f2f2f1f(SGSurface* surface, float x, float y, float z, float xscale, float yscale, float xoffset, float yoffset, float angle)
159{
160 sgTextureDrawRads3f2f2f1f(surface->texture, x, y, z, xscale, yscale, xoffset, yoffset, angle);
161}
162void SG_CALL__cdecl sgSurfaceDrawDegs3f2f2f1f(SGSurface* surface, float x, float y, float z, float xscale, float yscale, float xoffset, float yoffset, float angle)
163{
164 sgSurfaceDrawRads3f2f2f1f(surface, x, y, z, xscale, yscale, xoffset, yoffset, angle * SG_PI3.14159265358979323846 / 180.0);
165}
166void SG_CALL__cdecl sgSurfaceDrawRads2f2f2f1f(SGSurface* surface, float x, float y, float xscale, float yscale, float xoffset, float yoffset, float angle)
167{
168 sgSurfaceDrawRads3f2f2f1f(surface, x, y, 0.0, xscale, yscale, xoffset, yoffset, angle);
169}
170void SG_CALL__cdecl sgSurfaceDrawDegs2f2f2f1f(SGSurface* surface, float x, float y, float xscale, float yscale, float xoffset, float yoffset, float angle)
171{
172 sgSurfaceDrawDegs3f2f2f1f(surface, x, y, 0.0, xscale, yscale, xoffset, yoffset, angle);
173}
174void SG_CALL__cdecl sgSurfaceDrawRads3f2f1f(SGSurface* surface, float x, float y, float z, float xscale, float yscale, float angle)
175{
176 sgSurfaceDrawRads3f2f2f1f(surface, x, y, z, xscale, yscale, 0.0, 0.0, angle);
177}
178void SG_CALL__cdecl sgSurfaceDrawDegs3f2f1f(SGSurface* surface, float x, float y, float z, float xscale, float yscale, float angle)
179{
180 sgSurfaceDrawDegs3f2f2f1f(surface, x, y, z, xscale, yscale, 0.0, 0.0, angle);
181}
182void SG_CALL__cdecl sgSurfaceDrawRads2f2f1f(SGSurface* surface, float x, float y, float xscale, float yscale, float angle)
183{
184 sgSurfaceDrawRads3f2f2f1f(surface, x, y, 0.0, xscale, yscale, 0.0, 0.0, angle);
185}
186void SG_CALL__cdecl sgSurfaceDrawDegs2f2f1f(SGSurface* surface, float x, float y, float xscale, float yscale, float angle)
187{
188 sgSurfaceDrawDegs3f2f2f1f(surface, x, y, 0.0, xscale, yscale, 0.0, 0.0, angle);
189}
190void SG_CALL__cdecl sgSurfaceDrawRads3f1f(SGSurface* surface, float x, float y, float z, float angle)
191{
192 sgSurfaceDrawRads3f2f2f1f(surface, x, y, z, 1.0, 1.0, 0.0, 0.0, angle);
193}
194void SG_CALL__cdecl sgSurfaceDrawDegs3f1f(SGSurface* surface, float x, float y, float z, float angle)
195{
196 sgSurfaceDrawDegs3f2f2f1f(surface, x, y, z, 1.0, 1.0, 0.0, 0.0, angle);
197}
198void SG_CALL__cdecl sgSurfaceDrawRads2f1f(SGSurface* surface, float x, float y, float angle)
199{
200 sgSurfaceDrawRads3f2f2f1f(surface, x, y, 0.0, 1.0, 1.0, 0.0, 0.0, angle);
201}
202void SG_CALL__cdecl sgSurfaceDrawDegs2f1f(SGSurface* surface, float x, float y, float angle)
203{
204 sgSurfaceDrawDegs3f2f2f1f(surface, x, y, 0.0, 1.0, 1.0, 0.0, 0.0, angle);
205}
206void SG_CALL__cdecl sgSurfaceDraw3f2f2f(SGSurface* surface, float x, float y, float z, float xscale, float yscale, float xoffset, float yoffset)
207{
208 sgSurfaceDrawRads3f2f2f1f(surface, x, y, z, xscale, yscale, xoffset, yoffset, 0.0);
209}
210void SG_CALL__cdecl sgSurfaceDraw2f2f2f(SGSurface* surface, float x, float y, float xscale, float yscale, float xoffset, float yoffset)
211{
212 sgSurfaceDrawRads3f2f2f1f(surface, x, y, 0.0, xscale, yscale, xoffset, yoffset, 0.0);
213}
214void SG_CALL__cdecl sgSurfaceDraw3f2f(SGSurface* surface, float x, float y, float z, float xscale, float yscale)
215{
216 sgSurfaceDrawRads3f2f2f1f(surface, x, y, z, xscale, yscale, 0.0, 0.0, 0.0);
217}
218void SG_CALL__cdecl sgSurfaceDraw2f2f(SGSurface* surface, float x, float y, float xscale, float yscale)
219{
220 sgSurfaceDrawRads3f2f2f1f(surface, x, y, 0.0, xscale, yscale, 0.0, 0.0, 0.0);
221}
222void SG_CALL__cdecl sgSurfaceDraw3f(SGSurface* surface, float x, float y, float z)
223{
224 sgSurfaceDrawRads3f2f2f1f(surface, x, y, z, 1.0, 1.0, 0.0, 0.0, 0.0);
225}
226void SG_CALL__cdecl sgSurfaceDraw2f(SGSurface* surface, float x, float y)
227{
228 sgSurfaceDrawRads3f2f2f1f(surface, x, y, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0);
229}
230void SG_CALL__cdecl sgSurfaceDraw(SGSurface* surface)
231{
232 sgSurfaceDrawRads3f2f2f1f(surface, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
233}
234
235void SG_CALL__cdecl sgSurfaceTarget(SGSurface* surface)
236{
237 glPushAttrib(GL_VIEWPORT_BIT0x00000800);
238 glViewport(0, 0, surface->texture->width, surface->texture->height);
239 glMatrixMode(GL_PROJECTION0x1701);
240 glPushMatrix();
241 glLoadIdentity();
242 glOrtho(0, surface->texture->width, surface->texture->height, 0, 127, -128);
243 glMatrixMode(GL_MODELVIEW0x1700);
244
245 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT0x8D40, GLFBO(surface)(*(GLuint*)(surface)->fboid));
246}
247void SG_CALL__cdecl sgSurfaceUntarget(SGSurface* surface)
248{
249 glMatrixMode(GL_PROJECTION0x1701);
250 glPopMatrix();
251 glMatrixMode(GL_MODELVIEW0x1700);
252 glPopAttrib();
253
254 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT0x8D40, 0);
255}
256void SG_CALL__cdecl sgSurfaceClear4f(SGSurface* surface, float r, float g, float b, float a)
257{
258 sgSurfaceTarget(surface);
259 sgDrawClear4f(r, g, b, a);
260 sgSurfaceUntarget(surface);
261}
262void SG_CALL__cdecl sgSurfaceClear3f(SGSurface* surface, float r, float g, float b)
263{
264 sgSurfaceClear4f(surface, r, g, b, 1.0f);
265}
266void SG_CALL__cdecl sgSurfaceClear2f(SGSurface* surface, float g, float a)
267{
268 sgSurfaceClear4f(surface, g, g, g, a);
269}
270void SG_CALL__cdecl sgSurfaceClear1f(SGSurface* surface, float g)
271{
272 sgSurfaceClear4f(surface, g, g, g, 1.0f);
273}
274void SG_CALL__cdecl sgSurfaceClear4ub(SGSurface* surface, SGubyte r, SGubyte g, SGubyte b, SGubyte a)
275{
276 sgSurfaceClear4f(surface, r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
277}
278void SG_CALL__cdecl sgSurfaceClear3ub(SGSurface* surface, SGubyte r, SGubyte g, SGubyte b)
279{
280 sgSurfaceClear4f(surface, r / 255.0f, g / 255.0f, b / 255.0f, 1.0f);
281}
282void SG_CALL__cdecl sgSurfaceClear2ub(SGSurface* surface, SGubyte g, SGubyte a)
283{
284 sgSurfaceClear4f(surface, g / 255.0f, g / 255.0f, g / 255.0f, a / 255.0f);
285}
286void SG_CALL__cdecl sgSurfaceClear1ub(SGSurface* surface, SGubyte g)
287{
288 sgSurfaceClear4f(surface, g / 255.0f, g / 255.0f, g / 255.0f, 1.0f);
289}
290void SG_CALL__cdecl sgSurfaceClear(SGSurface* surface)
291{
292 sgSurfaceClear4f(surface, 0.0f, 0.0f, 0.0f, 0.0f);
293}
294
295void SG_CALL__cdecl sgSurfaceGetSize(SGSurface* surface, SGuint* width, SGuint* height)
296{
297 sgTextureGetSize(surface->texture, width, height);
298}
299SGuint SG_CALL__cdecl sgSurfaceGetWidth(SGSurface* surface)
300{
301 return sgTextureGetWidth(surface->texture);
302}
303SGuint SG_CALL__cdecl sgSurfaceGetHeight(SGSurface* surface)
304{
305 return sgTextureGetHeight(surface->texture);
306}
307SGenum SG_CALL__cdecl sgSurfaceGetBPP(SGSurface* surface)
308{
309 return sgTextureGetBPP(surface->texture);
310}