SDL_LockTextureToSurface: robustness of locked region compared to texture size

This commit is contained in:
Sylvain Becker 2019-10-01 09:26:30 +02:00
parent a664e95d65
commit 7d47f526a7
1 changed files with 10 additions and 13 deletions

View File

@ -1684,7 +1684,7 @@ int
SDL_LockTextureToSurface(SDL_Texture *texture, const SDL_Rect *rect, SDL_LockTextureToSurface(SDL_Texture *texture, const SDL_Rect *rect,
SDL_Surface **surface) SDL_Surface **surface)
{ {
SDL_Rect r; SDL_Rect real_rect;
void *pixels = NULL; void *pixels = NULL;
int pitch, ret; int pitch, ret;
@ -1692,24 +1692,21 @@ SDL_LockTextureToSurface(SDL_Texture *texture, const SDL_Rect *rect,
return -1; return -1;
} }
if (rect == NULL) { real_rect.x = 0;
r.x = 0; real_rect.y = 0;
r.y = 0; real_rect.w = texture->w;
r.w = texture->w; real_rect.h = texture->h;
r.h = texture->h;
} else { if (rect) {
r.x = rect->x; SDL_IntersectRect(rect, &real_rect, &real_rect);
r.y = rect->y;
r.w = SDL_min(texture->w - rect->x, rect->w);
r.h = SDL_min(texture->h - rect->y, rect->h);
} }
ret = SDL_LockTexture(texture, &r, &pixels, &pitch); ret = SDL_LockTexture(texture, &real_rect, &pixels, &pitch);
if (ret < 0) { if (ret < 0) {
return ret; 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) { if (texture->locked_surface == NULL) {
SDL_UnlockTexture(texture); SDL_UnlockTexture(texture);
return -1; return -1;