passt: Address new clang-tidy warnings from LLVM 13.0.1

clang-tidy from LLVM 13.0.1 reports some new warnings from these
checkers:

- altera-unroll-loops, altera-id-dependent-backward-branch: ignore
  for the moment being, add a TODO item

- bugprone-easily-swappable-parameters: ignore, nothing to do about
  those

- readability-function-cognitive-complexity: ignore for the moment
  being, add a TODO item

- altera-struct-pack-align: ignore, alignment is forced in protocol
  headers

- concurrency-mt-unsafe: ignore for the moment being, add a TODO
  item

Fix bugprone-implicit-widening-of-multiplication-result warnings,
though, that's doable and they seem to make sense.

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
Stefano Brivio 2022-01-30 02:59:12 +01:00
parent 20d271b226
commit 292c185553
9 changed files with 52 additions and 25 deletions

View file

@ -167,6 +167,23 @@ pkgs:
#
# - bugprone-suspicious-string-compare
# Return value of memcmp(), not really suspicious
#
# - altera-unroll-loops
# - altera-id-dependent-backward-branch
# TODO: check paths where it might make sense to improve performance
#
# - bugprone-easily-swappable-parameters
# Not much can be done about them other than being careful
#
# - readability-function-cognitive-complexity
# TODO: split reported functions
#
# - altera-struct-pack-align
# "Poor" alignment needed for structs reflecting message formats/headers
#
# - concurrency-mt-unsafe
# TODO: check again if multithreading is implemented
clang-tidy: $(wildcard *.c) $(wildcard *.h)
clang-tidy -checks=*,-modernize-*,\
-clang-analyzer-valist.Uninitialized,\
@ -187,7 +204,12 @@ clang-tidy: $(wildcard *.c) $(wildcard *.h)
-bugprone-narrowing-conversions,\
-cppcoreguidelines-narrowing-conversions,\
-cppcoreguidelines-avoid-non-const-global-variables,\
-bugprone-suspicious-string-compare \
-bugprone-suspicious-string-compare,\
-altera-unroll-loops,-altera-id-dependent-backward-branch,\
-bugprone-easily-swappable-parameters,\
-readability-function-cognitive-complexity,\
-altera-struct-pack-align,\
-concurrency-mt-unsafe \
--warnings-as-errors=* $(wildcard *.c) -- $(CFLAGS)
ifeq ($(shell $(CC) -v 2>&1 | grep -c "gcc version"),1)

View file

@ -108,10 +108,13 @@ uint16_t csum_unaligned(const void *buf, size_t len, uint32_t init)
*/
void csum_tcp4(struct iphdr *iph)
{
struct tcphdr *th = (struct tcphdr *)((char *)iph + iph->ihl * 4);
uint16_t tlen = ntohs(iph->tot_len) - iph->ihl * 4, *p = (uint16_t *)th;
uint16_t tlen = ntohs(iph->tot_len) - iph->ihl * 4, *p;
struct tcphdr *th;
uint32_t sum = 0;
th = (struct tcphdr *)((char *)iph + (intptr_t)(iph->ihl * 4));
p = (uint16_t *)th;
sum += (iph->saddr >> 16) & 0xffff;
sum += iph->saddr & 0xffff;
sum += (iph->daddr >> 16) & 0xffff;

4
conf.c
View file

@ -867,7 +867,7 @@ void conf(struct ctx *c, int argc, char **argv)
for (i = 0; i < ETH_ALEN; i++) {
errno = 0;
b = strtol(optarg + i * 3, NULL, 16);
b = strtol(optarg + (intptr_t)i * 3, NULL, 16);
if (b < 0 || b > UCHAR_MAX || errno) {
err("Invalid MAC address: %s", optarg);
usage(argv[0]);
@ -1032,7 +1032,7 @@ void conf(struct ctx *c, int argc, char **argv)
case 'M':
for (i = 0; i < ETH_ALEN; i++) {
errno = 0;
b = strtol(optarg + i * 3, NULL, 16);
b = strtol(optarg + (intptr_t)i * 3, NULL, 16);
if (b < 0 || b > UCHAR_MAX || errno) {
err("Invalid MAC address: %s", optarg);
usage(argv[0]);

8
dhcp.c
View file

@ -271,10 +271,10 @@ int dhcp(struct ctx *c, struct ethhdr *eh, size_t len)
if (len < sizeof(*eh) + sizeof(*iph))
return 0;
if (len < sizeof(*eh) + iph->ihl * 4 + sizeof(*uh))
if (len < sizeof(*eh) + (long)iph->ihl * 4 + sizeof(*uh))
return 0;
uh = (struct udphdr *)((char *)iph + iph->ihl * 4);
uh = (struct udphdr *)((char *)iph + (long)(iph->ihl * 4));
m = (struct msg *)(uh + 1);
if (uh->dest != htons(67))
@ -283,7 +283,7 @@ int dhcp(struct ctx *c, struct ethhdr *eh, size_t len)
if (c->no_dhcp)
return 1;
mlen = len - sizeof(*eh) - iph->ihl * 4 - sizeof(*uh);
mlen = len - sizeof(*eh) - (long)iph->ihl * 4 - sizeof(*uh);
if (mlen != ntohs(uh->len) - sizeof(*uh) ||
mlen < offsetof(struct msg, o) ||
m->op != BOOTREQUEST)
@ -349,7 +349,7 @@ int dhcp(struct ctx *c, struct ethhdr *eh, size_t len)
iph->daddr = c->addr4;
iph->saddr = c->gw4;
iph->check = 0;
iph->check = csum_unaligned(iph, iph->ihl * 4, 0);
iph->check = csum_unaligned(iph, (intptr_t)(iph->ihl * 4), 0);
len += sizeof(*eh);
memcpy(eh->h_dest, eh->h_source, ETH_ALEN);

2
qrap.c
View file

@ -111,7 +111,7 @@ void usage(const char *name)
*/
int main(int argc, char **argv)
{
struct timeval tv = { .tv_sec = 0, .tv_usec = 500 * 1000 };
struct timeval tv = { .tv_sec = 0, .tv_usec = (long)(500 * 1000) };
int i, s, qemu_argc = 0, addr_map = 0, has_dev = 0;
char *qemu_argv[ARG_MAX], dev_str[ARG_MAX];
struct sockaddr_un addr = {

8
tap.c
View file

@ -131,7 +131,7 @@ void tap_ip_send(struct ctx *c, struct in6_addr *src, uint8_t proto,
memcpy(&iph->saddr, &src->s6_addr[12], 4);
iph->check = 0;
iph->check = csum_unaligned(iph, iph->ihl * 4, 0);
iph->check = csum_unaligned(iph, (size_t)iph->ihl * 4, 0);
memcpy(data, in, len);
@ -337,9 +337,9 @@ resume:
continue;
iph = (struct iphdr *)(eh + 1);
if ((iph->ihl * 4) + sizeof(*eh) > len)
if ((size_t)iph->ihl * 4 + sizeof(*eh) > len)
continue;
if (iph->ihl * 4 < (int)sizeof(*iph))
if ((size_t)iph->ihl * 4 < (int)sizeof(*iph))
continue;
if (iph->saddr && c->addr4_seen != iph->saddr) {
@ -347,7 +347,7 @@ resume:
proto_update_l2_buf(NULL, NULL, &c->addr4_seen);
}
l4h = (char *)iph + iph->ihl * 4;
l4h = (char *)iph + (size_t)iph->ihl * 4;
l4_len = len - ((intptr_t)l4h - (intptr_t)eh);
if (iph->protocol == IPPROTO_ICMP) {

10
tcp.c
View file

@ -345,7 +345,7 @@
#define TCP_TAP_FRAMES 256
#define MAX_PIPE_SIZE (2 * 1024 * 1024)
#define MAX_PIPE_SIZE (2UL * 1024 * 1024)
#define TCP_HASH_TABLE_LOAD 70 /* % */
#define TCP_HASH_TABLE_SIZE (MAX_TAP_CONNS * 100 / \
@ -1040,8 +1040,8 @@ static int tcp_opt_get(struct tcphdr *th, size_t len, uint8_t type_search,
uint8_t type, optlen;
char *p;
if (len > (unsigned)th->doff * 4)
len = th->doff * 4;
if (len > (size_t)th->doff * 4)
len = (size_t)th->doff * 4;
len -= sizeof(*th);
p = (char *)(th + 1);
@ -1568,7 +1568,7 @@ static int tcp_update_seqack_wnd(struct ctx *c, struct tcp_tap_conn *conn,
#else
if (conn->state > ESTABLISHED || (flags & (DUP_ACK | FORCE_ACK)) ||
conn->local || tcp_rtt_dst_low(conn) ||
conn->snd_buf < SNDBUF_SMALL) {
(unsigned long)conn->snd_buf < SNDBUF_SMALL) {
conn->seq_ack_to_tap = conn->seq_from_tap;
} else if (conn->seq_ack_to_tap != conn->seq_from_tap) {
if (!tinfo) {
@ -2393,7 +2393,7 @@ static void tcp_data_from_tap(struct ctx *c, struct tcp_tap_conn *conn,
return;
}
off = th->doff * 4;
off = (size_t)th->doff * 4;
if (off < sizeof(*th) || off > len) {
tcp_rst(c, conn);
return;

8
util.c
View file

@ -51,7 +51,7 @@ void name(const char *format, ...) { \
clock_gettime(CLOCK_REALTIME, &tp); \
fprintf(stderr, "%li.%04li: ", \
tp.tv_sec - log_debug_start, \
tp.tv_nsec / (100 * 1000)); \
tp.tv_nsec / (100L * 1000)); \
} else { \
va_start(args, format); \
passt_vsyslog(level, format, args); \
@ -305,12 +305,14 @@ void sock_probe_mem(struct ctx *c)
sl = sizeof(v);
if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &v, sizeof(v)) ||
getsockopt(s, SOL_SOCKET, SO_SNDBUF, &v, &sl) || v < SNDBUF_BIG)
getsockopt(s, SOL_SOCKET, SO_SNDBUF, &v, &sl) ||
(size_t)v < SNDBUF_BIG)
c->low_wmem = 1;
v = INT_MAX / 2;
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &v, sizeof(v)) ||
getsockopt(s, SOL_SOCKET, SO_RCVBUF, &v, &sl) || v < RCVBUF_BIG)
getsockopt(s, SOL_SOCKET, SO_RCVBUF, &v, &sl) ||
(size_t)v < RCVBUF_BIG)
c->low_rmem = 1;
close(s);

6
util.h
View file

@ -144,9 +144,9 @@ enum {
.daddr = IN6ADDR_ANY_INIT, \
}
#define RCVBUF_BIG (2 * 1024 * 1024)
#define SNDBUF_BIG (4 * 1024 * 1024)
#define SNDBUF_SMALL (128 * 1024)
#define RCVBUF_BIG (2UL * 1024 * 1024)
#define SNDBUF_BIG (4UL * 1024 * 1024)
#define SNDBUF_SMALL (128UL * 1024)
#include <net/if.h>
#include <limits.h>