]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
value: Don't zero out structures and then immdiately write data to them
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 11 May 2020 18:35:44 +0000 (13:35 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 11 May 2020 18:35:44 +0000 (13:35 -0500)
...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.

src/lib/util/value.c

index c8efadddbcc797fd76eb37fe007978b54e79a39e..a7228594e87a0dca240a05b4239e8dca8b549ee8 100644 (file)
@@ -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: