From 51d43a6989b669160ce20771c44deb0ffae2b5aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigrid=20Solveig=20Hafl=C3=ADnud=C3=B3ttir?= Date: Mon, 17 Jul 2023 00:50:19 +0200 Subject: [PATCH] screen: don't leak memory and don't crash if failed to adjust for new screen size --- src/devices/screen.c | 16 +++++++++++----- src/uxnemu.c | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/devices/screen.c b/src/devices/screen.c index d4f0723..bf6b6c0 100644 --- a/src/devices/screen.c +++ b/src/devices/screen.c @@ -83,14 +83,20 @@ void screen_resize(Uint16 width, Uint16 height) { Uint8 *bg, *fg; - Uint32 *pixels; + Uint32 *pixels = NULL; if(width < 0x8 || height < 0x8 || width >= 0x400 || height >= 0x400) return; - bg = realloc(uxn_screen.bg, width * height), - fg = realloc(uxn_screen.fg, width * height); - pixels = realloc(uxn_screen.pixels, width * height * sizeof(Uint32)); - if(!bg || !fg || !pixels) + bg = malloc(width * height), + fg = malloc(width * height); + if(bg && fg) + pixels = realloc(uxn_screen.pixels, width * height * sizeof(Uint32)); + if(!bg || !fg || !pixels) { + free(bg); + free(fg); return; + } + free(uxn_screen.bg); + free(uxn_screen.fg); uxn_screen.bg = bg; uxn_screen.fg = fg; uxn_screen.pixels = pixels; diff --git a/src/uxnemu.c b/src/uxnemu.c index 6ba6737..34315c1 100644 --- a/src/uxnemu.c +++ b/src/uxnemu.c @@ -293,7 +293,7 @@ capture_screen(void) return; SDL_RenderReadPixels(emu_renderer, NULL, format, surface->pixels, surface->pitch); 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); fflush(stderr); }