]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Deal with empty xlat expansions in a sane way
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 26 Jan 2018 02:47:37 +0000 (19:47 -0700)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 26 Jan 2018 02:47:37 +0000 (19:47 -0700)
src/main/map.c
src/tests/keywords/null-value-assign [new file with mode: 0644]

index e56a5e9bb181b7eabdcd1b2880b394edd1bfa32b..95e98b083cde3ab27b2f8cefba23abea490b3369 100644 (file)
@@ -1080,21 +1080,34 @@ int map_to_list_mod(TALLOC_CTX *ctx, vp_list_mod_t **out,
                rad_assert(map->lhs->type == TMPL_TYPE_ATTR);
                rad_assert(map->lhs->tmpl_da);          /* We need to know which attribute to create */
 
-               n = map_list_mod_afrom_map(ctx, map_in, map);
-               if (!n) goto error;
-
                /*
-                *      Fixup zero length result to be an empty string
+                *      Empty value
                 */
                if (!rhs_result || !*rhs_result) {
-                       n_vb = fr_value_box_alloc(n->mod->rhs, FR_TYPE_STRING, NULL, false);
-                       if (!n_vb) goto error;
+                       switch (map->lhs->tmpl_da->type) {
+                       case FR_TYPE_STRING:
+                               n = map_list_mod_afrom_map(ctx, map_in, map);
+                               if (!n) goto error;
 
-                       if (fr_value_box_strdup(n_vb, n_vb, NULL, "", false) < 0) goto error;
-                       fr_cursor_append(&values, n_vb);
+                               n_vb = fr_value_box_alloc(n->mod->rhs, FR_TYPE_STRING, NULL, false);
+                               if (!n_vb) goto error;
+
+                               if (fr_value_box_strdup(n_vb, n_vb, NULL, "", false) < 0) goto error;
+                               fr_cursor_append(&values, n_vb);
+                               break;
+
+                       default:
+                               goto finish;    /* Hmm we could error out? */
+                       }
                        break;
                }
 
+               /*
+                *      Non-Empty value
+                */
+               n = map_list_mod_afrom_map(ctx, map_in, map);
+               if (!n) goto error;
+
                (void)fr_cursor_init(&from, rhs_result);
                while ((vb = fr_cursor_remove(&from))) {
                        if (vb->type != map->lhs->tmpl_da->type) {
@@ -1303,7 +1316,7 @@ int map_to_list_mod(TALLOC_CTX *ctx, vp_list_mod_t **out,
        talloc_free(head);
 
 finish:
-       *out = n;
+       if (n) *out = n;
 
        /*
         *      Reparent ephemeral LHS to the vp_list_mod_t.
@@ -1331,6 +1344,7 @@ static inline VALUE_PAIR *map_list_mod_to_vp(TALLOC_CTX *ctx, vp_tmpl_t const *a
                talloc_free(vp);
                return NULL;
        }
+       VP_VERIFY(vp);          /* Check we created something sane */
 
        return vp;
 }
diff --git a/src/tests/keywords/null-value-assign b/src/tests/keywords/null-value-assign
new file mode 100644 (file)
index 0000000..e30a292
--- /dev/null
@@ -0,0 +1,34 @@
+#
+#  Reply message doesn't exist, so Tmp-Integer-0 shouldn't be created
+#
+update request {
+       Tmp-Integer-0 := "%{Reply-Message}"
+}
+if (&Tmp-Integer-0) {
+       fail
+}
+
+#
+#  NULL valued strings get converted to empty length strings
+#
+update request {
+       Tmp-String-0 := "%{Reply-Message}"
+}
+if (!&Tmp-String-0) {
+       fail
+}
+if (&Tmp-String-0 != '') {
+       fail
+}
+
+#
+#  NULL valued octet strings get nothing
+#
+update request {
+       Tmp-Octets-0 := "%{Reply-Message}"
+}
+if (&Tmp-Octets-0) {
+       fail
+}
+
+success