[libvirt] [PATCH 1/2] nwfilter: Fix pointer.

Richard W.M. Jones rjones at redhat.com
Mon Jan 21 15:13:20 UTC 2019


GCC 9 complains:

nwfilter/nwfilter_dhcpsnoop.c: In function 'virNWFilterDHCPSnoopThread':
nwfilter/nwfilter_dhcpsnoop.c:1456:31: error: converting a packed 'virNWFilterSnoopEthHdrPtr' {aka 'struct _virNWFilterSnoopEthHdr *'} pointer (alignment 1) to 'const u_char *' {aka 'const unsigned char *'} (alignment 8) may result in an unaligned pointer value [-Werror=address-of-packed-member]
 1456 |                               (const u_char **)&packet);
      |                               ^
nwfilter/nwfilter_dhcpsnoop.c:183:8: note: defined here
  183 | struct _virNWFilterSnoopEthHdr {
      |        ^~~~~~~~~~~~~~~~~~~~~~~

However it seems like there's more going on here than just an enhanced
GCC warning.  The function pcap_next_ex is documented as:

       the pointer pointed  to  by  the
       pkt_data  argument  is  set  to  point  to the data in the packet

We are passing a struct here rather than a pointer.  I changed the
code to pass a pointer instead.
---
 src/nwfilter/nwfilter_dhcpsnoop.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/nwfilter/nwfilter_dhcpsnoop.c b/src/nwfilter/nwfilter_dhcpsnoop.c
index 58f0057c3f..45873a542c 100644
--- a/src/nwfilter/nwfilter_dhcpsnoop.c
+++ b/src/nwfilter/nwfilter_dhcpsnoop.c
@@ -1335,7 +1335,7 @@ virNWFilterDHCPSnoopThread(void *req0)
 {
     virNWFilterSnoopReqPtr req = req0;
     struct pcap_pkthdr *hdr;
-    virNWFilterSnoopEthHdrPtr packet;
+    const virNWFilterSnoopEthHdrPtr *packetPtr;
     int ifindex = 0;
     int errcount = 0;
     int tmp = -1, rv, n, pollTo;
@@ -1453,7 +1453,7 @@ virNWFilterDHCPSnoopThread(void *req0)
             n--;
 
             rv = pcap_next_ex(pcapConf[i].handle, &hdr,
-                              (const u_char **)&packet);
+                              (const u_char **)&packetPtr);
 
             if (rv < 0) {
                 /* error reading from socket */
@@ -1530,7 +1530,7 @@ virNWFilterDHCPSnoopThread(void *req0)
                     continue;
                 }
 
-                if (virNWFilterSnoopDHCPDecodeJobSubmit(worker, packet,
+                if (virNWFilterSnoopDHCPDecodeJobSubmit(worker, *packetPtr,
                                                       hdr->caplen,
                                                       pcapConf[i].dir,
                                                       &pcapConf[i].qCtr) < 0) {
-- 
2.20.1




More information about the libvir-list mailing list