aboutsummaryrefslogtreecommitdiff
path: root/pcap-linux.c
diff options
context:
space:
mode:
authorGuy Harris <gharris@sonic.net>2020-06-02 23:23:54 -0700
committerGuy Harris <gharris@sonic.net>2020-06-02 23:23:54 -0700
commitf4fcc9396dc425399846cf082f9ed1056b81dd11 (patch)
treed45610a31210285fb3b2e4f539be52a95b8e27f2 /pcap-linux.c
parent3af96ce3753c6ed1490a02a5c62286ae760ba7f6 (diff)
linux: test whether reading from the event FD succeeds.
It *should* never succeed, but 1) you never know and 2) that gets newer versions of GCC, and Coverity, to stop whining that we're not checking the return value of read(). (We can't check of the write() to the event FD without adding a new API that's like pcap_breakloop() but that 1) returns a success/error indication and 2) provides an error message.)
Diffstat (limited to 'pcap-linux.c')
-rw-r--r--pcap-linux.c39
1 files changed, 38 insertions, 1 deletions
diff --git a/pcap-linux.c b/pcap-linux.c
index 6e1d89b6..546d0369 100644
--- a/pcap-linux.c
+++ b/pcap-linux.c
@@ -3890,9 +3890,46 @@ static int pcap_wait_for_frames_mmap(pcap_t *handle)
* Now check the event device.
*/
if (pollinfo[1].revents & POLLIN) {
+ ssize_t nread;
uint64_t value;
- (void)read(handlep->poll_breakloop_fd, &value,
+
+ /*
+ * This should never fail, but, just
+ * in case....
+ */
+ nread = read(handlep->poll_breakloop_fd, &value,
sizeof(value));
+ if (nread == -1) {
+ pcap_fmt_errmsg_for_errno(handle->errbuf,
+ PCAP_ERRBUF_SIZE,
+ errno,
+ "Error reading from event FD");
+ return PCAP_ERROR;
+ }
+
+ /*
+ * According to the Linux read(2) man
+ * page, read() will transfer at most
+ * 2^31-1 bytes, so the return value is
+ * either -1 or a value between 0
+ * and 2^31-1, so it's non-negative.
+ *
+ * Cast it to size_t to squelch
+ * warnings from the compiler; add this
+ * comment to squelch warnings from
+ * humans reading the code. :-)
+ *
+ * Don't treat an EOF as an error, but
+ * *do* treat a short read as an error;
+ * that "shouldn't happen", but....
+ */
+ if (nread != 0 &&
+ (size_t)nread < sizeof(value)) {
+ snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
+ "Short read from event FD: expected %zu, got %zd",
+ sizeof(value), nread);
+ return PCAP_ERROR;
+ }
/*
* This event gets signaled by a