Skip to content

Commit 1483d2c

Browse files
committed
libpgagroal: utils.c: fix UB in pgagroal_read_int32()
In pgagroal_read_int32, the byte[i] value is promoted to int32. According to the C spec, if you cannot represent the result in the result type, then that behavior is undefined (6.5.7). This issue is fixed by explicitly casting the bytes[i] values to uint32_t. Signed-off-by: Henrique de Carvalho <decarv.henrique@gmail.com>
1 parent 92ac986 commit 1483d2c

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/libpgagroal/utils.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,10 @@ pgagroal_read_int32(void* data)
316316
*((unsigned char*)(data + 2)),
317317
*((unsigned char*)(data + 3))};
318318

319-
int32_t res = (int32_t)((bytes[0] << 24)) |
320-
((bytes[1] << 16)) |
321-
((bytes[2] << 8)) |
322-
((bytes[3]));
319+
int32_t res = (int32_t)(((uint32_t)bytes[0] << 24)) |
320+
(((uint32_t)bytes[1] << 16)) |
321+
(((uint32_t)bytes[2] << 8)) |
322+
(((uint32_t)bytes[3]));
323323

324324
return res;
325325
}

0 commit comments

Comments
 (0)