pespin has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmo-ggsn/+/14980


Change subject: sgsnemu: Fix unaligned pointer access during ip/icmp checksum
......................................................................

sgsnemu: Fix unaligned pointer access during ip/icmp checksum

Catched by gcc 9.1.0:
osmo-ggsn/sgsnemu/sgsnemu.c:1294:2: error: converting a packed struct ip_ping 
pointer (alignment 1) to a uint16_t {aka short unsigned int} pointer (alignment 
2) may result in an unaligned pointer value [-Werror=address-of-packed-member]
 1294 |  p = (uint16_t *) & pack;

Change-Id: I783f104c31234a07f2a13f6dbc577a71b25b36a7
---
M sgsnemu/sgsnemu.c
1 file changed, 12 insertions(+), 8 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-ggsn refs/changes/80/14980/1

diff --git a/sgsnemu/sgsnemu.c b/sgsnemu/sgsnemu.c
index 8e41031..4f1f844 100644
--- a/sgsnemu/sgsnemu.c
+++ b/sgsnemu/sgsnemu.c
@@ -1234,7 +1234,7 @@
 {

        struct ip_ping pack;
-       uint16_t *p = (uint16_t *) & pack;
+       uint16_t v16;
        uint8_t *p8 = (uint8_t *) & pack;
        struct in_addr src;
        unsigned int n;
@@ -1270,7 +1270,7 @@
        pack.seq = htons(seq);

        /* Generate ICMP payload */
-       p8 = (uint8_t *) & pack + CREATEPING_IP + CREATEPING_ICMP;
+       p8 = (uint8_t *) &pack + CREATEPING_IP + CREATEPING_ICMP;
        for (n = 0; n < (datasize); n++)
                p8[n] = n;

@@ -1278,11 +1278,13 @@
                gettimeofday(tp, &tz);

        /* Calculate IP header checksum */
-       p = (uint16_t *) & pack;
+       p8 = (uint8_t *) &pack;
        count = CREATEPING_IP;
        sum = 0;
        while (count > 1) {
-               sum += *p++;
+               memcpy(&v16, p8, 2);
+               sum += v16;
+               p8 += 2;
                count -= 2;
        }
        while (sum >> 16)
@@ -1292,14 +1294,16 @@
        /* Calculate ICMP checksum */
        count = CREATEPING_ICMP + datasize;     /* Length of ICMP message */
        sum = 0;
-       p = (uint16_t *) & pack;
-       p += CREATEPING_IP / 2;
+       p8 = (uint8_t *) &pack;
+       p8 += CREATEPING_IP;
        while (count > 1) {
-               sum += *p++;
+               memcpy(&v16, p8, 2);
+               sum += v16;
+               p8 += 2;
                count -= 2;
        }
        if (count > 0)
-               sum += *(unsigned char *)p;
+               sum += *(unsigned char *)p8;
        while (sum >> 16)
                sum = (sum & 0xffff) + (sum >> 16);
        pack.checksum = ~sum;

--
To view, visit https://gerrit.osmocom.org/c/osmo-ggsn/+/14980
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-ggsn
Gerrit-Branch: master
Gerrit-Change-Id: I783f104c31234a07f2a13f6dbc577a71b25b36a7
Gerrit-Change-Number: 14980
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <[email protected]>
Gerrit-MessageType: newchange

Reply via email to