commit 83adebf
d_m
·
2025-04-15 00:35:35 +0000 UTC
parent 5aa2cca
Use memset to initialize memory on screen resize. Previously the system used realloc to increase the size of the fg and bg pixel arrays and just used the memory as-is. However, this can lead to problems because pixel values are expected to be 0-3 and are used to index into the 16 element palette array. On some platforms memory returned by realloc is not initialized to zero, which can lead to large fg and bg pixel values which lead to an out of bounds palette access (i.e. a segmentation fault). Using memset after realloc ensures that pixel data starts at a valid value (zero). This means that as long as no other code sets fg or bg pixels to invalid values, it is safe to directly index into the palette.
1 files changed,
+3,
-0
+3,
-0
1@@ -1,4 +1,5 @@
2 #include <stdlib.h>
3+#include <string.h>
4
5 #include "../uxn.h"
6 #include "screen.h"
7@@ -72,6 +73,8 @@ screen_apply(int width, int height)
8 clamp(height, 8, 0x800);
9 length = MAR2(width) * MAR2(height);
10 uxn_screen.bg = realloc(uxn_screen.bg, length), uxn_screen.fg = realloc(uxn_screen.fg, length);
11+ memset(uxn_screen.bg, 0, length);
12+ memset(uxn_screen.fg, 0, length);
13 uxn_screen.width = width, uxn_screen.height = height;
14 screen_change(0, 0, width, height);
15 emu_resize();