From: Arran Cudbard-Bell Date: Tue, 13 Jun 2017 19:54:10 +0000 (-0400) Subject: Add cf_data_add function for dealing with non-talloced data X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=20be516a618a72b0a6eb96824a887d5bdffab344;p=thirdparty%2Ffreeradius-server.git Add cf_data_add function for dealing with non-talloced data --- diff --git a/src/include/cf_util.h b/src/include/cf_util.h index 9f2ecf229e6..cf346f0a2e0 100644 --- a/src/include/cf_util.h +++ b/src/include/cf_util.h @@ -166,6 +166,9 @@ void *cf_data_value(CONF_DATA const *cd); #define cf_data_add(_cf, _data, _name, _free) _cf_data_add(CF_TO_ITEM(_cf), _data, _name, _free) CONF_DATA const *_cf_data_add(CONF_ITEM *ci, void const *data, char const *name, bool free); +#define cf_data_add_static(_cf, _data, _type, _name) _cf_data_add(CF_TO_ITEM(_cf), _data, #_type, _name) +CONF_DATA const *_cf_data_add_static(CONF_ITEM *ci, void const *data, char const *type, char const *name); + #define cf_data_remove(_cf, _cd) _cf_data_remove(CF_TO_ITEM(_cf), _cd); void *_cf_data_remove(CONF_ITEM *ci, CONF_DATA const *_cd); diff --git a/src/main/cf_priv.h b/src/main/cf_priv.h index 61071308802..fe43b050e27 100644 --- a/src/main/cf_priv.h +++ b/src/main/cf_priv.h @@ -115,6 +115,7 @@ struct cf_data { char const *name; //!< Additional qualification of type. void const *data; //!< User data. + bool is_talloced; //!< If true we can do extra checks. bool free; //!< If true, free data with talloc if parent node is freed. }; diff --git a/src/main/cf_util.c b/src/main/cf_util.c index b79770c7663..81144b6c949 100644 --- a/src/main/cf_util.c +++ b/src/main/cf_util.c @@ -1253,14 +1253,15 @@ static int _cd_free(CONF_DATA *cd) /** Allocate a new user data container * * @param[in] parent #CONF_PAIR, or #CONF_SECTION to hang CONF_DATA off of. - * @param[in] name String identifier of the user data. * @param[in] data being added. + * @param[in] type of data being added. + * @param[in] name String identifier of the user data. * @param[in] do_free function, called when the parent #CONF_SECTION is being freed. * @return * - CONF_DATA on success. * - NULL on error. */ -static CONF_DATA *cf_data_alloc(CONF_ITEM *parent, void const *data, char const *name, bool do_free) +static CONF_DATA *cf_data_alloc(CONF_ITEM *parent, void const *data, char const *type, char const *name, bool do_free) { CONF_DATA *cd; @@ -1277,7 +1278,7 @@ static CONF_DATA *cf_data_alloc(CONF_ITEM *parent, void const *data, char const * explosions. */ if (data) { - cd->type = talloc_typed_strdup(cd, talloc_get_name(data)); + cd->type = talloc_typed_strdup(cd, type); cd->data = data; } if (name) cd->name = talloc_typed_strdup(cd, name); @@ -1340,7 +1341,7 @@ void *cf_data_value(CONF_DATA const *cd) return to_return; } -/** Add user data to a config section +/** Add talloced user data to a config section * * @param[in] ci to add data to. * @param[in] data to add. @@ -1367,13 +1368,47 @@ CONF_DATA const *_cf_data_add(CONF_ITEM *ci, void const *data, char const *name, return NULL; } - cd = cf_data_alloc(ci, data, name, do_free); + cd = cf_data_alloc(ci, data, type, name, do_free); + if (!cd) { + cf_log_err(ci, "Failed allocating data"); + return NULL; + } + cd->is_talloced = true; + + cf_item_add(ci, cd); + + return cd; +} + +/** Add non-talloced user data to a config section + * + * @param[in] ci to add data to. + * @param[in] data to add. + * @param[in] type identifier of the user data. + * @param[in] name String identifier of the user data. + * - #CONF_DATA - opaque handle to the stored data - on success. + * - NULL error. + */ +CONF_DATA const *_cf_data_add_static(CONF_ITEM *ci, void const *data, char const *type, char const *name) +{ + CONF_DATA *cd; + + /* + * Already exists. Can't add it. + */ + if (_cf_data_find(ci, type, name)) { + cf_log_err(ci, "Data of type %s with name %s already exists", type, name); + return NULL; + } + + cd = cf_data_alloc(ci, data, type, name, false); if (!cd) { cf_log_err(ci, "Failed allocating data"); return NULL; } + cd->is_talloced = false; - cf_item_add(ci, cf_data_to_item(cd)); + cf_item_add(ci, cd); return cd; }