]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Revert "Use the allocation functions to allocate buffers in cal.c instead of doing...
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sun, 5 Dec 2021 15:13:42 +0000 (10:13 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sun, 5 Dec 2021 15:13:42 +0000 (10:13 -0500)
This reverts commit 0f677c5ff474e88942bf25b49aafd5b97bcdfea4.

src/lib/util/calc.c

index 2dcc77a5a673cbfc7486c35f5dd1c4b55621bb95..d02cb861783859ed2fdbe6f58f45d852de862b7a 100644 (file)
@@ -394,7 +394,8 @@ static int calc_octets(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t cons
 
        switch (op) {
        case T_OP_PREPEND:      /* dst = b . a */
-               if (unlikely(fr_value_box_mem_alloc(ctx, &buf, dst, dst->enumv, len, a->tainted | b->tainted) < 0)) {
+               buf = talloc_array(ctx, uint8_t, len);
+               if (!buf) {
                oom:
                        fr_strerror_const("Out of memory");
                        return -1;
@@ -402,13 +403,20 @@ static int calc_octets(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t cons
 
                memcpy(buf, b->vb_octets, b->vb_length);
                memcpy(buf + b->vb_length, a->vb_octets, a->vb_length);
+
+               fr_value_box_clear_value(dst);
+               fr_value_box_memdup_shallow(dst, dst->enumv, buf, len, a->tainted | b->tainted);
                break;
 
        case T_ADD:     /* dst = a . b */
-               if (unlikely(fr_value_box_mem_alloc(ctx, &buf, dst, dst->enumv, len, a->tainted | b->tainted) < 0)) goto oom;
+               buf = talloc_array(ctx, uint8_t, len);
+               if (!buf) goto oom;
 
                memcpy(buf, a->vb_octets, a->vb_length);
                memcpy(buf + a->vb_length, b->vb_octets, b->vb_length);
+
+               fr_value_box_clear_value(dst);
+               fr_value_box_memdup_shallow(dst, dst->enumv, buf, len, a->tainted | b->tainted);
                break;
 
        case T_SUB:
@@ -426,8 +434,13 @@ static int calc_octets(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t cons
                }
 
                len = a->vb_length - b->vb_length;
-               if (unlikely(fr_value_box_mem_alloc(ctx, &buf, dst, dst->enumv, len, a->tainted | b->tainted) < 0)) goto oom;
+               buf = talloc_array(ctx, uint8_t, len);
+               if (!buf) goto oom;
+
                memcpy(buf, a->vb_strvalue, len);
+
+               fr_value_box_clear_value(dst);
+               fr_value_box_memdup_shallow(dst, dst->enumv, buf, len, a->tainted | b->tainted);
                break;
 
        case T_AND:
@@ -437,17 +450,29 @@ static int calc_octets(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t cons
                        return -1;
                }
 
-               if (unlikely(fr_value_box_mem_alloc(ctx, &buf, dst, dst->enumv, a->vb_length, a->tainted | b->tainted) < 0)) goto oom;
+               buf = talloc_array(ctx, uint8_t, a->vb_length);
+               if (!buf) goto oom;
 
-               for (len = 0; len < a->vb_length; len++) buf[len] = a->vb_octets[len] & b->vb_octets[len];
+               for (len = 0; len < a->vb_length; len++) {
+                       buf[len] = a->vb_octets[len] & b->vb_octets[len];
+               }
+
+               fr_value_box_clear_value(dst);
+               fr_value_box_memdup_shallow(dst, dst->enumv, buf, a->vb_length, a->tainted | b->tainted);
                break;
 
        case T_OR:
                if (a->vb_length != b->vb_length) goto length_error;
 
-               if (unlikely(fr_value_box_mem_alloc(ctx, &buf, dst, dst->enumv, a->vb_length, a->tainted | b->tainted) < 0)) goto oom;
+               buf = talloc_array(ctx, uint8_t, a->vb_length);
+               if (!buf) goto oom;
 
-               for (len = 0; len < a->vb_length; len++) buf[len] = a->vb_octets[len] | b->vb_octets[len];
+               for (len = 0; len < a->vb_length; len++) {
+                       buf[len] = a->vb_octets[len] | b->vb_octets[len];
+               }
+
+               fr_value_box_clear_value(dst);
+               fr_value_box_memdup_shallow(dst, dst->enumv, buf, a->vb_length, a->tainted | b->tainted);
                break;
 
        default:
@@ -482,7 +507,8 @@ static int calc_string(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t cons
 
        switch (op) {
        case T_OP_PREPEND:      /* dst = b . a */
-               if (unlikely(fr_value_box_bstr_alloc(ctx, &buf, dst, dst->enumv, len, a->tainted | b->tainted) < 0)) {
+               buf = talloc_array(ctx, char, len + 1);
+               if (!buf) {
                oom:
                        fr_strerror_const("Out of memory");
                        return -1;
@@ -491,13 +517,21 @@ static int calc_string(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t cons
                memcpy(buf, b->vb_strvalue, b->vb_length);
                memcpy(buf + b->vb_length, a->vb_strvalue, a->vb_length);
                buf[a->vb_length + b->vb_length] = '\0';
+
+               fr_value_box_clear_value(dst);
+               fr_value_box_strdup_shallow(dst, dst->enumv, buf, a->tainted | b->tainted);
                break;
 
        case T_ADD:
-               if (unlikely(fr_value_box_bstr_alloc(ctx, &buf, dst, dst->enumv, len, a->tainted | b->tainted) < 0)) goto oom;
+               buf = talloc_array(ctx, char, len + 1);
+               if (!buf) goto oom;
 
                memcpy(buf, a->vb_strvalue, a->vb_length);
                memcpy(buf + a->vb_length, b->vb_strvalue, b->vb_length);
+               buf[a->vb_length + b->vb_length] = '\0';
+
+               fr_value_box_clear_value(dst);
+               fr_value_box_strdup_shallow(dst, dst->enumv, buf, a->tainted | b->tainted);
                break;
 
        case T_SUB:
@@ -515,9 +549,14 @@ static int calc_string(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t cons
                }
 
                len = a->vb_length - b->vb_length;
+               buf = talloc_array(ctx, char, len + 1);
+               if (!buf) goto oom;
 
-               if (unlikely(fr_value_box_bstr_alloc(ctx, &buf, dst, dst->enumv, len, a->tainted | b->tainted) < 0)) goto oom;
                memcpy(buf, a->vb_strvalue, len);
+               buf[len] = '\0';
+
+               fr_value_box_clear_value(dst);
+               fr_value_box_strdup_shallow(dst, dst->enumv, buf, a->tainted | b->tainted);
                break;
 
        default: