From 81afa26e4ad53bea00da18a7666f63d33cf3f588 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Wed, 9 Sep 2009 12:04:08 +0200 Subject: [PATCH] Convert bitfields to integers in a safe way. This is commit eb391c52eed46f3f03b404553df417851fc0cb90 redone, but without the non-standard anonymous union. --- lib/utils.c | 7 +++++++ lib/utils.h | 2 ++ src/connection.c | 2 +- src/net.c | 2 +- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/utils.c b/lib/utils.c index 56bcb0b1..02b7b344 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -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; +} diff --git a/lib/utils.h b/lib/utils.h index 04c4ddbd..ce726482 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -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__ */ diff --git a/src/connection.c b/src/connection.c index 430e1c14..66eb0596 100644 --- a/src/connection.c +++ b/src/connection.c @@ -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); } diff --git a/src/net.c b/src/net.c index 910e86dd..3cf1773d 100644 --- 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; } -- 2.39.5