Skip to content

Commit

Permalink
SDL_LockTextureToSurface: robustness of locked region compared to tex…
Browse files Browse the repository at this point in the history
…ture size
  • Loading branch information
1bsyl committed Oct 1, 2019
1 parent a664e95 commit 7d47f52
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/render/SDL_render.c
Expand Up @@ -1684,32 +1684,29 @@ int
SDL_LockTextureToSurface(SDL_Texture *texture, const SDL_Rect *rect,
SDL_Surface **surface)
{
SDL_Rect r;
SDL_Rect real_rect;
void *pixels = NULL;
int pitch, ret;

if (texture == NULL || surface == NULL) {
return -1;
}

if (rect == NULL) {
r.x = 0;
r.y = 0;
r.w = texture->w;
r.h = texture->h;
} else {
r.x = rect->x;
r.y = rect->y;
r.w = SDL_min(texture->w - rect->x, rect->w);
r.h = SDL_min(texture->h - rect->y, rect->h);
real_rect.x = 0;
real_rect.y = 0;
real_rect.w = texture->w;
real_rect.h = texture->h;

if (rect) {
SDL_IntersectRect(rect, &real_rect, &real_rect);
}

ret = SDL_LockTexture(texture, &r, &pixels, &pitch);
ret = SDL_LockTexture(texture, &real_rect, &pixels, &pitch);
if (ret < 0) {
return ret;
}

texture->locked_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, r.w, r.h, 0, pitch, texture->format);
texture->locked_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, real_rect.w, real_rect.h, 0, pitch, texture->format);
if (texture->locked_surface == NULL) {
SDL_UnlockTexture(texture);
return -1;
Expand Down

0 comments on commit 7d47f52

Please sign in to comment.