emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 00c9949 1/2: Remove some undefined behavior related to left shift


From: Philipp Stephani
Subject: master 00c9949 1/2: Remove some undefined behavior related to left shifts.
Date: Mon, 23 Dec 2019 15:27:55 -0500 (EST)

branch: master
commit 00c9949158e82fc93135ac62013bee1c08161649
Author: Philipp Stephani <address@hidden>
Commit: Philipp Stephani <address@hidden>

    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 7170e80..fa4a818 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 1d3aea0..3e835a7 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 25d3b22..6ca6ee8 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 9e036aa..c575e6c 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]