]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Allow %substr() to work on any data type
authorNick Porter <nick@portercomputing.co.uk>
Wed, 21 Feb 2024 09:05:14 +0000 (09:05 +0000)
committerNick Porter <nick@portercomputing.co.uk>
Wed, 21 Feb 2024 09:05:14 +0000 (09:05 +0000)
src/lib/unlang/xlat_builtin.c
src/tests/keywords/substr

index 816c73310c13df460f890d4222eced95a3a7df69..6802717897dd55245359ecb0ac94721cf5c0f305 100644 (file)
@@ -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;
index cb422869daa4360f0d97dbce8e91937efe35acd1..f060f97482e3edff0379dc791c4c8e1914a1ebd8 100644 (file)
@@ -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
 }