]> git.eshelyaron.com Git - emacs.git/commitdiff
Remove some undefined behavior related to left shifts.
authorPhilipp Stephani <phst@google.com>
Mon, 23 Dec 2019 20:24:37 +0000 (21:24 +0100)
committerPhilipp Stephani <phst@google.com>
Mon, 23 Dec 2019 20:27:23 +0000 (21:27 +0100)
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
src/nsfns.m
src/nsimage.m
src/nsterm.m

index 7170e80140790a62b1561c731d389b06ea7068b2..fa4a818efa6af5b1e770271a34ed95bf6fbe42ad 100644 (file)
@@ -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)
index 1d3aea038ae2c744f39f83d71457158a73eab0dd..3e835a71d03831df2149895dfdef2738b427d851 100644 (file)
@@ -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)
     {
index 25d3b2299cb52d02e615fdbbae29cd0061ea2058..6ca6ee86d66c7c640b61db46acfaa9b4498e7a93 100644 (file)
@@ -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
     {
index 9e036aa16081fee94a812422aee98d296e8535de..c575e6c100cec7a20848e3d645ca2cf875f0e2eb 100644 (file)
@@ -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