*/
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);
vp->da = da;
}
+ /*
+ * Only frees unknown fr_dict_attr_t's
+ */
+ fr_dict_unknown_free(&to_free);
+
return 0;
}