From: Nick Porter Date: Wed, 21 Feb 2024 09:05:14 +0000 (+0000) Subject: Allow %substr() to work on any data type X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=357d15050bca7c850501c33a19eba4ab8f20207a;p=thirdparty%2Ffreeradius-server.git Allow %substr() to work on any data type --- diff --git a/src/lib/unlang/xlat_builtin.c b/src/lib/unlang/xlat_builtin.c index 816c73310c1..6802717897d 100644 --- a/src/lib/unlang/xlat_builtin.c +++ b/src/lib/unlang/xlat_builtin.c @@ -2974,6 +2974,8 @@ static xlat_arg_parser_t const xlat_func_substr_args[] = { }; /** Extract a substring from string / octets data + * + * Non string / octets data is cast to a string. * * Second parameter is start position, optional third parameter is length * Negative start / length count from RHS of data. @@ -2993,9 +2995,17 @@ static xlat_action_t xlat_func_substr(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED XLAT_ARGS(args, &in, &start_vb, &len_vb); - if (!((in->type == FR_TYPE_STRING) || (in->type == FR_TYPE_OCTETS))) { - RPEDEBUG("substr only valid for string or octets data"); - return XLAT_ACTION_FAIL; + switch (in->type) { + case FR_TYPE_OCTETS: + case FR_TYPE_STRING: + break; + + default: + if (fr_value_box_cast_in_place(in, in, FR_TYPE_STRING, NULL) < 0) { + RPEDEBUG("Failed casting value to string"); + return XLAT_ACTION_FAIL; + } + break; } if (start_vb->vb_int32 > (int32_t)in->vb_length) return XLAT_ACTION_DONE; diff --git a/src/tests/keywords/substr b/src/tests/keywords/substr index cb422869daa..f060f97482e 100644 --- a/src/tests/keywords/substr +++ b/src/tests/keywords/substr @@ -45,7 +45,7 @@ if !(%substr(%{test_octets}, 1, 2) == 0x2345) { test_fail } -if (%substr(%{test_int}, 1, 2)) { +if !(%substr(%{test_int}, 1, 2) == '23') { test_fail }