Skip to content

Commit 14deef9

Browse files
castholmslouken
authored andcommitted
emscripten: Fix undefined behavior in opengles2 renderer
1 parent 5283f73 commit 14deef9

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/render/opengles2/SDL_render_gles2.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,8 +1022,8 @@ static bool SetDrawState(GLES2_RenderData *data, const SDL_RenderCommand *cmd, c
10221022
}
10231023

10241024
if (texture) {
1025-
SDL_Vertex *verts = (SDL_Vertex *)(((Uint8 *)vertices) + cmd->data.draw.first);
1026-
data->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, stride, (const GLvoid *)&verts->tex_coord);
1025+
uintptr_t base = (uintptr_t)vertices + cmd->data.draw.first; // address of first vertex, or base offset when using VBOs.
1026+
data->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, stride, (const GLvoid *)(base + offsetof(SDL_Vertex, tex_coord)));
10271027
}
10281028

10291029
SDL_Colorspace colorspace = texture ? texture->colorspace : SDL_COLORSPACE_SRGB;
@@ -1057,9 +1057,9 @@ static bool SetDrawState(GLES2_RenderData *data, const SDL_RenderCommand *cmd, c
10571057

10581058
// all drawing commands use this
10591059
{
1060-
SDL_VertexSolid *verts = (SDL_VertexSolid *)(((Uint8 *)vertices) + cmd->data.draw.first);
1061-
data->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, stride, (const GLvoid *)&verts->position);
1062-
data->glVertexAttribPointer(GLES2_ATTRIBUTE_COLOR, 4, GL_FLOAT, GL_TRUE /* Normalized */, stride, (const GLvoid *)&verts->color);
1060+
uintptr_t base = (uintptr_t)vertices + cmd->data.draw.first; // address of first vertex, or base offset when using VBOs.
1061+
data->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, stride, (const GLvoid *)(base + offsetof(SDL_VertexSolid, position)));
1062+
data->glVertexAttribPointer(GLES2_ATTRIBUTE_COLOR, 4, GL_FLOAT, GL_TRUE /* Normalized */, stride, (const GLvoid *)(base + offsetof(SDL_VertexSolid, color)));
10631063
}
10641064

10651065
return true;
@@ -1375,7 +1375,8 @@ static bool GLES2_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd
13751375
if (data->current_vertex_buffer >= SDL_arraysize(data->vertex_buffers)) {
13761376
data->current_vertex_buffer = 0;
13771377
}
1378-
vertices = NULL; // attrib pointers will be offsets into the VBO.
1378+
// attrib pointers will be offsets into the VBO.
1379+
vertices = (void *)(uintptr_t)0; // must be the exact value 0, not NULL (the representation of NULL is not guaranteed to be 0).
13791380
#endif
13801381

13811382
while (cmd) {

0 commit comments

Comments
 (0)