]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
convert error-prone code to inline typed functions
authorAlan T. DeKok <aland@freeradius.org>
Thu, 7 Apr 2022 20:54:24 +0000 (16:54 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Thu, 7 Apr 2022 20:59:50 +0000 (16:59 -0400)
src/lib/server/tmpl_eval.c
src/lib/util/value.c
src/lib/util/value.h

index adaef27f3d20137b56b8a9cd01f23bd669a2dd9f..8a0ba06e746600331ece2bec381771824e87fb1c 100644 (file)
@@ -559,7 +559,7 @@ ssize_t _tmpl_to_type(void *out,
        RDEBUG4("Copying %zu bytes to %p from offset %zu",
                fr_value_box_field_sizes[dst_type], *((void **)out), fr_value_box_offsets[dst_type]);
 
-       memcpy(out, ((uint8_t const *) from_cast) + fr_value_box_offsets[dst_type], fr_value_box_field_sizes[dst_type]);
+       fr_value_box_memcpy_out(out, from_cast, dst_type);
 
        return from_cast->vb_length;
 }
@@ -810,7 +810,7 @@ ssize_t _tmpl_to_atype(TALLOC_CTX *ctx, void *out,
        RDEBUG4("Copying %zu bytes to %p from offset %zu",
                fr_value_box_field_sizes[dst_type], *((void **)out), fr_value_box_offsets[dst_type]);
 
-       memcpy(out, ((uint8_t *)&from_cast) + fr_value_box_offsets[dst_type], fr_value_box_field_sizes[dst_type]);
+       fr_value_box_memcpy_out(out, &from_cast, dst_type);
 
        /*
         *      Frees any memory allocated for temporary buffers
index 913a4db170792bc7e79f2816eed3eced450edb9d..c567c6396506c0fe08b569bfcc8aed99d171f66e 100644 (file)
@@ -1404,7 +1404,7 @@ ssize_t fr_value_box_to_network(fr_dbuff_t *dbuff, fr_value_box_t const *value)
        case FR_TYPE_ETHERNET:
        case FR_TYPE_UINT8:
        case FR_TYPE_INT8:
-               FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, ((uint8_t const *)value) + fr_value_box_offsets[value->type], min);
+               FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, fr_value_box_raw(value, value->type), min);
                break;
 
        /*
@@ -1423,7 +1423,7 @@ ssize_t fr_value_box_to_network(fr_dbuff_t *dbuff, fr_value_box_t const *value)
 
                fr_value_box_hton(&tmp, value);
 
-               FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, ((uint8_t const *)&tmp) + fr_value_box_offsets[value->type], min);
+               FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, fr_value_box_raw(&tmp, value->type), min);
        }
                break;
 
@@ -1712,7 +1712,7 @@ ssize_t fr_value_box_from_network(TALLOC_CTX *ctx,
 
        case FR_TYPE_IFID:
        case FR_TYPE_ETHERNET:
-               FR_DBUFF_OUT_MEMCPY_RETURN(((uint8_t *)dst) + fr_value_box_offsets[type], &work_dbuff, len);
+               FR_DBUFF_OUT_MEMCPY_RETURN(fr_value_box_raw(dst, type), &work_dbuff, len);
                break;
 
        case FR_TYPE_UINT8:
@@ -2102,14 +2102,14 @@ static inline int fr_value_box_cast_to_octets(TALLOC_CTX *ctx, fr_value_box_t *d
 
                fr_value_box_hton(&tmp, src);   /* Flip any numeric representations */
                return fr_value_box_memdup(ctx, dst, dst_enumv,
-                                          ((uint8_t const *)&tmp) + fr_value_box_offsets[src->type],
+                                          fr_value_box_raw(&tmp, src->type),
                                           fr_value_box_field_sizes[src->type], src->tainted);
        }
 
        default:
                /* Not the same talloc_memdup call as above.  The above memdup reads data from the dst */
                return fr_value_box_memdup(ctx, dst, dst_enumv,
-                                          ((uint8_t const *)src) + fr_value_box_offsets[src->type],
+                                          fr_value_box_raw(src, src->type),
                                           fr_value_box_field_sizes[src->type], src->tainted);
        }
 }
@@ -2765,9 +2765,9 @@ static inline int fr_value_box_cast_integer_to_integer(UNUSED TALLOC_CTX *ctx, f
        default:
 #ifdef WORDS_BIGENDIAN
                memcpy(((uint8_t *)&tmp) + (sizeof(tmp) - len),
-                      ((uint8_t const *)src) + fr_value_box_offsets[src->type], len);
+                      fr_value_box_raw(src, src->type), len);
 #else
-               memcpy(&tmp, ((uint8_t const *)src) + fr_value_box_offsets[src->type], len);
+               memcpy(&tmp, fr_value_box_raw(src, src->type), len);
 #endif
                break;
        }
@@ -2831,10 +2831,10 @@ static inline int fr_value_box_cast_integer_to_integer(UNUSED TALLOC_CTX *ctx, f
 
        default:
 #ifdef WORDS_BIGENDIAN
-               memcpy(((uint8_t *)dst) + fr_value_box_offsets[dst_type],
+               memcpy(fr_value_box_raw(dst, dst->type),
                       ((uint8_t *)&tmp) + (sizeof(tmp) - len), fr_value_box_field_sizes[dst_type]);
 #else
-               memcpy(((uint8_t *)dst) + fr_value_box_offsets[dst_type],
+               memcpy(fr_value_box_raw(dst, dst->type),
                       &tmp, fr_value_box_field_sizes[dst_type]);
 #endif
                break;
@@ -3420,9 +3420,7 @@ int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t
 {
        switch (src->type) {
        default:
-               memcpy(((uint8_t *)dst) + fr_value_box_offsets[src->type],
-                      ((uint8_t const *)src) + fr_value_box_offsets[src->type],
-                      fr_value_box_field_sizes[src->type]);
+               fr_value_box_memcpy_out(fr_value_box_raw(dst, src->type), src, src->type);
                fr_value_box_copy_meta(dst, src);
                break;
 
@@ -5677,8 +5675,8 @@ uint32_t fr_value_box_hash(fr_value_box_t const *vb)
 {
        switch (vb->type) {
        case FR_TYPE_FIXED_SIZE:
-               return fr_hash(((uint8_t const *)vb) + fr_value_box_offsets[vb->type],
-                                     fr_value_box_field_sizes[vb->type]);
+               return fr_hash(fr_value_box_raw(vb, vb->type),
+                              fr_value_box_field_sizes[vb->type]);
 
        case FR_TYPE_STRING:
                return fr_hash(vb->vb_strvalue, vb->vb_length);
index 8eb9bf63c8fdccdf8ad5cb35fce8cd7c980f533b..ecea2732b1b8e014ff75264ae6f80e38f18f3d9a 100644 (file)
@@ -503,6 +503,28 @@ fr_value_box_t *fr_value_box_alloc_null(TALLOC_CTX *ctx)
        return fr_value_box_alloc(ctx, FR_TYPE_NULL, NULL, false);
 }
 
+/** Return a pointer to the "raw" value from a value-box.
+ *
+ *  This has "const" input and "unconst" output because sometimes it's used
+ *  to copy out of, and sometimes in to, a value-box.  We rely on the caller to know
+ *  the correct uses of it.
+ */
+static inline CC_HINT(always_inline)
+uint8_t *fr_value_box_raw(fr_value_box_t const *vb, fr_type_t type)
+{
+       return UNCONST(uint8_t *, vb) + fr_value_box_offsets[type];
+}
+
+/** Copy raw values out of a value box
+ *
+ */
+static inline CC_HINT(always_inline)
+void fr_value_box_memcpy_out(void *dst, fr_value_box_t const *vb, fr_type_t type)
+{
+       memcpy(dst, ((uint8_t const *)vb) + fr_value_box_offsets[type], fr_value_box_field_sizes[type]);
+}
+
+
 /** Box an ethernet value (6 bytes, network byte order)
  *
  * @param[in] dst      Where to copy the ethernet address to.