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 <alyssa@rosenzweig.io>
Signed-off-by: Juan A. Suarez Romero <jasuarez@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/29772>
This commit is contained in:
Juan A. Suarez Romero 2024-06-18 17:24:34 +02:00 committed by Marge Bot
parent d4dcbaf825
commit a407285ff2
4 changed files with 35 additions and 13 deletions

View file

@ -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

View file

@ -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;

View file

@ -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) {

View file

@ -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: