}
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
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);
char const *p, *start, *end;
char *endptr;
- char *buff, *buff_p;
+ char *buff_p;
unsigned int result;
unsigned int reps;
size_t outlen = 0;
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;
default:
REDEBUG("Invalid character class '%c'", *p);
- talloc_free(buff);
+ talloc_free(vb);
return XLAT_ACTION_FAIL;
}
*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;
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;
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;
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;
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);
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;
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++));
*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;
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;
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;
*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;
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;
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;
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;
}
}
*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;
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 */
/*
* 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);
} 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;
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);
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;
* - -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;
size_t ciphertext_len;
char *plaintext;
- size_t plaintext_len;
+ size_t plaintext_len_est, plaintext_len;
fr_value_box_t *vb;
* 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;
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);
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);
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,
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;
}