screen: don't leak memory and don't crash if failed to adjust for new screen size

This commit is contained in:
Sigrid Solveig Haflínudóttir 2023-07-17 00:50:19 +02:00
parent a75f4a1496
commit 51d43a6989
2 changed files with 12 additions and 6 deletions

View File

@ -83,14 +83,20 @@ void
screen_resize(Uint16 width, Uint16 height) screen_resize(Uint16 width, Uint16 height)
{ {
Uint8 *bg, *fg; Uint8 *bg, *fg;
Uint32 *pixels; Uint32 *pixels = NULL;
if(width < 0x8 || height < 0x8 || width >= 0x400 || height >= 0x400) if(width < 0x8 || height < 0x8 || width >= 0x400 || height >= 0x400)
return; return;
bg = realloc(uxn_screen.bg, width * height), bg = malloc(width * height),
fg = realloc(uxn_screen.fg, width * height); fg = malloc(width * height);
pixels = realloc(uxn_screen.pixels, width * height * sizeof(Uint32)); if(bg && fg)
if(!bg || !fg || !pixels) pixels = realloc(uxn_screen.pixels, width * height * sizeof(Uint32));
if(!bg || !fg || !pixels) {
free(bg);
free(fg);
return; return;
}
free(uxn_screen.bg);
free(uxn_screen.fg);
uxn_screen.bg = bg; uxn_screen.bg = bg;
uxn_screen.fg = fg; uxn_screen.fg = fg;
uxn_screen.pixels = pixels; uxn_screen.pixels = pixels;

View File

@ -293,7 +293,7 @@ capture_screen(void)
return; return;
SDL_RenderReadPixels(emu_renderer, NULL, format, surface->pixels, surface->pitch); SDL_RenderReadPixels(emu_renderer, NULL, format, surface->pixels, surface->pitch);
strftime(fname, sizeof(fname), "screenshot-%Y%m%d-%H%M%S.bmp", localtime(&t)); strftime(fname, sizeof(fname), "screenshot-%Y%m%d-%H%M%S.bmp", localtime(&t));
if(SDL_SaveBMP(surface, fname) == 0){ if(SDL_SaveBMP(surface, fname) == 0) {
fprintf(stderr, "Saved %s\n", fname); fprintf(stderr, "Saved %s\n", fname);
fflush(stderr); fflush(stderr);
} }