From: Guy Harris Date: Sun, 9 Aug 2015 23:01:40 +0000 (-0700) Subject: Make the buffer member of a pcap_t a void *. X-Git-Tag: libpcap-1.8.0-bp~268 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/b0f028907bac3a1b4cbabd0faade2a72e0753ee8 Make the buffer member of a pcap_t a void *. Yes, in some sense, it's an array of bytes - on modern processors, *all* data is ultimately an array of bytes - but different modules will use it in different ways, not all of which will be an undifferentiated array of bytes. This squelches a complaint from the Clang static analyzer. Clean up some code while we're at it. --- diff --git a/dlpisubs.c b/dlpisubs.c index 131fa279..fd2d61c0 100644 --- a/dlpisubs.c +++ b/dlpisubs.c @@ -326,7 +326,7 @@ int pcap_alloc_databuf(pcap_t *p) { p->bufsize = PKTBUFSIZE; - p->buffer = (u_char *)malloc(p->bufsize + p->offset); + p->buffer = malloc(p->bufsize + p->offset); if (p->buffer == NULL) { strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); return (-1); diff --git a/pcap-bpf.c b/pcap-bpf.c index 98140f22..5b863b2b 100644 --- a/pcap-bpf.c +++ b/pcap-bpf.c @@ -866,7 +866,7 @@ pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user) } else #endif { - cc = read(p->fd, (char *)p->buffer, p->bufsize); + cc = read(p->fd, p->buffer, p->bufsize); } if (cc < 0) { /* Don't choke when we get ptraced */ @@ -937,7 +937,7 @@ pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user) pcap_strerror(errno)); return (PCAP_ERROR); } - bp = p->buffer; + bp = (u_char *)p->buffer; } else bp = p->bp; @@ -2209,7 +2209,7 @@ pcap_activate_bpf(pcap_t *p) #ifdef HAVE_ZEROCOPY_BPF if (!pb->zerocopy) { #endif - p->buffer = (u_char *)malloc(p->bufsize); + p->buffer = malloc(p->bufsize); if (p->buffer == NULL) { snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); diff --git a/pcap-bt-linux.c b/pcap-bt-linux.c index 56df6876..ebdf1249 100644 --- a/pcap-bt-linux.c +++ b/pcap-bt-linux.c @@ -199,8 +199,7 @@ bt_activate(pcap_t* handle) } /* Initialize some components of the pcap structure. */ - handle->bufsize = handle->snapshot+BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header); - handle->offset = BT_CTRL_SIZE; + handle->bufsize = BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header)+handle->snapshot; handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR; handle->read_op = bt_read_linux; @@ -305,16 +304,18 @@ bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *us ssize_t ret; struct pcap_pkthdr pkth; pcap_bluetooth_h4_header* bthdr; + char *pktd; - bthdr = (pcap_bluetooth_h4_header*) &handle->buffer[handle->offset]; - iv.iov_base = &handle->buffer[handle->offset+sizeof(pcap_bluetooth_h4_header)]; + pktd = (char *)handle->buffer + BT_CTRL_SIZE; + bthdr = (pcap_bluetooth_h4_header*)(void *)pktd; + iv.iov_base = pktd + sizeof(pcap_bluetooth_h4_header); iv.iov_len = handle->snapshot; memset(&msg, 0, sizeof(msg)); msg.msg_iov = &iv; msg.msg_iovlen = 1; msg.msg_control = handle->buffer; - msg.msg_controllen = handle->offset; + msg.msg_controllen = BT_CTRL_SIZE; /* ignore interrupt system call error */ do { @@ -357,9 +358,8 @@ bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *us pkth.caplen+=sizeof(pcap_bluetooth_h4_header); pkth.len = pkth.caplen; if (handle->fcode.bf_insns == NULL || - bpf_filter(handle->fcode.bf_insns, &handle->buffer[handle->offset], - pkth.len, pkth.caplen)) { - callback(user, &pkth, &handle->buffer[handle->offset]); + bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) { + callback(user, &pkth, pktd); return 1; } return 0; /* didn't pass filter */ diff --git a/pcap-bt-monitor-linux.c b/pcap-bt-monitor-linux.c index b18bdcae..b73d08b8 100644 --- a/pcap-bt-monitor-linux.c +++ b/pcap-bt-monitor-linux.c @@ -82,13 +82,15 @@ bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_ch ssize_t ret; struct pcap_pkthdr pkth; pcap_bluetooth_linux_monitor_header *bthdr; + char *pktd; struct hci_mon_hdr hdr; - bthdr = (pcap_bluetooth_linux_monitor_header*) &handle->buffer[handle->offset]; + pktd = (char *)handle->buffer + BT_CONTROL_SIZE; + bthdr = (pcap_bluetooth_linux_monitor_header*)(void *)pktd; iv[0].iov_base = &hdr; iv[0].iov_len = sizeof(hdr); - iv[1].iov_base = &handle->buffer[handle->offset + sizeof(pcap_bluetooth_linux_monitor_header)]; + iv[1].iov_base = pktd + sizeof(pcap_bluetooth_linux_monitor_header); iv[1].iov_len = handle->snapshot; memset(&pkth.ts, 0, sizeof(pkth.ts)); @@ -96,7 +98,7 @@ bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_ch msg.msg_iov = iv; msg.msg_iovlen = 2; msg.msg_control = handle->buffer; - msg.msg_controllen = handle->offset; + msg.msg_controllen = BT_CONTROL_SIZE; do { ret = recvmsg(handle->fd, &msg, 0); @@ -128,9 +130,8 @@ bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_ch bthdr->opcode = htons(hdr.opcode); if (handle->fcode.bf_insns == NULL || - bpf_filter(handle->fcode.bf_insns, &handle->buffer[handle->offset], - pkth.len, pkth.caplen)) { - callback(user, &pkth, &handle->buffer[handle->offset]); + bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) { + callback(user, &pkth, pktd); return 1; } return 0; /* didn't pass filter */ @@ -172,8 +173,7 @@ bt_monitor_activate(pcap_t* handle) return PCAP_ERROR_RFMON_NOTSUP; } - handle->bufsize = handle->snapshot + BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header); - handle->offset = BT_CONTROL_SIZE; + handle->bufsize = BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header) + handle->snapshot; handle->linktype = DLT_BLUETOOTH_LINUX_MONITOR; handle->read_op = bt_monitor_read; diff --git a/pcap-can-linux.c b/pcap-can-linux.c index a8e1e355..f1d2d3b7 100644 --- a/pcap-can-linux.c +++ b/pcap-can-linux.c @@ -39,6 +39,8 @@ #include "pcap-int.h" #include "pcap-can-linux.h" +#define CAN_CONTROL_SIZE 8 + #ifdef NEED_STRERROR_H #include "strerror.h" #endif @@ -148,8 +150,7 @@ can_activate(pcap_t* handle) struct ifreq ifr; /* Initialize some components of the pcap structure. */ - handle->bufsize = 24; - handle->offset = 8; + handle->bufsize = CAN_CONTROL_SIZE + 16; handle->linktype = DLT_CAN_SOCKETCAN; handle->read_op = can_read_linux; handle->inject_op = can_inject_linux; @@ -221,17 +222,19 @@ can_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *u { struct msghdr msg; struct pcap_pkthdr pkth; + char *pktd; struct iovec iv; struct can_frame* cf; - iv.iov_base = &handle->buffer[handle->offset]; + pktd = (char *)handle->buffer + CAN_CONTROL_SIZE; + iv.iov_base = pktd; iv.iov_len = handle->snapshot; memset(&msg, 0, sizeof(msg)); msg.msg_iov = &iv; msg.msg_iovlen = 1; msg.msg_control = handle->buffer; - msg.msg_controllen = handle->offset; + msg.msg_controllen = CAN_CONTROL_SIZE; do { @@ -251,8 +254,8 @@ can_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *u } /* adjust capture len according to frame len */ - cf = (struct can_frame*)&handle->buffer[8]; - pkth.caplen -= 8 - cf->can_dlc; + cf = (struct can_frame*)(void *)pktd; + pkth.caplen -= CAN_CONTROL_SIZE - cf->can_dlc; pkth.len = pkth.caplen; cf->can_id = htonl( cf->can_id ); @@ -264,7 +267,7 @@ can_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *u return -1; } - callback(user, &pkth, &handle->buffer[8]); + callback(user, &pkth, pktd); return 1; } diff --git a/pcap-dlpi.c b/pcap-dlpi.c index c0071352..254ca43b 100644 --- a/pcap-dlpi.c +++ b/pcap-dlpi.c @@ -230,7 +230,7 @@ pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user) } cc = data.len; } while (cc == 0); - bp = p->buffer + p->offset; + bp = (u_char *)p->buffer + p->offset; } else bp = p->bp; diff --git a/pcap-int.h b/pcap-int.h index 2f71e115..bae3ee95 100644 --- a/pcap-int.h +++ b/pcap-int.h @@ -162,7 +162,7 @@ struct pcap { * Read buffer. */ int bufsize; - u_char *buffer; + void *buffer; u_char *bp; int cc; diff --git a/pcap-libdlpi.c b/pcap-libdlpi.c index 333e532b..899b07bf 100644 --- a/pcap-libdlpi.c +++ b/pcap-libdlpi.c @@ -337,7 +337,7 @@ pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user) } msglen = p->bufsize; - bufp = p->buffer + p->offset; + bufp = (u_char *)p->buffer + p->offset; retv = dlpi_recv(pd->dlpi_hd, NULL, NULL, bufp, &msglen, -1, NULL); diff --git a/pcap-linux.c b/pcap-linux.c index 6ef9e756..1f975370 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -1715,7 +1715,7 @@ pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata) * if we're using a memory-mapped buffer, we won't even * get notified of "network down" events. */ - bp = handle->buffer + handle->offset; + bp = (u_char *)handle->buffer + handle->offset; #if defined(HAVE_PACKET_AUXDATA) && defined(HAVE_LINUX_TPACKET_AUXDATA_TP_VLAN_TCI) msg.msg_name = &from; diff --git a/pcap-netfilter-linux.c b/pcap-netfilter-linux.c index 3ee6faa3..4dda9a79 100644 --- a/pcap-netfilter-linux.c +++ b/pcap-netfilter-linux.c @@ -105,7 +105,7 @@ netfilter_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_c return -1; } - buf = handle->buffer; + buf = (unsigned char *)handle->buffer; while (len >= NLMSG_SPACE(0)) { const struct nlmsghdr *nlh = (const struct nlmsghdr *) buf; u_int32_t msg_len; diff --git a/pcap-nit.c b/pcap-nit.c index a8355f9d..36ed7758 100644 --- a/pcap-nit.c +++ b/pcap-nit.c @@ -118,7 +118,7 @@ pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) pcap_strerror(errno)); return (-1); } - bp = p->buffer; + bp = (u_char *)p->buffer; } else bp = p->bp; @@ -301,7 +301,7 @@ pcap_activate_nit(pcap_t *p) p->linktype = DLT_EN10MB; p->bufsize = BUFSPACE; - p->buffer = (u_char *)malloc(p->bufsize); + p->buffer = malloc(p->bufsize); if (p->buffer == NULL) { strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); goto bad; diff --git a/pcap-pf.c b/pcap-pf.c index e03b2ed1..d9e9ec76 100644 --- a/pcap-pf.c +++ b/pcap-pf.c @@ -131,7 +131,7 @@ pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user) pcap_strerror(errno)); return (-1); } - bp = pc->buffer + pc->offset; + bp = (u_char *)pc->buffer + pc->offset; } else bp = pc->bp; /* @@ -476,7 +476,7 @@ your system may not be properly configured; see the packetfilter(4) man page\n", } p->bufsize = BUFSPACE; - p->buffer = (u_char*)malloc(p->bufsize + p->offset); + p->buffer = malloc(p->bufsize + p->offset); if (p->buffer == NULL) { strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); goto bad; diff --git a/pcap-sita.c b/pcap-sita.c index bc0428c0..ffffc92a 100644 --- a/pcap-sita.c +++ b/pcap-sita.c @@ -951,7 +951,7 @@ static int pcap_read_acn(pcap_t *handle, int max_packets, pcap_handler callback, pcap_header.caplen = ntohl(*(uint32_t *)&packet_header[8]); /* caplen */ pcap_header.len = ntohl(*(uint32_t *)&packet_header[12]); /* len */ - handle->bp = handle->buffer + handle->offset; /* start off the receive pointer at the right spot */ + handle->bp = (u_char *)handle->buffer + handle->offset; /* start off the receive pointer at the right spot */ if (acn_read_n_bytes_with_timeout(handle, pcap_header.caplen) == -1) return 0; /* then try to read in the rest of the data */ callback(user, &pcap_header, handle->bp); /* call the user supplied callback function */ diff --git a/pcap-snit.c b/pcap-snit.c index 0ce78603..c6d8a93f 100644 --- a/pcap-snit.c +++ b/pcap-snit.c @@ -134,7 +134,7 @@ pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) pcap_strerror(errno)); return (-1); } - bp = p->buffer; + bp = (u_char *)p->buffer; } else bp = p->bp; @@ -378,7 +378,7 @@ pcap_activate_snit(pcap_t *p) p->linktype = DLT_EN10MB; p->bufsize = BUFSPACE; - p->buffer = (u_char *)malloc(p->bufsize); + p->buffer = malloc(p->bufsize); if (p->buffer == NULL) { strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); goto bad; diff --git a/pcap-snoop.c b/pcap-snoop.c index f622f31f..10b93aa6 100644 --- a/pcap-snoop.c +++ b/pcap-snoop.c @@ -371,7 +371,7 @@ pcap_activate_snoop(pcap_t *p) } p->bufsize = 4096; /* XXX */ - p->buffer = (u_char *)malloc(p->bufsize); + p->buffer = malloc(p->bufsize); if (p->buffer == NULL) { snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); diff --git a/pcap-usb-linux.c b/pcap-usb-linux.c index 957273a8..aa49eacf 100644 --- a/pcap-usb-linux.c +++ b/pcap-usb-linux.c @@ -789,7 +789,7 @@ usb_read_linux_bin(pcap_t *handle, int max_packets, pcap_handler callback, u_cha /* the usb header is going to be part of 'packet' data*/ info.hdr = (pcap_usb_header*) handle->buffer; - info.data = handle->buffer + sizeof(pcap_usb_header); + info.data = (u_char *)handle->buffer + sizeof(pcap_usb_header); info.data_len = clen; /* ignore interrupt system call errors */ diff --git a/pcap-win32.c b/pcap-win32.c index f449f796..9b38f3c3 100644 --- a/pcap-win32.c +++ b/pcap-win32.c @@ -663,7 +663,7 @@ pcap_activate_win32(pcap_t *p) goto bad; } - p->buffer = (u_char *)malloc(p->bufsize); + p->buffer = malloc(p->bufsize); if (p->buffer == NULL) { snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); diff --git a/sf-pcap-ng.c b/sf-pcap-ng.c index 45dca11d..6579f30a 100644 --- a/sf-pcap-ng.c +++ b/sf-pcap-ng.c @@ -260,6 +260,8 @@ read_block(FILE *fp, pcap_t *p, struct block_cursor *cursor, char *errbuf) { int status; struct block_header bhdr; + u_char *bdata; + size_t data_remaining; status = read_bytes(fp, &bhdr, sizeof(bhdr), 0, errbuf); if (status <= 0) @@ -316,16 +318,16 @@ read_block(FILE *fp, pcap_t *p, struct block_cursor *cursor, char *errbuf) * of the block. */ memcpy(p->buffer, &bhdr, sizeof(bhdr)); - if (read_bytes(fp, p->buffer + sizeof(bhdr), - bhdr.total_length - sizeof(bhdr), 1, errbuf) == -1) + bdata = (u_char *)p->buffer + sizeof(bhdr); + data_remaining = bhdr.total_length - sizeof(bhdr); + if (read_bytes(fp, bdata, data_remaining, 1, errbuf) == -1) return (-1); /* * Initialize the cursor. */ - cursor->data = p->buffer + sizeof(bhdr); - cursor->data_remaining = bhdr.total_length - sizeof(bhdr) - - sizeof(struct block_trailer); + cursor->data = bdata; + cursor->data_remaining = data_remaining - sizeof(struct block_trailer); cursor->block_type = bhdr.block_type; return (1); } @@ -803,12 +805,12 @@ pcap_ng_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf, * of the SHB. */ bhdrp = (struct block_header *)p->buffer; - shbp = (struct section_header_block *)(p->buffer + sizeof(struct block_header)); + shbp = (struct section_header_block *)((u_char *)p->buffer + sizeof(struct block_header)); bhdrp->block_type = magic; bhdrp->total_length = total_length; shbp->byte_order_magic = byte_order_magic; if (read_bytes(fp, - p->buffer + (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)), + (u_char *)p->buffer + (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)), total_length - (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)), 1, errbuf) == -1) goto fail;