Skip to content

Commit

Permalink
libpgagroal: utils.c: fix UB in pgagroal_read_int32()
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
decarv committed Jan 17, 2025
1 parent 92ac986 commit 1483d2c
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/libpgagroal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ pgagroal_read_int32(void* data)
*((unsigned char*)(data + 2)),
*((unsigned char*)(data + 3))};

int32_t res = (int32_t)((bytes[0] << 24)) |
((bytes[1] << 16)) |
((bytes[2] << 8)) |
((bytes[3]));
int32_t res = (int32_t)(((uint32_t)bytes[0] << 24)) |
(((uint32_t)bytes[1] << 16)) |
(((uint32_t)bytes[2] << 8)) |
(((uint32_t)bytes[3]));

return res;
}
Expand Down

0 comments on commit 1483d2c

Please sign in to comment.