]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
fix fr_pair_reinit_from_da()
authorAlan T. DeKok <aland@freeradius.org>
Tue, 5 Apr 2022 12:45:44 +0000 (08:45 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 5 Apr 2022 12:58:54 +0000 (08:58 -0400)
it may be called with a VP which has been created via
fr_pair_alloc_null(), in which case vp->da is NULL.

src/lib/util/pair.c

index cdcfdd66ba28d2989494a7d8e4eb2ee45e547b2c..992c39cc567194e7512f56feaefa0fc9e4a302ca 100644 (file)
@@ -328,17 +328,23 @@ fr_pair_t *fr_pair_afrom_da_with_pool(TALLOC_CTX *ctx, fr_dict_attr_t const *da,
  */
 int fr_pair_reinit_from_da(fr_pair_list_t *list, fr_pair_t *vp, fr_dict_attr_t const *da)
 {
-       if (vp->da == da) return 0;
-
-       if ((da->type != vp->da->type) && (fr_value_box_cast_in_place(vp, &vp->data, da->type, da) < 0)) return -1;
+       fr_dict_attr_t const *to_free;
 
        /*
-        *      Only frees unknown fr_dict_attr_t's
+        *      vp may be created from fr_pair_alloc_null(), in which case it has no da.
         */
-       fr_dict_unknown_free(&vp->da);
+       if (vp->da) {
+               if (vp->da == da) return 0;
+
+               if ((da->type != vp->da->type) && (fr_value_box_cast_in_place(vp, &vp->data, da->type, da) < 0)) return -1;
+       } else {
+               fr_value_box_init(&vp->data, da->type, da, false);
+       }
+
+       to_free = vp->da;
 
        /*
-        *      Ensure we update the attr index in the parent
+        *      Ensure we update the attribute index in the parent.
         */
        if (list) {
                fr_pair_remove(list, vp);
@@ -350,6 +356,11 @@ int fr_pair_reinit_from_da(fr_pair_list_t *list, fr_pair_t *vp, fr_dict_attr_t c
                vp->da = da;
        }
 
+       /*
+        *      Only frees unknown fr_dict_attr_t's
+        */
+       fr_dict_unknown_free(&to_free);
+
        return 0;
 }