diff options
author | Stefano Brivio <sbrivio@redhat.com> | 2021-10-19 17:28:18 +0200 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2021-10-20 08:29:30 +0200 |
commit | 1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc (patch) | |
tree | 13ee3b6da82e5eb53a89bb881ccf3a7092dab9fe /pcap.c | |
parent | 087b5f4dbb9e3f767a8afbb6c1001c509965940b (diff) | |
download | passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.tar passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.tar.gz passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.tar.bz2 passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.tar.lz passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.tar.xz passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.tar.zst passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.zip |
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 <mardnh@gmx.de>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'pcap.c')
-rw-r--r-- | pcap.c | 35 |
1 files changed, 25 insertions, 10 deletions
@@ -23,6 +23,7 @@ #include <sys/stat.h> #include <fcntl.h> #include <time.h> +#include <errno.h> #include <net/ethernet.h> #include <netinet/in.h> #include <unistd.h> @@ -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)); } |