From: Arran Cudbard-Bell Date: Mon, 11 May 2020 18:35:44 +0000 (-0500) Subject: value: Don't zero out structures and then immdiately write data to them X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ad3d3db61a23232b45f2ce6fa1b882419d2129db;p=thirdparty%2Ffreeradius-server.git value: Don't zero out structures and then immdiately write data to them ...and as for uninitialised fields in compound literals containing garbage, c99 states that shouldn't be the case. http://c0x.coding-guidelines.com/6.7.8.html 1677. --- diff --git a/src/lib/util/value.c b/src/lib/util/value.c index c8efadddbcc..a7228594e87 100644 --- a/src/lib/util/value.c +++ b/src/lib/util/value.c @@ -1341,6 +1341,7 @@ ssize_t fr_value_box_from_network(TALLOC_CTX *ctx, bool tainted) { uint8_t const *p = src; + uint8_t const *end = p + len; size_t min, max; min = fr_value_box_network_sizes[type][0]; @@ -1374,35 +1375,37 @@ ssize_t fr_value_box_from_network(TALLOC_CTX *ctx, * Already in network byte order */ case FR_TYPE_IPV4_ADDR: - memset(&dst->vb_ip, 0, sizeof(dst->vb_ip)); - memcpy(&dst->vb_ip.addr.v4, src, len); - dst->vb_ip.af = AF_INET; - dst->vb_ip.prefix = 32; - dst->vb_ip.scope_id = 0; /* init even if it's unused */ + dst->vb_ip = (fr_ipaddr_t){ + .af = AF_INET, + .prefix = 32, + }; + memcpy(&dst->vb_ip.addr.v4, p, (end - p)); break; case FR_TYPE_IPV4_PREFIX: - memset(&dst->vb_ip, 0, sizeof(dst->vb_ip)); - dst->vb_ip.af = AF_INET; - dst->vb_ip.prefix = *p++; - memcpy(&dst->vb_ip.addr.v4, p, sizeof(dst->vb_ip.addr.v4)); - dst->vb_ip.scope_id = 0; /* init even if it's unused */ + dst->vb_ip = (fr_ipaddr_t){ + .af = AF_INET, + .prefix = *p++, + }; + memcpy(&dst->vb_ip.addr.v4, p, (end - p)); break; case FR_TYPE_IPV6_ADDR: - memset(&dst->vb_ip, 0, sizeof(dst->vb_ip)); - dst->vb_ip.af = AF_INET6; - dst->vb_ip.scope_id = len == max ? *p++ : 0; /* optional */ - memcpy(&dst->vb_ip.addr.v6, p, sizeof(dst->vb_ip.addr.v6)); - dst->vb_ip.prefix = 128; + dst->vb_ip = (fr_ipaddr_t){ + .af = AF_INET6, + .scope_id = len == max ? *p++ : 0, /* optional */ + .prefix = 128 + }; + memcpy(&dst->vb_ip.addr.v6, p, (end - p)); break; case FR_TYPE_IPV6_PREFIX: - memset(&dst->vb_ip, 0, sizeof(dst->vb_ip)); - dst->vb_ip.af = AF_INET6; - dst->vb_ip.scope_id = len == max ? *p++ : 0; /* optional */ - dst->vb_ip.prefix = *p++; - memcpy(&dst->vb_ip.addr.v6, p, sizeof(dst->vb_ip.addr.v6)); + dst->vb_ip = (fr_ipaddr_t){ + .af = AF_INET6, + .scope_id = len == max ? *p++ : 0, + .prefix = *p++ + }; + memcpy(&dst->vb_ip.addr.v6, p, (end - p)); break; case FR_TYPE_BOOL: