PNM loader: avoid some undefined behavior

`ptr` is a `uint8_t` pointer, which will get promoted to an `int` due to
integer promotion. and shifting a signed int by 24 places could cause
signed overflow which is undefined.

> If E1 has a signed type and nonnegative value, and E1 x 2^E2 is
> representable in the result type, then that is the resulting value;
> otherwise, the behavior is undefined.

ref: https://port70.net/~nsz/c/c99/n1256.html#6.5.7p4
This commit is contained in:
NRK 2023-01-13 12:40:25 +06:00 committed by Kim Woelders
parent 0f27c5e6ec
commit e76d9bcf9f
1 changed files with 5 additions and 5 deletions

View File

@ -9,8 +9,8 @@
static const char *const _formats[] = { "pnm", "ppm", "pgm", "pbm", "pam" };
typedef enum
{ BW_RAW_PACKED, BW_RAW, BW_PLAIN, GRAY_RAW, GRAY_PLAIN, RGB_RAW, RGB_PLAIN,
typedef enum {
BW_RAW_PACKED, BW_RAW, BW_PLAIN, GRAY_RAW, GRAY_PLAIN, RGB_RAW, RGB_PLAIN,
XV332
} px_type;
@ -513,8 +513,8 @@ _load(ImlibImage * im, int load_data)
for (x = 0; x < w; x++)
{
*ptr2 =
(ptr[1] << 24) | (ptr[0] << 16) | (ptr[0] << 8) |
ptr[0];
((uint32_t)ptr[1] << 24) | (ptr[0] << 16) |
(ptr[0] << 8) | ptr[0];
ptr2++;
ptr += 2;
}
@ -524,7 +524,7 @@ _load(ImlibImage * im, int load_data)
for (x = 0; x < w; x++)
{
*ptr2 =
(((ptr[1] * 255) / v) << 24) |
((uint32_t)((ptr[1] * 255) / v) << 24) |
(((ptr[0] * 255) / v) << 16) |
(((ptr[0] * 255) / v) << 8) | ((ptr[0] * 255) /
v);