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.
This commit is contained in:
Philipp Stephani 2019-12-23 21:24:37 +01:00
parent 17c19817f7
commit 00c9949158
4 changed files with 22 additions and 10 deletions

View file

@ -1126,7 +1126,8 @@ sorted in the canonical order (CTFontManagerCompareFontFamilyNames on
};
#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)

View file

@ -255,7 +255,10 @@ Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side.
[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 @@ Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side.
[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)
{

View file

@ -407,9 +407,10 @@ - (unsigned long) getPixelAtX: (int)x Y: (int)y
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
{

View file

@ -2303,8 +2303,10 @@ so some key presses (TAB) are swallowed by the system. */
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 @@ so some key presses (TAB) are swallowed by the system. */
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