From a407285ff2d6a51d48b686d143d92a1a98a31e9e Mon Sep 17 00:00:00 2001 From: "Juan A. Suarez Romero" Date: Tue, 18 Jun 2024 17:24:34 +0200 Subject: [PATCH] util: use unsigned types when performing bitshift Ensure unsigned integers are used instead of signed ones when performing left bit shifts. This has been detected by the Undefined Behaviour Sanitizer (UBSan). Reviewed-by: Alyssa Rosenzweig Signed-off-by: Juan A. Suarez Romero Part-of: --- src/util/format/texcompress_etc_tmp.h | 5 +++- src/util/format_rgb9e5.h | 3 ++- src/util/u_idalloc.c | 2 +- src/util/u_pack_color.h | 38 ++++++++++++++++++++------- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/util/format/texcompress_etc_tmp.h b/src/util/format/texcompress_etc_tmp.h index 5497566324f..26fa9593bcb 100644 --- a/src/util/format/texcompress_etc_tmp.h +++ b/src/util/format/texcompress_etc_tmp.h @@ -109,7 +109,10 @@ TAG(etc1_parse_block)(struct TAG(etc1_block) *block, const UINT8_TYPE *src) block->flipped = (src[3] & 0x1); block->pixel_indices = - (src[4] << 24) | (src[5] << 16) | (src[6] << 8) | src[7]; + ((uint32_t) src[4] << 24) | + ((uint32_t) src[5] << 16) | + ((uint32_t) src[6] << 8) | + (uint32_t) src[7]; } static void diff --git a/src/util/format_rgb9e5.h b/src/util/format_rgb9e5.h index 51debd276e0..1b747b3ec76 100644 --- a/src/util/format_rgb9e5.h +++ b/src/util/format_rgb9e5.h @@ -58,7 +58,8 @@ static inline int rgb9e5_ClampRange(float x) static inline uint32_t float3_to_rgb9e5(const float rgb[3]) { - int rm, gm, bm, exp_shared; + int rm, gm, bm; + uint32_t exp_shared; uint32_t revdenom_biasedexp; union { float f; uint32_t u; } rc, bc, gc, maxrgb, revdenom; diff --git a/src/util/u_idalloc.c b/src/util/u_idalloc.c index 2149e09e86b..9cae590d3e0 100644 --- a/src/util/u_idalloc.c +++ b/src/util/u_idalloc.c @@ -154,7 +154,7 @@ util_idalloc_free(struct util_idalloc *buf, unsigned id) return; buf->lowest_free_idx = MIN2(idx, buf->lowest_free_idx); - buf->data[idx] &= ~(1 << (id % 32)); + buf->data[idx] &= ~(1u << (id % 32)); /* Decrease num_used to the last used element + 1. */ if (buf->num_set_elements == idx + 1) { diff --git a/src/util/u_pack_color.h b/src/util/u_pack_color.h index bd9b5c32eaa..7d5bf7f3545 100644 --- a/src/util/u_pack_color.h +++ b/src/util/u_pack_color.h @@ -348,55 +348,71 @@ util_pack_color(const float rgba[4], enum pipe_format format, union util_color * a = float_to_ubyte(rgba[3]); } +#define PACK32(r,g,b,a) (((uint32_t)(r) << 24) | \ + ((uint32_t)(g) << 16) | \ + ((uint32_t)(b) << 8) | \ + (uint32_t)(a)) + switch (format) { case PIPE_FORMAT_ABGR8888_UNORM: { - uc->ui[0] = (r << 24) | (g << 16) | (b << 8) | a; + uc->ui[0] = PACK32(r, g, b, a); } return; case PIPE_FORMAT_XBGR8888_UNORM: { - uc->ui[0] = (r << 24) | (g << 16) | (b << 8) | 0xff; + uc->ui[0] = PACK32(r, g, b, 0xff); } return; case PIPE_FORMAT_BGRA8888_UNORM: { - uc->ui[0] = (a << 24) | (r << 16) | (g << 8) | b; + uc->ui[0] = PACK32(a, r, g, b); } return; case PIPE_FORMAT_BGRX8888_UNORM: { - uc->ui[0] = (0xffu << 24) | (r << 16) | (g << 8) | b; + uc->ui[0] = PACK32(0xff, r, g, b); } return; case PIPE_FORMAT_ARGB8888_UNORM: { - uc->ui[0] = (b << 24) | (g << 16) | (r << 8) | a; + uc->ui[0] = PACK32(b, g, r, a); } return; case PIPE_FORMAT_XRGB8888_UNORM: { - uc->ui[0] = (b << 24) | (g << 16) | (r << 8) | 0xff; + uc->ui[0] = PACK32(b, g, r, 0xff); } return; case PIPE_FORMAT_B5G6R5_UNORM: { - uc->us = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3); + uc->us = (((uint16_t)r & 0xf8) << 8) | + (((uint16_t)g & 0xfc) << 3) | + ((uint16_t)b >> 3); } return; case PIPE_FORMAT_B5G5R5X1_UNORM: { - uc->us = ((0x80) << 8) | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3); + uc->us = ((uint16_t)0x80 << 8) | + (((uint16_t)r & 0xf8) << 7) | + (((uint16_t)g & 0xf8) << 2) | + ((uint16_t)b >> 3); } return; case PIPE_FORMAT_B5G5R5A1_UNORM: { - uc->us = ((a & 0x80) << 8) | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3); + uc->us = (((uint16_t)a & 0x80) << 8) | + (((uint16_t)r & 0xf8) << 7) | + (((uint16_t)g & 0xf8) << 2) | + ((uint16_t)b >> 3); } return; case PIPE_FORMAT_B4G4R4A4_UNORM: { - uc->us = ((a & 0xf0) << 8) | ((r & 0xf0) << 4) | ((g & 0xf0) << 0) | (b >> 4); + uc->us = (((uint16_t)a & 0xf0) << 8) | + (((uint16_t)r & 0xf0) << 4) | + (((uint16_t)g & 0xf0) << 0) | + ((uint16_t)b >> 4); } return; case PIPE_FORMAT_A8_UNORM: @@ -426,6 +442,8 @@ util_pack_color(const float rgba[4], enum pipe_format format, union util_color * } return; +#undef PACK32 + /* Handle other cases with a generic function. */ default: