Convert bitfields to integers in a safe way.
authorGuus Sliepen <guus@tinc-vpn.org>
Wed, 9 Sep 2009 10:04:08 +0000 (12:04 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Wed, 9 Sep 2009 10:04:08 +0000 (12:04 +0200)
This is commit eb391c52eed46f3f03b404553df417851fc0cb90 redone, but without the
non-standard anonymous union.

lib/utils.c
lib/utils.h
src/connection.c
src/net.c

index 56bcb0b1b35d8687be63c346ce2690e69063aff4..02b7b3443e626c268bead9bff0bccd03f094b596 100644 (file)
@@ -100,3 +100,10 @@ const char *winerror(int err) {
 }
 #endif
 
+unsigned int bitfield_to_int(void *bitfield, size_t size) {
+       unsigned int value = 0;
+       if(size > sizeof value)
+               size = sizeof value;
+       memcpy(&value, bitfield, size);
+       return value;
+}
index 04c4ddbd3aeb6c8f94087594a99baf9ba7240fc1..ce7264829c7d8fd5e50114537ef09e1f96719229 100644 (file)
@@ -43,4 +43,6 @@ extern const char *winerror(int);
 #define strerror(x) ((x)>0?strerror(x):winerror(GetLastError()))
 #endif
 
+extern unsigned int bitfield_to_int(void *bitfield, size_t size);
+
 #endif                                                 /* __TINC_UTILS_H__ */
index 430e1c149ec19d9307cb203947dab624177cd095..66eb0596bdc41bf4fb0c4518611a724d1aa1c4da 100644 (file)
@@ -144,7 +144,7 @@ void dump_connections(void)
        for(node = connection_tree->head; node; node = node->next) {
                c = node->data;
                logger(LOG_DEBUG, _(" %s at %s options %lx socket %d status %04x outbuf %d/%d/%d"),
-                          c->name, c->hostname, c->options, c->socket, *(uint32_t *)&c->status,
+                          c->name, c->hostname, c->options, c->socket, bitfield_to_int(&c->status, sizeof c->status),
                           c->outbufsize, c->outbufstart, c->outbuflen);
        }
 
index 910e86dd24ef4d7b49f7a5cf61473ef33cad8708..3cf1773d578a70b99daa3217915d175906c1ee46 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -251,7 +251,7 @@ static void check_dead_connections(void)
                        } else {
                                if(c->status.remove) {
                                        logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
-                                                  c->name, c->hostname, *(uint32_t *)&c->status);
+                                                  c->name, c->hostname, bitfield_to_int(&c->status, sizeof c->status));
                                        connection_del(c);
                                        continue;
                                }