]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
value: Remove some calls to fr_value_box_bstrsteal
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 25 May 2020 00:05:47 +0000 (19:05 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 25 May 2020 00:05:47 +0000 (19:05 -0500)
src/lib/json/json.c
src/lib/unlang/xlat_builtin.c
src/lib/unlang/xlat_eval.c
src/lib/util/value.c
src/modules/rlm_cipher/rlm_cipher.c
src/modules/rlm_json/rlm_json.c
src/modules/rlm_wimax/rlm_wimax.c

index 10c3a784d27839caad7e348207966a63412d6577..613b80d61f4153ef4c455246d7ec0be5032ad354 100644 (file)
@@ -1011,7 +1011,7 @@ char *fr_json_afrom_pair_list(TALLOC_CTX *ctx, VALUE_PAIR *vps,
        }
 
        MEM(p = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
-       MEM(out = talloc_strdup(ctx, p));
+       MEM(out = talloc_typed_strdup(ctx, p));
 
        /*
         * Free the JSON structure, it's not needed any more
index bea6fad9d9884dbd34820ff0bef81af1f5b389cd..3d28c45690f4ecf734f62a5da18a16ec3025b41c 100644 (file)
@@ -1818,7 +1818,7 @@ static xlat_action_t xlat_func_concat(TALLOC_CTX *ctx, fr_cursor_t *out,
        buff = fr_value_box_list_asprint(result, (*in)->next, sep, '\0');
        if (!buff) goto error;
 
-       fr_value_box_bstrsteal(result, result, NULL, buff, fr_value_box_list_tainted((*in)->next));
+       fr_value_box_bstrdup_buffer_shallow(NULL, result, NULL, buff, fr_value_box_list_tainted((*in)->next));
 
        fr_cursor_append(out, result);
 
@@ -2318,7 +2318,7 @@ static xlat_action_t xlat_func_randstr(TALLOC_CTX *ctx, fr_cursor_t *out,
 
        char const      *p, *start, *end;
        char            *endptr;
-       char            *buff, *buff_p;
+       char            *buff_p;
        unsigned int    result;
        unsigned int    reps;
        size_t          outlen = 0;
@@ -2366,7 +2366,8 @@ static xlat_action_t xlat_func_randstr(TALLOC_CTX *ctx, fr_cursor_t *out,
                p++;
        }
 
-       buff = buff_p = talloc_array(NULL, char, outlen + 1);
+       MEM(vb = fr_value_box_alloc_null(ctx));
+       MEM(fr_value_box_bstr_alloc(vb, &buff_p, vb, NULL, outlen, false) == 0);
 
        /* Reset p to start position */
        p = start;
@@ -2461,7 +2462,7 @@ static xlat_action_t xlat_func_randstr(TALLOC_CTX *ctx, fr_cursor_t *out,
 
                        default:
                                REDEBUG("Invalid character class '%c'", *p);
-                               talloc_free(buff);
+                               talloc_free(vb);
 
                                return XLAT_ACTION_FAIL;
                        }
@@ -2472,9 +2473,6 @@ static xlat_action_t xlat_func_randstr(TALLOC_CTX *ctx, fr_cursor_t *out,
 
        *buff_p++ = '\0';
 
-       MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL, false));
-       fr_value_box_bstrsteal(vb, vb, NULL, buff, false);
-
        fr_cursor_append(out, vb);
 
        return XLAT_ACTION_DONE;
@@ -2504,15 +2502,15 @@ static xlat_action_t xlat_func_regex(TALLOC_CTX *ctx, fr_cursor_t *out,
                fr_value_box_t  *vb;
                char            *p;
 
-               if (regex_request_to_sub(ctx, &p, request, 0) < 0) {
+               MEM(vb = fr_value_box_alloc_null(ctx));
+               if (regex_request_to_sub(vb, &p, request, 0) < 0) {
                        REDEBUG2("No previous regex capture");
+                       talloc_free(vb);
                        return XLAT_ACTION_FAIL;
                }
 
                fr_assert(p);
-
-               MEM(vb = fr_value_box_alloc_null(ctx));
-               fr_value_box_bstrsteal(vb, vb, NULL, p, false);
+               fr_value_box_bstrdup_buffer_shallow(NULL, vb, NULL, p, false);
                fr_cursor_append(out, vb);
 
                return XLAT_ACTION_DONE;
@@ -2539,15 +2537,14 @@ static xlat_action_t xlat_func_regex(TALLOC_CTX *ctx, fr_cursor_t *out,
                        return XLAT_ACTION_FAIL;
                }
 
-               if (regex_request_to_sub(ctx, &p, request, idx.vb_uint32) < 0) {
+               MEM(vb = fr_value_box_alloc_null(ctx));
+               if (regex_request_to_sub(vb, &p, request, idx.vb_uint32) < 0) {
                        REDEBUG2("No previous numbered regex capture group");
+                       talloc_free(vb);
                        return XLAT_ACTION_FAIL;
                }
-
                fr_assert(p);
-
-               MEM(vb = fr_value_box_alloc_null(ctx));
-               fr_value_box_bstrsteal(vb, vb, NULL, p, false);
+               fr_value_box_bstrdup_buffer_shallow(NULL, vb, NULL, p, false);
                fr_cursor_append(out, vb);
 
                return XLAT_ACTION_DONE;
@@ -2566,15 +2563,15 @@ static xlat_action_t xlat_func_regex(TALLOC_CTX *ctx, fr_cursor_t *out,
                        return XLAT_ACTION_FAIL;
                }
 
-               if (regex_request_to_sub_named(request, &p, request, (*in)->vb_strvalue) < 0) {
+               MEM(vb = fr_value_box_alloc_null(ctx));
+               if (regex_request_to_sub_named(vb, &p, request, (*in)->vb_strvalue) < 0) {
                        REDEBUG2("No previous named regex capture group");
+                       talloc_free(vb);
                        return XLAT_ACTION_FAIL;
                }
 
                fr_assert(p);
-
-               MEM(vb = fr_value_box_alloc_null(ctx));
-               fr_value_box_bstrsteal(vb, vb, NULL, p, false);
+               fr_value_box_bstrdup_buffer_shallow(NULL, vb, NULL, p, false);
                fr_cursor_append(out, vb);
 
                return XLAT_ACTION_DONE;
@@ -2900,7 +2897,7 @@ static xlat_action_t xlat_func_sub_regex(TALLOC_CTX *ctx, fr_cursor_t *out,
                talloc_free(pattern);
                return XLAT_ACTION_FAIL;
        }
-       fr_value_box_bstrsteal(vb, vb, NULL, buff, (*in)->tainted);
+       fr_value_box_bstrdup_buffer_shallow(NULL, vb, NULL, buff, (*in)->tainted);
 
        fr_cursor_append(out, vb);
 
@@ -3086,7 +3083,7 @@ static xlat_action_t xlat_func_tag(TALLOC_CTX *ctx, fr_cursor_t *out,
 static xlat_action_t xlat_change_case(TALLOC_CTX *ctx, fr_cursor_t *out,
                                       REQUEST *request, fr_value_box_t **in, bool upper)
 {
-       char            *buff, *buff_p;
+       char            *buff_p;
        char const      *p, *end;
        fr_value_box_t  *vb;
 
@@ -3106,7 +3103,8 @@ static xlat_action_t xlat_change_case(TALLOC_CTX *ctx, fr_cursor_t *out,
        p = (*in)->vb_strvalue;
        end = p + (*in)->vb_length;
 
-       buff = buff_p = talloc_array(NULL, char, (*in)->vb_length + 1);
+       MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL, false));
+       MEM(fr_value_box_bstr_alloc(vb, &buff_p, vb, NULL, (*in)->vb_length, (*in)->tainted) == 0);
 
        while (p < end) {
                *(buff_p++) = upper ? toupper ((int) *(p++)) : tolower((int) *(p++));
@@ -3114,9 +3112,6 @@ static xlat_action_t xlat_change_case(TALLOC_CTX *ctx, fr_cursor_t *out,
 
        *buff_p = '\0';
 
-       MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL, false));
-       fr_value_box_bstrsteal(vb, vb, NULL, buff, false);
-
        fr_cursor_append(out, vb);
 
        return XLAT_ACTION_DONE;
@@ -3175,7 +3170,7 @@ static xlat_action_t xlat_func_urlquote(TALLOC_CTX *ctx, fr_cursor_t *out,
                                        fr_value_box_t **in)
 {
        char const      *p, *end;
-       char            *buff, *buff_p;
+       char            *buff_p;
        size_t          outlen = 0;
        fr_value_box_t  *vb;
 
@@ -3211,7 +3206,8 @@ static xlat_action_t xlat_func_urlquote(TALLOC_CTX *ctx, fr_cursor_t *out,
                p++;
        }
 
-       buff = buff_p = talloc_array(NULL, char, outlen + 1);
+       MEM(vb = fr_value_box_alloc_null(ctx));
+       MEM(fr_value_box_bstr_alloc(vb, &buff_p, vb, NULL, outlen, false) == 0);
 
        /* Reset p to start position */
        p = (*in)->vb_strvalue;
@@ -3240,9 +3236,6 @@ static xlat_action_t xlat_func_urlquote(TALLOC_CTX *ctx, fr_cursor_t *out,
 
        *buff_p = '\0';
 
-       MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL, false));
-       fr_value_box_bstrsteal(vb, vb, NULL, buff, false);
-
        fr_cursor_append(out, vb);
 
        return XLAT_ACTION_DONE;
@@ -3265,7 +3258,7 @@ static xlat_action_t xlat_func_urlunquote(TALLOC_CTX *ctx, fr_cursor_t *out,
                                          fr_value_box_t **in)
 {
        char const      *p, *end;
-       char            *buff, *buff_p;
+       char            *buff_p;
        char            *c1, *c2;
        size_t          outlen = 0;
        fr_value_box_t  *vb;
@@ -3298,7 +3291,8 @@ static xlat_action_t xlat_func_urlunquote(TALLOC_CTX *ctx, fr_cursor_t *out,
                outlen++;
        }
 
-       buff = buff_p = talloc_array(NULL, char, outlen + 1);
+       MEM(vb = fr_value_box_alloc_null(ctx));
+       MEM(fr_value_box_bstr_alloc(vb, &buff_p, vb, NULL, outlen, false) == 0);
 
        /* Reset p to start position */
        p = (*in)->vb_strvalue;
@@ -3314,7 +3308,7 @@ static xlat_action_t xlat_func_urlunquote(TALLOC_CTX *ctx, fr_cursor_t *out,
                if (!(c1 = memchr(hextab, tolower(*++p), 16)) ||
                    !(c2 = memchr(hextab, tolower(*++p), 16))) {
                        REMARKER((*in)->vb_strvalue, p - (*in)->vb_strvalue, "Non-hex char in %% sequence");
-                       talloc_free(buff);
+                       talloc_free(vb);
 
                        return XLAT_ACTION_FAIL;
                }
@@ -3323,10 +3317,6 @@ static xlat_action_t xlat_func_urlunquote(TALLOC_CTX *ctx, fr_cursor_t *out,
        }
 
        *buff_p = '\0';
-
-       MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL, false));
-       fr_value_box_bstrsteal(vb, vb, NULL, buff, false);
-
        fr_cursor_append(out, vb);
 
        return XLAT_ACTION_DONE;
index 3afc6205bf6fe31844903862accf7024c88e4f76..a05e4c8a25ebe84ddcd321f08adf59358a385237 100644 (file)
@@ -768,25 +768,26 @@ xlat_action_t xlat_frame_eval_repeat(TALLOC_CTX *ctx, fr_cursor_t *out,
                                result_str = talloc_typed_strdup(NULL, "");
                        }
 
+                       MEM(value = fr_value_box_alloc_null(ctx));
                        if (node->xlat->buf_len > 0) {
-                               str = talloc_array(ctx, char, node->xlat->buf_len);
-                               str[0] = '\0';  /* Be sure the string is \0 terminated */
+                               fr_value_box_bstr_alloc(value, &str, value, NULL, node->xlat->buf_len, false);
                        }
 
                        XLAT_DEBUG("** [%i] %s(func) - %%{%s:%pV}", unlang_interpret_stack_depth(request), __FUNCTION__,
                                   node->fmt,
                                   fr_box_strvalue_len(result_str, talloc_array_length(result_str) - 1));
 
-                       slen = node->xlat->func.sync(ctx, &str, node->xlat->buf_len,
+                       slen = node->xlat->func.sync(value, &str, node->xlat->buf_len,
                                                     node->xlat->mod_inst, NULL, request, result_str);
                        xlat_debug_log_expansion(request, *in, *result);
                        if (slen < 0) {
+                               talloc_free(value);
                                talloc_free(result_str);
-                               talloc_free(str);
                                return XLAT_ACTION_FAIL;
                        }
                        if (slen == 0) {                                /* Zero length result */
                                talloc_free(result_str);
+                               talloc_free(value);
                                break;
                        }
                        (void)talloc_get_type_abort(str, char);         /* Check output buffer is sane */
@@ -794,16 +795,12 @@ xlat_action_t xlat_frame_eval_repeat(TALLOC_CTX *ctx, fr_cursor_t *out,
                        /*
                         *      Shrink the buffer
                         */
-                       if ((node->xlat->buf_len > 0) && (slen > 0)) {
-                               MEM(str = talloc_bstr_realloc(ctx, str, (size_t)slen));
+                       if (node->xlat->buf_len > 0) {
+                               if (slen > 0) fr_value_box_bstr_realloc(value, &str, value, slen);
+                       } else {
+                               fr_value_box_bstrdup_buffer_shallow(NULL, value, NULL, str, false);
                        }
 
-                       /*
-                        *      Fixup talloc lineage and assign the
-                        *      output of the function to a box.
-                        */
-                       MEM(value = fr_value_box_alloc_null(ctx));
-                       fr_value_box_bstrsteal(value, value, NULL, str, false);
                        fr_cursor_append(out, value);                   /* Append the result of the expansion */
                        talloc_free(result_str);
                        xlat_debug_log_result(request, value);
@@ -929,7 +926,7 @@ xlat_action_t xlat_frame_eval_repeat(TALLOC_CTX *ctx, fr_cursor_t *out,
                        } else {
                                str = talloc_typed_strdup(value, "");
                        }
-               
+
                        fr_value_box_strdup_shallow(value, NULL, str, fr_value_box_list_tainted(*result));
                        talloc_free(*result);
                        *result = value;
@@ -1047,13 +1044,16 @@ xlat_action_t xlat_frame_eval(TALLOC_CTX *ctx, fr_cursor_t *out, xlat_exp_t cons
                                   node->fmt);
 
                        xlat_debug_log_expansion(request, node, NULL);
-                       slen = node->xlat->func.sync(ctx, &str, node->xlat->buf_len, node->xlat->mod_inst,
+                       MEM(value = fr_value_box_alloc_null(ctx));
+                       slen = node->xlat->func.sync(value, &str, node->xlat->buf_len, node->xlat->mod_inst,
                                                     NULL, request, NULL);
-                       if (slen < 0) goto fail;
+                       if (slen < 0) {
+                               talloc_free(value);
+                               goto fail;
+                       }
                        if (slen == 0) continue;
 
-                       MEM(value = fr_value_box_alloc_null(ctx));
-                       fr_value_box_bstrsteal(value, value, NULL, str, false);
+                       fr_value_box_bstrdup_buffer_shallow(NULL, value, NULL, str, false);
                        fr_cursor_append(out, value);
                        fr_cursor_next(out);
 
@@ -1094,15 +1094,12 @@ xlat_action_t xlat_frame_eval(TALLOC_CTX *ctx, fr_cursor_t *out, xlat_exp_t cons
 
                        XLAT_DEBUG("** [%i] %s(regex) - %%{%s}", unlang_interpret_stack_depth(request), __FUNCTION__,
                                   node->fmt);
-                       if (regex_request_to_sub(ctx, &str, request, node->regex_index) < 0) continue;
-
-                       /*
-                        *      Above call strdups the capture data, so
-                        *      we just need to fix up the talloc lineage
-                        *      and box it.
-                        */
                        MEM(value = fr_value_box_alloc_null(ctx));
-                       fr_value_box_bstrsteal(value, value, NULL, str, false);
+                       if (regex_request_to_sub(ctx, &str, request, node->regex_index) < 0) {
+                               talloc_free(value);
+                               continue;
+                       }
+                       fr_value_box_bstrdup_buffer_shallow(NULL, value, NULL, str, false);
                        fr_cursor_append(out, value);
                }
                        continue;
index 01650cce612d3a254f8977da31eb3810b38cc483..4052c544eba4ef6098098aa0b3768398654916ec 100644 (file)
@@ -3819,7 +3819,7 @@ void fr_value_box_bstrndup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *en
  *     - -1 on failure.
  */
 int fr_value_box_bstrdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
-                                      char const *src, bool tainted)
+                                       char const *src, bool tainted)
 {
        size_t  len;
 
index a0e1526db9f498987843114ff00b019cdab0db40..f060784785f1dba931ba8301b2e0d7f910d26893 100644 (file)
@@ -654,7 +654,7 @@ static xlat_action_t cipher_rsa_decrypt_xlat(TALLOC_CTX *ctx, fr_cursor_t *out,
        size_t                          ciphertext_len;
 
        char                            *plaintext;
-       size_t                          plaintext_len;
+       size_t                          plaintext_len_est, plaintext_len;
 
        fr_value_box_t                  *vb;
 
@@ -674,37 +674,22 @@ static xlat_action_t cipher_rsa_decrypt_xlat(TALLOC_CTX *ctx, fr_cursor_t *out,
         *      Decrypt the ciphertext
         */
        RHEXDUMP3(ciphertext, ciphertext_len, "Ciphertext (%zu bytes)", ciphertext_len);
-       if (EVP_PKEY_decrypt(xt->evp_decrypt_ctx, NULL, &plaintext_len, ciphertext, ciphertext_len) <= 0) {
+       if (EVP_PKEY_decrypt(xt->evp_decrypt_ctx, NULL, &plaintext_len_est, ciphertext, ciphertext_len) <= 0) {
                fr_tls_log_error(request, "Failed getting length of cleartext");
                return XLAT_ACTION_FAIL;
        }
+       plaintext_len = plaintext_len_est;
 
-       MEM(plaintext = talloc_array(ctx, char, plaintext_len + 1));
+       MEM(vb = fr_value_box_alloc_null(ctx));
+       MEM(fr_value_box_bstr_alloc(vb, &plaintext, vb, NULL, plaintext_len_est, true) == 0);
        if (EVP_PKEY_decrypt(xt->evp_decrypt_ctx, (unsigned char *)plaintext, &plaintext_len,
                             ciphertext, ciphertext_len) <= 0) {
                fr_tls_log_error(request, "Failed decrypting ciphertext");
+               talloc_free(vb);
                return XLAT_ACTION_FAIL;
        }
        RHEXDUMP3((uint8_t const *)plaintext, plaintext_len, "Plaintext (%zu bytes)", plaintext_len);
-
-       /*
-        *      Fixup the output buffer (and ensure it's \0 terminated)
-        */
-       {
-               char *n;
-
-               n = talloc_bstr_realloc(ctx, plaintext, plaintext_len);
-               if (unlikely(!n)) {
-                       REDEBUG("Failed shrinking plaintext buffer");
-                       talloc_free(plaintext);
-                       return XLAT_ACTION_FAIL;
-               }
-
-               plaintext = n;
-       }
-
-       MEM(vb = fr_value_box_alloc_null(ctx));
-       fr_value_box_bstrsteal(vb, vb, NULL, plaintext, false);
+       MEM(fr_value_box_bstr_realloc(vb, NULL, vb, plaintext_len) == 0);
        fr_cursor_append(out, vb);
 
        return XLAT_ACTION_DONE;
index 613f1129890ccf9f74521807c637b8752a834ca2..73033e4d3a66b9699071180e2b4d4093c5705a10 100644 (file)
@@ -115,15 +115,10 @@ static xlat_action_t json_quote_xlat(TALLOC_CTX *ctx, fr_cursor_t *out, REQUEST
 
        if (!(tmp = fr_json_from_string(vb, (*in)->vb_strvalue, false))) {
                REDEBUG("Unable to JSON-quote string");
-       error:
                talloc_free(vb);
                return XLAT_ACTION_FAIL;
        }
-
-       if (unlikely(fr_value_box_bstrsteal(vb, vb, NULL, tmp, false) < 0)) {
-               REDEBUG("Failed to allocate JSON string");
-               goto error;
-       }
+       fr_value_box_bstrdup_buffer_shallow(NULL, vb, NULL, tmp, false);
 
        fr_cursor_append(out, vb);
 
@@ -293,11 +288,7 @@ static xlat_action_t json_encode_xlat(TALLOC_CTX *ctx, fr_cursor_t *out, REQUEST
                REDEBUG("Failed to generate JSON string");
                goto error;
        }
-
-       if (unlikely(fr_value_box_bstrsteal(vb, vb, NULL, json_str, false) < 0)) {
-               REDEBUG("Failed to allocate JSON string");
-               goto error;
-       }
+       fr_value_box_bstrdup_buffer_shallow(NULL, vb, NULL, json_str, false);
 
        fr_cursor_append(out, vb);
        fr_pair_list_free(&json_vps);
index 3d0c3735f9d74dfc34b0318ad7390286a7d6bf3b..bdec2bf8e9fd7ed6089184ac49c746cae1d0e06d 100644 (file)
@@ -130,7 +130,8 @@ static rlm_rcode_t CC_HINT(nonnull) mod_authorize(UNUSED void *instance, UNUSED
 
                memcpy(buffer, vp->vp_strvalue, 6);
 
-               p = talloc_array(vp, char, (5 * 3) + 2 + 1);
+               MEM(fr_pair_value_bstr_realloc(vp, &p, (5 * 3) + 2) == 0);
+
                /*
                 *      RFC 3580 Section 3.20 says this is the preferred
                 *      format.  Everyone *SANE* is using this format,
@@ -141,9 +142,6 @@ static rlm_rcode_t CC_HINT(nonnull) mod_authorize(UNUSED void *instance, UNUSED
                        p[(i * 3) + 2] = '-';
                }
 
-               p[(5*3)+2] = '\0';
-               fr_pair_value_strsteal(vp, p);
-
                DEBUG2("Fixing WiMAX binary Calling-Station-Id to %pV", &vp->data);
                return RLM_MODULE_OK;
        }