From caa22aa644cb56e6fda77ff80c62d038653f3cf9 Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Tue, 25 Jan 2022 20:21:18 +0100 Subject: tcp, udp, util: Fixes for bitmap handling on big-endian, casts Bitmap manipulating functions would otherwise refer to inconsistent sets of bits on big-endian architectures. While at it, fix up a couple of casts. Signed-off-by: Stefano Brivio --- passt.h | 3 ++- tcp.c | 2 +- udp.c | 4 ++-- util.c | 12 +++++++++--- util.h | 2 ++ 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/passt.h b/passt.h index ae3035f..0ef1897 100644 --- a/passt.h +++ b/passt.h @@ -67,7 +67,8 @@ extern char pkt_buf [PKT_BUF_BYTES]; extern char *ip_proto_str[]; #define IP_PROTO_STR(n) \ - (((n) <= IPPROTO_SCTP && ip_proto_str[(n)]) ? ip_proto_str[(n)] : "?") + (((uint8_t)(n) <= IPPROTO_SCTP && ip_proto_str[(n)]) ? \ + ip_proto_str[(n)] : "?") #include /* For MAXNS below */ diff --git a/tcp.c b/tcp.c index 839bf30..18f07b6 100644 --- a/tcp.c +++ b/tcp.c @@ -2518,7 +2518,7 @@ eintr: } - if (n < (seq_from_tap - conn->seq_from_tap)) { + if (n < (int)(seq_from_tap - conn->seq_from_tap)) { partial_send = 1; conn->seq_from_tap += n; tcp_send_to_tap(c, conn, 0, now); diff --git a/udp.c b/udp.c index 15e0c96..e1a9ecb 100644 --- a/udp.c +++ b/udp.c @@ -684,7 +684,7 @@ void udp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, cur_mh->msg_iov = &udp6_l2_iov_tap[0]; msg_i = msglen = iov_in_msg = 0; - for (i = 0; i < n; i++) { + for (i = 0; i < (unsigned)n; i++) { struct udp6_l2_buf_t *b = &udp6_l2_buf[i]; size_t ip_len, iov_len; @@ -770,7 +770,7 @@ void udp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, cur_mh->msg_iov = &udp4_l2_iov_tap[0]; msg_i = msglen = iov_in_msg = 0; - for (i = 0; i < n; i++) { + for (i = 0; i < (unsigned)n; i++) { struct udp4_l2_buf_t *b = &udp4_l2_buf[i]; size_t ip_len, iov_len; in_addr_t s_addr; diff --git a/util.c b/util.c index 7a3ea51..46a539b 100644 --- a/util.c +++ b/util.c @@ -342,7 +342,9 @@ int timespec_diff_ms(struct timespec *a, struct timespec *b) */ void bitmap_set(uint8_t *map, int bit) { - map[bit / 8] |= 1 << (bit % 8); + unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); + + *word |= BITMAP_BIT(bit); } /** @@ -352,7 +354,9 @@ void bitmap_set(uint8_t *map, int bit) */ void bitmap_clear(uint8_t *map, int bit) { - map[bit / 8] &= ~(1 << (bit % 8)); + unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); + + *word &= ~BITMAP_BIT(bit); } /** @@ -364,7 +368,9 @@ void bitmap_clear(uint8_t *map, int bit) */ int bitmap_isset(const uint8_t *map, int bit) { - return map[bit / 8] & (1 << bit % 8); + unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); + + return *word & BITMAP_BIT(bit); } /** diff --git a/util.h b/util.h index 63cd8f9..800a91d 100644 --- a/util.h +++ b/util.h @@ -43,6 +43,8 @@ void debug(const char *format, ...); #define ROUND_DOWN(x, y) ((x) & ~((y) - 1)) #define ROUND_UP(x, y) (((x) + (y) - 1) & ~((y) - 1)) +#define BITMAP_BIT(n) (1UL << (n) % (sizeof(long) * 8)) +#define BITMAP_WORD(n) (n / (sizeof(long) * 8)) #define SWAP(a, b) \ do { \ -- cgit v1.2.3