[systemd-commits] 6 commits - src/libsystemd-network src/shared src/test

Tom Gundersen tomegun at kemper.freedesktop.org
Wed Nov 5 07:54:45 PST 2014


 src/libsystemd-network/dhcp6-option.c |   17 ++++------
 src/libsystemd-network/sd-pppoe.c     |   29 +++++------------
 src/shared/ptyfwd.c                   |    4 +-
 src/shared/unaligned.h                |   48 +++++++++++++++++-----------
 src/test/test-fileio.c                |    2 -
 src/test/test-hashmap-plain.c         |    2 -
 src/test/test-unaligned.c             |   57 +++++++++++++++++-----------------
 7 files changed, 80 insertions(+), 79 deletions(-)

New commits:
commit c962cb68d5754690cbe924a0d0b4251053217783
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue Nov 4 20:19:07 2014 +0100

    libsystemd-network: don't use unaligned helpers in _packed_ structs
    
    The compiler will do this for us.

diff --git a/src/libsystemd-network/dhcp6-option.c b/src/libsystemd-network/dhcp6-option.c
index a46b6e3..ea863f4 100644
--- a/src/libsystemd-network/dhcp6-option.c
+++ b/src/libsystemd-network/dhcp6-option.c
@@ -37,11 +37,11 @@ typedef struct DHCP6Option {
         be16_t code;
         be16_t len;
         uint8_t data[];
-} DHCP6Option;
+} _packed_ DHCP6Option;
 
 static int option_append_hdr(uint8_t **buf, size_t *buflen, uint16_t optcode,
                              size_t optlen) {
-        DHCP6Option *option = (DHCP6Option*) *buf; /* unaligned! */
+        DHCP6Option *option = (DHCP6Option*) *buf;
 
         assert_return(buf, -EINVAL);
         assert_return(*buf, -EINVAL);
@@ -50,8 +50,8 @@ static int option_append_hdr(uint8_t **buf, size_t *buflen, uint16_t optcode,
         if (optlen > 0xffff || *buflen < optlen + sizeof(DHCP6Option))
                 return -ENOBUFS;
 
-        unaligned_write_be16(&option->code, optcode);
-        unaligned_write_be16(&option->len, (uint16_t) optlen);
+        option->code = htobe16(optcode);
+        option->len = htobe16(optlen);
 
         *buf += sizeof(DHCP6Option);
         *buflen -= sizeof(DHCP6Option);
@@ -136,9 +136,8 @@ int dhcp6_option_append_ia(uint8_t **buf, size_t *buflen, DHCP6IA *ia) {
 }
 
 
-static int option_parse_hdr(uint8_t **buf, size_t *buflen, uint16_t *optcode,
-                            size_t *optlen) {
-        DHCP6Option *option = (DHCP6Option*) *buf; /* unaligned! */
+static int option_parse_hdr(uint8_t **buf, size_t *buflen, uint16_t *optcode, size_t *optlen) {
+        DHCP6Option *option = (DHCP6Option*) *buf;
         uint16_t len;
 
         assert_return(buf, -EINVAL);
@@ -148,12 +147,12 @@ static int option_parse_hdr(uint8_t **buf, size_t *buflen, uint16_t *optcode,
         if (*buflen < sizeof(DHCP6Option))
                 return -ENOMSG;
 
-        len = unaligned_read_be16(&option->len);
+        len = be16toh(option->len);
 
         if (len > *buflen)
                 return -ENOMSG;
 
-        *optcode = unaligned_read_be16(&option->code);
+        *optcode = be16toh(option->code);
         *optlen = len;
 
         *buf += 4;
diff --git a/src/libsystemd-network/sd-pppoe.c b/src/libsystemd-network/sd-pppoe.c
index 075e102..0a96223 100644
--- a/src/libsystemd-network/sd-pppoe.c
+++ b/src/libsystemd-network/sd-pppoe.c
@@ -100,19 +100,13 @@ struct sd_pppoe {
         be16toh((header)->length)
 
 #define PPPOE_PACKET_TAIL(packet)                                                                               \
-        (struct pppoe_tag *)((uint8_t*)(packet) + sizeof(struct pppoe_hdr) + PPPOE_PACKET_LENGTH(packet))
+        (struct pppoe_tag*)((uint8_t*)(packet) + sizeof(struct pppoe_hdr) + PPPOE_PACKET_LENGTH(packet))
 
-#define PPPOE_TAG_LENGTH(tag)                   \
-        unaligned_read_be16(&(tag)->tag_len)
+#define PPPOE_TAG_LENGTH(tag)  \
+        be16toh((tag)->tag_len)
 
-#define PPPOE_TAG_TYPE(tag)                     \
-        htobe16(unaligned_read_be16(&(tag)->tag_type))
-
-#define PPPOE_TAG_SET_LENGTH(tag, len) \
-        unaligned_write_be16(&(tag)->tag_len, len)
-
-#define PPPOE_TAG_SET_TYPE(tag, len) \
-        unaligned_write_be16(&(tag)->tag_type, be16toh(len))
+#define PPPOE_TAG_TYPE(tag) \
+        (tag)->tag_type
 
 #define PPPOE_TAG_NEXT(tag)                                                                      \
         (struct pppoe_tag *)((uint8_t *)(tag) + sizeof(struct pppoe_tag) + PPPOE_TAG_LENGTH(tag))
@@ -278,8 +272,8 @@ static void pppoe_tag_append(struct pppoe_hdr *packet, size_t packet_size, be16_
 
         tag = PPPOE_PACKET_TAIL(packet);
 
-        PPPOE_TAG_SET_LENGTH(tag, tag_len);
-        PPPOE_TAG_SET_TYPE(tag, tag_type);
+        tag->tag_len = htobe16(tag_len);
+        tag->tag_type = tag_type;
         if (tag_data)
                 memcpy(tag->tag_data, tag_data, tag_len);
 

commit 617e79465249e694d697fd78704571b3d00338c8
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue Nov 4 19:52:04 2014 +0100

    shared: unaligned - use void* instead of unaligned be16_t*

diff --git a/src/shared/unaligned.h b/src/shared/unaligned.h
index b194d71..d6181dd 100644
--- a/src/shared/unaligned.h
+++ b/src/shared/unaligned.h
@@ -23,34 +23,44 @@
 
 #include <stdint.h>
 
-#include "sparse-endian.h"
+static inline uint16_t unaligned_read_be16(const void *_u) {
+        const uint8_t *u = _u;
 
-static inline uint16_t unaligned_read_be16(const be16_t *u) {
-        return (((uint16_t) ((uint8_t*) u)[0]) << 8) |
-                ((uint16_t) ((uint8_t*) u)[1]);
+        return (((uint16_t) u[0]) << 8) |
+                ((uint16_t) u[1]);
 }
 
-static inline uint32_t unaligned_read_be32(const be32_t *u) {
-        return (((uint32_t) unaligned_read_be16((be16_t*) u)) << 16) |
-                ((uint32_t) unaligned_read_be16((be16_t*) u + 1));
+static inline uint32_t unaligned_read_be32(const void *_u) {
+        const uint8_t *u = _u;
+
+        return (((uint32_t) unaligned_read_be16(u)) << 16) |
+                ((uint32_t) unaligned_read_be16(u + 2));
 }
 
-static inline uint64_t unaligned_read_be64(const be64_t *u) {
-        return (((uint64_t) unaligned_read_be32((be32_t*) u)) << 32) |
-                ((uint64_t) unaligned_read_be32((be32_t*) u + 1));
+static inline uint64_t unaligned_read_be64(const void *_u) {
+        const uint8_t *u = _u;
+
+        return (((uint64_t) unaligned_read_be32(u)) << 32) |
+                ((uint64_t) unaligned_read_be32(u + 4));
 }
 
-static inline void unaligned_write_be16(be16_t *u, uint16_t a) {
-        ((uint8_t*) u)[0] = (uint8_t) (a >> 8);
-        ((uint8_t*) u)[1] = (uint8_t) a;
+static inline void unaligned_write_be16(void *_u, uint16_t a) {
+        uint8_t *u = _u;
+
+        u[0] = (uint8_t) (a >> 8);
+        u[1] = (uint8_t) a;
 }
 
-static inline void unaligned_write_be32(be32_t *u, uint32_t a) {
-        unaligned_write_be16((be16_t*) u, (uint16_t) (a >> 16));
-        unaligned_write_be16((be16_t*) u + 1, (uint16_t) a);
+static inline void unaligned_write_be32(void *_u, uint32_t a) {
+        uint8_t *u = _u;
+
+        unaligned_write_be16(u, (uint16_t) (a >> 16));
+        unaligned_write_be16(u + 2, (uint16_t) a);
 }
 
-static inline void unaligned_write_be64(be64_t *u, uint64_t a) {
-        unaligned_write_be32((be32_t*) u, (uint32_t) (a >> 32));
-        unaligned_write_be32((be32_t*) u + 1, (uint32_t) a);
+static inline void unaligned_write_be64(void *_u, uint64_t a) {
+        uint8_t *u = _u;
+
+        unaligned_write_be32(u, (uint32_t) (a >> 32));
+        unaligned_write_be32(u + 4, (uint32_t) a);
 }
diff --git a/src/test/test-unaligned.c b/src/test/test-unaligned.c
index 52693cd..1754d06 100644
--- a/src/test/test-unaligned.c
+++ b/src/test/test-unaligned.c
@@ -18,6 +18,7 @@
 ***/
 
 #include "unaligned.h"
+#include "sparse-endian.h"
 #include "util.h"
 
 static uint8_t data[] = {
@@ -28,65 +29,65 @@ static uint8_t data[] = {
 int main(int argc, const char *argv[]) {
         uint8_t scratch[16];
 
-        assert_se(unaligned_read_be16((be16_t*)&data[0]) == 0x0001);
-        assert_se(unaligned_read_be16((be16_t*)&data[1]) == 0x0102);
+        assert_se(unaligned_read_be16(&data[0]) == 0x0001);
+        assert_se(unaligned_read_be16(&data[1]) == 0x0102);
 
-        assert_se(unaligned_read_be32((be32_t*)&data[0]) == 0x00010203);
-        assert_se(unaligned_read_be32((be32_t*)&data[1]) == 0x01020304);
-        assert_se(unaligned_read_be32((be32_t*)&data[2]) == 0x02030405);
-        assert_se(unaligned_read_be32((be32_t*)&data[3]) == 0x03040506);
+        assert_se(unaligned_read_be32(&data[0]) == 0x00010203);
+        assert_se(unaligned_read_be32(&data[1]) == 0x01020304);
+        assert_se(unaligned_read_be32(&data[2]) == 0x02030405);
+        assert_se(unaligned_read_be32(&data[3]) == 0x03040506);
 
-        assert_se(unaligned_read_be64((be64_t*)&data[0]) == 0x0001020304050607);
-        assert_se(unaligned_read_be64((be64_t*)&data[1]) == 0x0102030405060708);
-        assert_se(unaligned_read_be64((be64_t*)&data[2]) == 0x0203040506070809);
-        assert_se(unaligned_read_be64((be64_t*)&data[3]) == 0x030405060708090a);
-        assert_se(unaligned_read_be64((be64_t*)&data[4]) == 0x0405060708090a0b);
-        assert_se(unaligned_read_be64((be64_t*)&data[5]) == 0x05060708090a0b0c);
-        assert_se(unaligned_read_be64((be64_t*)&data[6]) == 0x060708090a0b0c0d);
-        assert_se(unaligned_read_be64((be64_t*)&data[7]) == 0x0708090a0b0c0d0e);
+        assert_se(unaligned_read_be64(&data[0]) == 0x0001020304050607);
+        assert_se(unaligned_read_be64(&data[1]) == 0x0102030405060708);
+        assert_se(unaligned_read_be64(&data[2]) == 0x0203040506070809);
+        assert_se(unaligned_read_be64(&data[3]) == 0x030405060708090a);
+        assert_se(unaligned_read_be64(&data[4]) == 0x0405060708090a0b);
+        assert_se(unaligned_read_be64(&data[5]) == 0x05060708090a0b0c);
+        assert_se(unaligned_read_be64(&data[6]) == 0x060708090a0b0c0d);
+        assert_se(unaligned_read_be64(&data[7]) == 0x0708090a0b0c0d0e);
 
         zero(scratch);
-        unaligned_write_be16((uint16_t*)&scratch[0], 0x0001);
+        unaligned_write_be16(&scratch[0], 0x0001);
         assert_se(memcmp(&scratch[0], &data[0], sizeof(uint16_t)) == 0);
         zero(scratch);
-        unaligned_write_be16((uint16_t*)&scratch[1], 0x0102);
+        unaligned_write_be16(&scratch[1], 0x0102);
         assert_se(memcmp(&scratch[1], &data[1], sizeof(uint16_t)) == 0);
 
         zero(scratch);
-        unaligned_write_be32((be32_t*)&scratch[0], 0x00010203);
+        unaligned_write_be32(&scratch[0], 0x00010203);
         assert_se(memcmp(&scratch[0], &data[0], sizeof(uint32_t)) == 0);
         zero(scratch);
-        unaligned_write_be32((be32_t*)&scratch[1], 0x01020304);
+        unaligned_write_be32(&scratch[1], 0x01020304);
         assert_se(memcmp(&scratch[1], &data[1], sizeof(uint32_t)) == 0);
         zero(scratch);
-        unaligned_write_be32((be32_t*)&scratch[2], 0x02030405);
+        unaligned_write_be32(&scratch[2], 0x02030405);
         assert_se(memcmp(&scratch[2], &data[2], sizeof(uint32_t)) == 0);
         zero(scratch);
-        unaligned_write_be32((be32_t*)&scratch[3], 0x03040506);
+        unaligned_write_be32(&scratch[3], 0x03040506);
         assert_se(memcmp(&scratch[3], &data[3], sizeof(uint32_t)) == 0);
 
         zero(scratch);
-        unaligned_write_be64((be64_t*)&scratch[0], 0x0001020304050607);
+        unaligned_write_be64(&scratch[0], 0x0001020304050607);
         assert_se(memcmp(&scratch[0], &data[0], sizeof(uint64_t)) == 0);
         zero(scratch);
-        unaligned_write_be64((be64_t*)&scratch[1], 0x0102030405060708);
+        unaligned_write_be64(&scratch[1], 0x0102030405060708);
         assert_se(memcmp(&scratch[1], &data[1], sizeof(uint64_t)) == 0);
         zero(scratch);
-        unaligned_write_be64((be64_t*)&scratch[2], 0x0203040506070809);
+        unaligned_write_be64(&scratch[2], 0x0203040506070809);
         assert_se(memcmp(&scratch[2], &data[2], sizeof(uint64_t)) == 0);
         zero(scratch);
-        unaligned_write_be64((be64_t*)&scratch[3], 0x030405060708090a);
+        unaligned_write_be64(&scratch[3], 0x030405060708090a);
         assert_se(memcmp(&scratch[3], &data[3], sizeof(uint64_t)) == 0);
         zero(scratch);
-        unaligned_write_be64((be64_t*)&scratch[4], 0x0405060708090a0b);
+        unaligned_write_be64(&scratch[4], 0x0405060708090a0b);
         assert_se(memcmp(&scratch[4], &data[4], sizeof(uint64_t)) == 0);
         zero(scratch);
-        unaligned_write_be64((be64_t*)&scratch[5], 0x05060708090a0b0c);
+        unaligned_write_be64(&scratch[5], 0x05060708090a0b0c);
         assert_se(memcmp(&scratch[5], &data[5], sizeof(uint64_t)) == 0);
         zero(scratch);
-        unaligned_write_be64((be64_t*)&scratch[6], 0x060708090a0b0c0d);
+        unaligned_write_be64(&scratch[6], 0x060708090a0b0c0d);
         assert_se(memcmp(&scratch[6], &data[6], sizeof(uint64_t)) == 0);
         zero(scratch);
-        unaligned_write_be64((be64_t*)&scratch[7], 0x0708090a0b0c0d0e);
+        unaligned_write_be64(&scratch[7], 0x0708090a0b0c0d0e);
         assert_se(memcmp(&scratch[7], &data[7], sizeof(uint64_t)) == 0);
 }

commit 9eec671331adeb3f9a81b390758000364388a042
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue Nov 4 16:21:41 2014 +0100

    sd-pppoe: whitespace

diff --git a/src/libsystemd-network/sd-pppoe.c b/src/libsystemd-network/sd-pppoe.c
index 21ddaeb..075e102 100644
--- a/src/libsystemd-network/sd-pppoe.c
+++ b/src/libsystemd-network/sd-pppoe.c
@@ -698,8 +698,7 @@ static int pppoe_handle_message(sd_pppoe *ppp, struct pppoe_hdr *packet, struct
         return 0;
 }
 
-static int pppoe_receive_message(sd_event_source *s, int fd,
-                                 uint32_t revents, void *userdata) {
+static int pppoe_receive_message(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
         sd_pppoe *ppp = userdata;
         _cleanup_free_ struct pppoe_hdr *packet = NULL;
         union sockaddr_union link = {};
@@ -721,11 +720,9 @@ static int pppoe_receive_message(sd_event_source *s, int fd,
         if (!packet)
                 return -ENOMEM;
 
-        len = recvfrom(fd, packet, buflen, 0,
-                       &link.sa, &addrlen);
+        len = recvfrom(fd, packet, buflen, 0, &link.sa, &addrlen);
         if (len < 0) {
-                log_warning("PPPoE: colud not receive message from raw socket: %s",
-                          strerror(-r));
+                log_warning("PPPoE: colud not receive message from raw socket: %s", strerror(-r));
                 return 0;
         } else if ((size_t)len < sizeof(struct pppoe_hdr))
                 return 0;

commit 61c81750217af7492be86adde81b28e310cd5e82
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue Nov 4 16:20:22 2014 +0100

    test: hashmap-plain - make coverity happy
    
    Check return value of hashmap_ensure_allocated().
    
    CID#1250807.

diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c
index 63f2f3e..6f0910a 100644
--- a/src/test/test-hashmap-plain.c
+++ b/src/test/test-hashmap-plain.c
@@ -246,7 +246,7 @@ static void test_hashmap_put(void) {
         int valid_hashmap_put;
         void *val1 = (void*) "val 1";
 
-        hashmap_ensure_allocated(&m, &string_hash_ops);
+        assert_se(hashmap_ensure_allocated(&m, &string_hash_ops) >= 0);
         assert_se(m);
 
         valid_hashmap_put = hashmap_put(m, "key 1", val1);

commit 696c24fced77b152dda45f0bdbcf6411849bd8ab
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue Nov 4 16:19:26 2014 +0100

    test: fileio - make coverity happy
    
    Explicitly check the length of the read.
    
    Fixes CID#1250803.

diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
index a713abd..c26a6fa 100644
--- a/src/test/test-fileio.c
+++ b/src/test/test-fileio.c
@@ -342,7 +342,7 @@ static void test_write_string_file_no_create(void) {
         assert_se(write_string_file_no_create("/a/file/which/does/not/exists/i/guess", "boohoo") < 0);
         assert_se(write_string_file_no_create(fn, "boohoo") == 0);
 
-        assert_se(read(fd, buf, sizeof(buf)));
+        assert_se(read(fd, buf, sizeof(buf)) == strlen("boohoo\n"));
         assert_se(streq(buf, "boohoo\n"));
 
         unlink(fn);

commit 7c63b23f499069b7bbdf5e74d3e7a622918341e9
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue Nov 4 16:13:43 2014 +0100

    shared: ptyfwd - make coverity happy
    
    Explicitly ignore return value of ioctl to set window size.
    
    Fixes CID#1250804 and CID#1250800.

diff --git a/src/shared/ptyfwd.c b/src/shared/ptyfwd.c
index 7c6e182..3f88539 100644
--- a/src/shared/ptyfwd.c
+++ b/src/shared/ptyfwd.c
@@ -280,7 +280,7 @@ static int on_sigwinch_event(sd_event_source *e, const struct signalfd_siginfo *
 
         /* The window size changed, let's forward that. */
         if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
-                ioctl(f->master, TIOCSWINSZ, &ws);
+                (void)ioctl(f->master, TIOCSWINSZ, &ws);
 
         return 0;
 }
@@ -317,7 +317,7 @@ int pty_forward_new(sd_event *event, int master, PTYForward **ret) {
         f->master = master;
 
         if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
-                ioctl(master, TIOCSWINSZ, &ws);
+                (void)ioctl(master, TIOCSWINSZ, &ws);
 
         if (tcgetattr(STDIN_FILENO, &f->saved_stdin_attr) >= 0) {
                 struct termios raw_stdin_attr;



More information about the systemd-commits mailing list