From 00c9949158e82fc93135ac62013bee1c08161649 Mon Sep 17 00:00:00 2001 From: Philipp Stephani Date: Mon, 23 Dec 2019 21:24:37 +0100 Subject: [PATCH] Remove some undefined behavior related to left shifts. Found by UBSan. * src/nsfns.m (ns_set_foreground_color, ns_set_background_color): * src/nsimage.m (getPixelAtX:Y:): * src/nsterm.m (ns_color_index_to_rgba): Add explicit casts to avoid undefined behavior when left-shifting beyond the bounds of the int type. * src/macfont.m (METRICS_VALUE): Add explicit casts to avoid undefined behavior when left-shifting a negative value. --- src/macfont.m | 3 ++- src/nsfns.m | 10 ++++++++-- src/nsimage.m | 7 ++++--- src/nsterm.m | 12 ++++++++---- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/macfont.m b/src/macfont.m index 7170e801407..fa4a818efa6 100644 --- a/src/macfont.m +++ b/src/macfont.m @@ -1126,7 +1126,8 @@ struct macfont_metrics }; #define METRICS_VALUE(metrics, member) \ - (((metrics)->member##_high << 8) | (metrics)->member##_low) + ((int) (((unsigned int) (metrics)->member##_high << 8) \ + | (metrics)->member##_low)) #define METRICS_SET_VALUE(metrics, member, value) \ do {short tmp = (value); (metrics)->member##_low = tmp & 0xff; \ (metrics)->member##_high = tmp >> 8;} while (0) diff --git a/src/nsfns.m b/src/nsfns.m index 1d3aea038ae..3e835a71d03 100644 --- a/src/nsfns.m +++ b/src/nsfns.m @@ -255,7 +255,10 @@ ns_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval) [col getRed: &r green: &g blue: &b alpha: &alpha]; FRAME_FOREGROUND_PIXEL (f) = - ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff)); + ARGB_TO_ULONG ((unsigned long) (alpha * 0xff), + (unsigned long) (r * 0xff), + (unsigned long) (g * 0xff), + (unsigned long) (b * 0xff)); if (FRAME_NS_VIEW (f)) { @@ -296,7 +299,10 @@ ns_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval) [col getRed: &r green: &g blue: &b alpha: &alpha]; FRAME_BACKGROUND_PIXEL (f) = - ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff)); + ARGB_TO_ULONG ((unsigned long) (alpha * 0xff), + (unsigned long) (r * 0xff), + (unsigned long) (g * 0xff), + (unsigned long) (b * 0xff)); if (view != nil) { diff --git a/src/nsimage.m b/src/nsimage.m index 25d3b2299cb..6ca6ee86d66 100644 --- a/src/nsimage.m +++ b/src/nsimage.m @@ -407,9 +407,10 @@ ns_set_alpha (void *img, int x, int y, unsigned char a) if (pixmapData[0] != NULL) { int loc = x + y * [self size].width; - return (pixmapData[3][loc] << 24) /* alpha */ - | (pixmapData[0][loc] << 16) | (pixmapData[1][loc] << 8) - | (pixmapData[2][loc]); + return (((unsigned long) pixmapData[3][loc] << 24) /* alpha */ + | ((unsigned long) pixmapData[0][loc] << 16) + | ((unsigned long) pixmapData[1][loc] << 8) + | (unsigned long) pixmapData[2][loc]); } else { diff --git a/src/nsterm.m b/src/nsterm.m index 9e036aa1608..c575e6c100c 100644 --- a/src/nsterm.m +++ b/src/nsterm.m @@ -2303,8 +2303,10 @@ ns_color_index_to_rgba(int idx, struct frame *f) EmacsCGFloat r, g, b, a; [col getRed: &r green: &g blue: &b alpha: &a]; - return ARGB_TO_ULONG((int)(a*255), - (int)(r*255), (int)(g*255), (int)(b*255)); + return ARGB_TO_ULONG((unsigned long) (a * 255), + (unsigned long) (r * 255), + (unsigned long) (g * 255), + (unsigned long) (b * 255)); } else return idx; @@ -2327,8 +2329,10 @@ ns_query_color(void *col, Emacs_Color *color_def, bool setPixel) if (setPixel == YES) color_def->pixel - = ARGB_TO_ULONG((int)(a*255), - (int)(r*255), (int)(g*255), (int)(b*255)); + = ARGB_TO_ULONG((unsigned long) (a * 255), + (unsigned long) (r * 255), + (unsigned long) (g * 255), + (unsigned long) (b * 255)); } bool -- 2.39.2