cleanup: fix UB (left shift promotes to int)

This commit is contained in:
bnnm 2021-10-24 10:28:20 +02:00
parent 7847abacce
commit 3c8dc615ab
3 changed files with 8 additions and 8 deletions

View File

@ -206,7 +206,7 @@ static uint32_t read_ubits(uint8_t bits, uint32_t offset, uint8_t* buf) {
shift = offset - 8 * (offset / 8);
mask = (1 << bits) - 1;
pos = offset / 8;
val = (buf[pos+0]) | (buf[pos+1]<<8) | (buf[pos+2]<<16) | (buf[pos+3]<<24);
val = ((uint32_t)buf[pos+0]) | ((uint32_t)buf[pos+1]<<8) | ((uint32_t)buf[pos+2]<<16) | ((uint32_t)buf[pos+3]<<24);
return (val >> shift) & mask;
}

View File

@ -1037,15 +1037,15 @@ static uint16_t crc16(const uint8_t* data, int length) {
/* ************************************************************************* */
static uint32_t get_u32be(const uint8_t* mem) {
return (mem[0] << 24) | (mem[1] << 16) | (mem[2] << 8) | mem[3];
return ((uint32_t)mem[0] << 24) | ((uint32_t)mem[1] << 16) | ((uint32_t)mem[2] << 8) | (uint32_t)mem[3];
}
static uint32_t get_u32le(const uint8_t* mem) {
return (mem[3] << 24) | (mem[2] << 16) | (mem[1] << 8) | mem[0];
return ((uint32_t)mem[3] << 24) | ((uint32_t)mem[2] << 16) | ((uint32_t)mem[1] << 8) | (uint32_t)mem[0];
}
static uint16_t get_u16le(const uint8_t* mem) {
return (mem[1] << 8) | mem[0];
return ((uint16_t)mem[1] << 8) | (uint16_t)mem[0];
}
static int init_header(tac_header_t* header, const uint8_t* buf) {

View File

@ -12,19 +12,19 @@
/* host endian independent multi-byte integer reading */
static inline int16_t get_16bitBE(const uint8_t* p) {
return (p[0]<<8) | (p[1]);
return ((uint16_t)p[0]<<8) | ((uint16_t)p[1]);
}
static inline int16_t get_16bitLE(const uint8_t* p) {
return (p[0]) | (p[1]<<8);
return ((uint16_t)p[0]) | ((uint16_t)p[1]<<8);
}
static inline int32_t get_32bitBE(const uint8_t* p) {
return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | (p[3]);
return ((uint32_t)p[0]<<24) | ((uint32_t)p[1]<<16) | ((uint32_t)p[2]<<8) | ((uint32_t)p[3]);
}
static inline int32_t get_32bitLE(const uint8_t* p) {
return (p[0]) | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
return ((uint32_t)p[0]) | ((uint32_t)p[1]<<8) | ((uint32_t)p[2]<<16) | ((uint32_t)p[3]<<24);
}
static inline int64_t get_64bitBE(const uint8_t* p) {