From 979a87b9bcf40d583251f6e450a7afd8f07d1fe1 Mon Sep 17 00:00:00 2001 From: Bert Pauline Date: Sat, 28 Apr 2018 10:22:00 +0000 Subject: gdk-pixbuf-xlib: Fix out-of-bounds error in dithering loop Use two loops to traverse the array of arrays `DM`, i.e. use `DM[y][x]` instead of `DM[0][i]`. This resolves a warning about undefined behavior when compiling with GCC with aggressive loop optimizations enabled. While we are at it: Move the variable definitions into the body of the outer if-statement. https://bugzilla.gnome.org/show_bug.cgi?id=748211 --- contrib/gdk-pixbuf-xlib/gdk-pixbuf-xlibrgb.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/contrib/gdk-pixbuf-xlib/gdk-pixbuf-xlibrgb.c b/contrib/gdk-pixbuf-xlib/gdk-pixbuf-xlibrgb.c index a87158688..dfde1aeaf 100644 --- a/contrib/gdk-pixbuf-xlib/gdk-pixbuf-xlibrgb.c +++ b/contrib/gdk-pixbuf-xlib/gdk-pixbuf-xlibrgb.c @@ -1346,19 +1346,24 @@ static guint32 *DM_565 = NULL; static void xlib_rgb_preprocess_dm_565 (void) { - int i; - guint32 dith; - if (DM_565 == NULL) { + int i, x, y; + guint32 dith; + DM_565 = malloc(sizeof(guint32) * DM_WIDTH * DM_HEIGHT); - for (i = 0; i < DM_WIDTH * DM_HEIGHT; i++) + i = 0; + for (y = 0; y < DM_HEIGHT; y++) { - dith = DM[0][i] >> 3; - DM_565[i] = (dith << 20) | dith | (((7 - dith) >> 1) << 10); + for (x = 0; x < DM_WIDTH; x++) + { + dith = DM[y][x] >> 3; + DM_565[i] = (dith << 20) | dith | (((7 - dith) >> 1) << 10); #ifdef VERBOSE - printf ("%i %x %x\n", i, dith, DM_565[i]); + printf ("%i %x %x\n", i, dith, DM_565[i]); #endif + i++; + } } } } -- cgit v1.2.1