diff options
| author | Philipp Stephani | 2019-12-23 21:24:37 +0100 |
|---|---|---|
| committer | Philipp Stephani | 2019-12-23 21:27:23 +0100 |
| commit | 00c9949158e82fc93135ac62013bee1c08161649 (patch) | |
| tree | e4905c8be7020ad36d54528a3b10d8cbab79cd0b /src | |
| parent | 17c19817f7f4ec918a3a9bb3c957b1aa0463da82 (diff) | |
| download | emacs-00c9949158e82fc93135ac62013bee1c08161649.tar.gz emacs-00c9949158e82fc93135ac62013bee1c08161649.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/macfont.m | 3 | ||||
| -rw-r--r-- | src/nsfns.m | 10 | ||||
| -rw-r--r-- | src/nsimage.m | 7 | ||||
| -rw-r--r-- | 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 | |||
| 1126 | }; | 1126 | }; |
| 1127 | 1127 | ||
| 1128 | #define METRICS_VALUE(metrics, member) \ | 1128 | #define METRICS_VALUE(metrics, member) \ |
| 1129 | (((metrics)->member##_high << 8) | (metrics)->member##_low) | 1129 | ((int) (((unsigned int) (metrics)->member##_high << 8) \ |
| 1130 | | (metrics)->member##_low)) | ||
| 1130 | #define METRICS_SET_VALUE(metrics, member, value) \ | 1131 | #define METRICS_SET_VALUE(metrics, member, value) \ |
| 1131 | do {short tmp = (value); (metrics)->member##_low = tmp & 0xff; \ | 1132 | do {short tmp = (value); (metrics)->member##_low = tmp & 0xff; \ |
| 1132 | (metrics)->member##_high = tmp >> 8;} while (0) | 1133 | (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) | |||
| 255 | 255 | ||
| 256 | [col getRed: &r green: &g blue: &b alpha: &alpha]; | 256 | [col getRed: &r green: &g blue: &b alpha: &alpha]; |
| 257 | FRAME_FOREGROUND_PIXEL (f) = | 257 | FRAME_FOREGROUND_PIXEL (f) = |
| 258 | ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff)); | 258 | ARGB_TO_ULONG ((unsigned long) (alpha * 0xff), |
| 259 | (unsigned long) (r * 0xff), | ||
| 260 | (unsigned long) (g * 0xff), | ||
| 261 | (unsigned long) (b * 0xff)); | ||
| 259 | 262 | ||
| 260 | if (FRAME_NS_VIEW (f)) | 263 | if (FRAME_NS_VIEW (f)) |
| 261 | { | 264 | { |
| @@ -296,7 +299,10 @@ ns_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval) | |||
| 296 | 299 | ||
| 297 | [col getRed: &r green: &g blue: &b alpha: &alpha]; | 300 | [col getRed: &r green: &g blue: &b alpha: &alpha]; |
| 298 | FRAME_BACKGROUND_PIXEL (f) = | 301 | FRAME_BACKGROUND_PIXEL (f) = |
| 299 | ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff)); | 302 | ARGB_TO_ULONG ((unsigned long) (alpha * 0xff), |
| 303 | (unsigned long) (r * 0xff), | ||
| 304 | (unsigned long) (g * 0xff), | ||
| 305 | (unsigned long) (b * 0xff)); | ||
| 300 | 306 | ||
| 301 | if (view != nil) | 307 | if (view != nil) |
| 302 | { | 308 | { |
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) | |||
| 407 | if (pixmapData[0] != NULL) | 407 | if (pixmapData[0] != NULL) |
| 408 | { | 408 | { |
| 409 | int loc = x + y * [self size].width; | 409 | int loc = x + y * [self size].width; |
| 410 | return (pixmapData[3][loc] << 24) /* alpha */ | 410 | return (((unsigned long) pixmapData[3][loc] << 24) /* alpha */ |
| 411 | | (pixmapData[0][loc] << 16) | (pixmapData[1][loc] << 8) | 411 | | ((unsigned long) pixmapData[0][loc] << 16) |
| 412 | | (pixmapData[2][loc]); | 412 | | ((unsigned long) pixmapData[1][loc] << 8) |
| 413 | | (unsigned long) pixmapData[2][loc]); | ||
| 413 | } | 414 | } |
| 414 | else | 415 | else |
| 415 | { | 416 | { |
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) | |||
| 2303 | EmacsCGFloat r, g, b, a; | 2303 | EmacsCGFloat r, g, b, a; |
| 2304 | [col getRed: &r green: &g blue: &b alpha: &a]; | 2304 | [col getRed: &r green: &g blue: &b alpha: &a]; |
| 2305 | 2305 | ||
| 2306 | return ARGB_TO_ULONG((int)(a*255), | 2306 | return ARGB_TO_ULONG((unsigned long) (a * 255), |
| 2307 | (int)(r*255), (int)(g*255), (int)(b*255)); | 2307 | (unsigned long) (r * 255), |
| 2308 | (unsigned long) (g * 255), | ||
| 2309 | (unsigned long) (b * 255)); | ||
| 2308 | } | 2310 | } |
| 2309 | else | 2311 | else |
| 2310 | return idx; | 2312 | return idx; |
| @@ -2327,8 +2329,10 @@ ns_query_color(void *col, Emacs_Color *color_def, bool setPixel) | |||
| 2327 | 2329 | ||
| 2328 | if (setPixel == YES) | 2330 | if (setPixel == YES) |
| 2329 | color_def->pixel | 2331 | color_def->pixel |
| 2330 | = ARGB_TO_ULONG((int)(a*255), | 2332 | = ARGB_TO_ULONG((unsigned long) (a * 255), |
| 2331 | (int)(r*255), (int)(g*255), (int)(b*255)); | 2333 | (unsigned long) (r * 255), |
| 2334 | (unsigned long) (g * 255), | ||
| 2335 | (unsigned long) (b * 255)); | ||
| 2332 | } | 2336 | } |
| 2333 | 2337 | ||
| 2334 | bool | 2338 | bool |