warnings: try to prevent warnings due to automatic integer promotion

pull/623/head
Brad House 1 year ago
parent 4245fdb1ef
commit ee11e7a07e
  1. 14
      src/lib/ares__buf.c

@ -523,20 +523,16 @@ ares_status_t ares__buf_fetch_be16(ares__buf_t *buf, unsigned short *u16)
{ {
size_t remaining_len; size_t remaining_len;
const unsigned char *ptr = ares__buf_fetch(buf, &remaining_len); const unsigned char *ptr = ares__buf_fetch(buf, &remaining_len);
unsigned short high_u16; unsigned int u32;
unsigned short low_u16;
if (buf == NULL || u16 == NULL || remaining_len < sizeof(*u16)) { if (buf == NULL || u16 == NULL || remaining_len < sizeof(*u16)) {
return ARES_EBADRESP; return ARES_EBADRESP;
} }
/* Bend over backwards to prevent analysis warnings */ /* Do math in an unsigned int in order to prevent warnings due to automatic
high_u16 = ptr[0]; * conversion by the compiler from short to int during shifts */
low_u16 = ptr[1]; u32 = ((unsigned int)(ptr[0]) << 8 | (unsigned int)ptr[1]);
*u16 = (unsigned short)(u32 & 0xFFFF);
*u16 = 0;
*u16 |= (unsigned short)((high_u16 << 8) & 0xFF00);
*u16 |= (unsigned short)(low_u16 & 0xFF);
return ares__buf_consume(buf, sizeof(*u16)); return ares__buf_consume(buf, sizeof(*u16));
} }

Loading…
Cancel
Save