From 1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Tue, 19 Oct 2021 17:28:18 +0200 Subject: passt: Address gcc 11 warnings A mix of unchecked return values, a missing permission mask for open(2) with O_CREAT, and some false positives from -Wstringop-overflow and -Wmaybe-uninitialized. Reported-by: Martin Hauke Signed-off-by: Stefano Brivio --- pcap.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'pcap.c') diff --git a/pcap.c b/pcap.c index 9cf585f..8ff1636 100644 --- a/pcap.c +++ b/pcap.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -87,8 +88,8 @@ void pcap(char *pkt, size_t len) h.tv_usec = tv.tv_usec; h.caplen = h.len = len; - write(pcap_fd, &h, sizeof(h)); - write(pcap_fd, pkt, len); + if (write(pcap_fd, &h, sizeof(h)) < 0 || write(pcap_fd, pkt, len) < 0) + debug("Cannot log packet, length %u", len); } /** @@ -98,6 +99,7 @@ void pcap(char *pkt, size_t len) void pcapm(struct msghdr *mh) { struct pcap_pkthdr h; + struct iovec *iov; struct timeval tv; unsigned int i; @@ -109,13 +111,19 @@ void pcapm(struct msghdr *mh) h.tv_usec = tv.tv_usec; for (i = 0; i < mh->msg_iovlen; i++) { - struct iovec *iov = &mh->msg_iov[i]; + iov = &mh->msg_iov[i]; h.caplen = h.len = iov->iov_len - 4; - write(pcap_fd, &h, sizeof(h)); - write(pcap_fd, (char *)iov->iov_base + 4, iov->iov_len - 4); + if (write(pcap_fd, &h, sizeof(h)) < 0) + goto fail; + if (write(pcap_fd, (char *)iov->iov_base + 4, iov->iov_len - 4)) + goto fail; } + + return; +fail: + debug("Cannot log packet, length %u", iov->iov_len - 4); } /** @@ -125,6 +133,7 @@ void pcapm(struct msghdr *mh) void pcapmm(struct mmsghdr *mmh, unsigned int vlen) { struct pcap_pkthdr h; + struct iovec *iov; struct timeval tv; unsigned int i, j; @@ -139,15 +148,20 @@ void pcapmm(struct mmsghdr *mmh, unsigned int vlen) struct msghdr *mh = &mmh[i].msg_hdr; for (j = 0; j < mh->msg_iovlen; j++) { - struct iovec *iov = &mh->msg_iov[j]; + iov = &mh->msg_iov[j]; h.caplen = h.len = iov->iov_len - 4; - write(pcap_fd, &h, sizeof(h)); - write(pcap_fd, (char *)iov->iov_base + 4, - iov->iov_len - 4); + if (write(pcap_fd, &h, sizeof(h)) < 0) + goto fail; + if (write(pcap_fd, (char *)iov->iov_base + 4, + iov->iov_len - 4) < 0) + goto fail; } } + return; +fail: + debug("Cannot log packet, length %u", iov->iov_len - 4); } /** @@ -194,5 +208,6 @@ void pcap_init(struct ctx *c, int index) info("Saving packet capture at %s", c->pcap); - write(pcap_fd, &pcap_hdr, sizeof(pcap_hdr)); + if (write(pcap_fd, &pcap_hdr, sizeof(pcap_hdr)) < 0) + warn("Cannot write PCAP header: %s", strerror(errno)); } -- cgit v1.2.3