From 7c0994dafc91944477e03700304b2308b0e3fdb1 Mon Sep 17 00:00:00 2001 From: Alan Barr Date: Sat, 13 Oct 2018 19:04:40 +0100 Subject: [PATCH] Fixes for undefined behaviour on signed shift (#3453) Fixes for undefined behaviour on signed shift Change literal 1 to unsigned to allow safe bitshift of 31. Caught by cppcheck. Make 0xFF unsigned to prevent a left shift into signed bit. Spotted by @orestisf1993 --- i3bar/src/ipc.c | 4 ++-- include/i3/ipc.h | 2 +- libi3/get_colorpixel.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/i3bar/src/ipc.c b/i3bar/src/ipc.c index 56fe2798..9a7fb21e 100644 --- a/i3bar/src/ipc.c +++ b/i3bar/src/ipc.c @@ -278,8 +278,8 @@ void got_data(struct ev_loop *loop, ev_io *watcher, int events) { buffer[size] = '\0'; /* And call the callback (indexed by the type) */ - if (type & (1 << 31)) { - type ^= 1 << 31; + if (type & (1UL << 31)) { + type ^= 1UL << 31; event_handlers[type](buffer); } else { if (reply_handlers[type]) diff --git a/include/i3/ipc.h b/include/i3/ipc.h index 0c57f7fd..884a0cf6 100644 --- a/include/i3/ipc.h +++ b/include/i3/ipc.h @@ -87,7 +87,7 @@ typedef struct i3_ipc_header { * Events from i3 to clients. Events have the first bit set high. * */ -#define I3_IPC_EVENT_MASK (1 << 31) +#define I3_IPC_EVENT_MASK (1UL << 31) /* The workspace event will be triggered upon changes in the workspace list */ #define I3_IPC_EVENT_WORKSPACE (I3_IPC_EVENT_MASK | 0) diff --git a/libi3/get_colorpixel.c b/libi3/get_colorpixel.c index afba202b..49a9e3b4 100644 --- a/libi3/get_colorpixel.c +++ b/libi3/get_colorpixel.c @@ -43,7 +43,7 @@ uint32_t get_colorpixel(const char *hex) { /* Shortcut: if our screen is true color, no need to do a roundtrip to X11 */ if (root_screen == NULL || root_screen->root_depth == 24 || root_screen->root_depth == 32) { - return (0xFF << 24) | (r << 16 | g << 8 | b); + return (0xFFUL << 24) | (r << 16 | g << 8 | b); } /* Lookup this colorpixel in the cache */ -- 2.39.2