]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
xlat: Fix xlat alternations that use functions
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 21 Aug 2023 22:58:16 +0000 (16:58 -0600)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 21 Aug 2023 22:58:34 +0000 (16:58 -0600)
src/lib/unlang/xlat_alloc.c
src/tests/keywords/xlat-alternation-with-func [new file with mode: 0644]

index 03b05f218b7c2cf1674dbad5f62d881c2c6d60df..29dfbd67863d155629c9495be2d8b0c910fd25d8 100644 (file)
@@ -258,8 +258,15 @@ static int _xlat_copy_internal(NDEBUG_LOCATION_ARGS TALLOC_CTX *ctx, xlat_exp_he
                 *      Ensure the format string is valid...  At this point
                 *      they should all be talloc'd strings.
                 */
-               MEM(node = xlat_exp_alloc(ctx, p->type,
-                                         talloc_get_type_abort_const(p->fmt, char), talloc_array_length(p->fmt) - 1));
+               if (p->type == XLAT_ALTERNATE) {
+                       /* Alternates don't have format strings */
+                       MEM(node = xlat_exp_alloc_null(ctx));
+                       xlat_exp_set_type(node, XLAT_ALTERNATE);
+               } else {
+                       MEM(node = xlat_exp_alloc(ctx, p->type,
+                                                 talloc_get_type_abort_const(p->fmt, char), talloc_array_length(p->fmt) - 1));
+               }
+
                node->quote = p->quote;
                node->flags = p->flags;
 
diff --git a/src/tests/keywords/xlat-alternation-with-func b/src/tests/keywords/xlat-alternation-with-func
new file mode 100644 (file)
index 0000000..4a5506c
--- /dev/null
@@ -0,0 +1,32 @@
+#
+# PRE:
+#
+
+&Tmp-String-0 := "foo"
+&Tmp-String-1 := "bar"
+
+if (!(%{%(test.passthrough:%{Tmp-String-0}):-%{Tmp-String-1}} == 'foo')) {
+       test_fail
+}
+
+if (!(%{%(test.passthrough:%{Tmp-String-2}):-%{Tmp-String-1}} == 'bar')) {
+       test_fail
+}
+
+if (!(%{%{Tmp-String-0}:-%(test.passthrough:%{Tmp-String-1})} == 'foo')) {
+       test_fail
+}
+
+if (!(%{%{Tmp-String-2}:-%(test.passthrough:%{Tmp-String-1})} == 'bar')) {
+       test_fail
+}
+
+if (!(%{%(test.passthrough:%{Tmp-String-0}):-%(test.passthrough:%{Tmp-String-1})} == 'foo')) {
+       test_fail
+}
+
+if (!(%{%(test.passthrough:%{Tmp-String-2}):-%(test.passthrough:%{Tmp-String-1})} == 'bar')) {
+       test_fail
+}
+
+success