From: Arran Cudbard-Bell Date: Wed, 28 Mar 2018 19:14:56 +0000 (+0100) Subject: Add talloc type verification to fifos X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf715e0ca780fac6bd8cf9bee69913985c1b147c;p=thirdparty%2Ffreeradius-server.git Add talloc type verification to fifos --- diff --git a/src/include/libradius.h b/src/include/libradius.h index 7210bd410a5..a1c8b016049 100644 --- a/src/include/libradius.h +++ b/src/include/libradius.h @@ -301,7 +301,39 @@ int fr_crypt_check(char const *password, char const *reference_crypt); */ typedef struct fr_fifo_t fr_fifo_t; typedef void (*fr_fifo_free_t)(void *); -fr_fifo_t *fr_fifo_create(TALLOC_CTX *ctx, int max_entries, fr_fifo_free_t freeNode); + +/** Creates a fifo that verifies elements are of a specific talloc type + * + * @param[in] _ctx to tie fifo lifetime to. + * If ctx is freed, fifo will free any nodes, calling the + * free function if set. + * @param[in] _max_entries Maximum number of entries. + * @param[in] _talloc_type of elements. + * @param[in] _node_free Optional function used to free data if tree nodes are + * deleted or replaced. + * @return + * - A new fifo on success. + * - NULL on failure. + */ +#define fr_fifo_talloc_create(_ctx, _talloc_type, _max_entries, _free_node) \ + _fr_fifo_create(_ctx, #_talloc_type, _max_entries, _free_node) + +/** Creates a fifo + * + * @param[in] _ctx to tie fifo lifetime to. + * If ctx is freed, fifo will free any nodes, calling the + * free function if set. + * @param[in] _max_entries Maximum number of entries. + * @param[in] _node_free Optional function used to free data if tree nodes are + * deleted or replaced. + * @return + * - A new fifo on success. + * - NULL on failure. + */ +#define fr_fifo_create(_ctx, _max_entries, _free_node) \ + _fr_fifo_create(_ctx, NULL, _max_entries, _free_node) + +fr_fifo_t *_fr_fifo_create(TALLOC_CTX *ctx, char const *type, int max_entries, fr_fifo_free_t free_node); int fr_fifo_push(fr_fifo_t *fi, void *data); void *fr_fifo_pop(fr_fifo_t *fi); void *fr_fifo_peek(fr_fifo_t *fi); diff --git a/src/include/rbtree.h b/src/include/rbtree.h index 1a71949ecdd..b9ebd8c4afa 100644 --- a/src/include/rbtree.h +++ b/src/include/rbtree.h @@ -59,6 +59,9 @@ typedef void (*rb_free_t)(void *data); * @param[in] _node_free Optional function used to free data if tree nodes are * deleted or replaced. * @param[in] _flags To modify tree behaviour. + * @return + * - A new rbtree on success. + * - NULL on failure. */ #define rbtree_talloc_create(_ctx, _cmp, _talloc_type, _node_free, _flags) \ _rbtree_create(_ctx, _cmp, #_talloc_type, _node_free, _flags) diff --git a/src/lib/util/fifo.c b/src/lib/util/fifo.c index 5d7337274b6..6817f01fb4b 100644 --- a/src/lib/util/fifo.c +++ b/src/lib/util/fifo.c @@ -31,6 +31,8 @@ struct fr_fifo_t { unsigned int max; //!< How many elements were created in the fifo. fr_fifo_free_t free_node; //!< Function to call to free nodes when the fifo is freed. + char const *type; //!< Type of elements. + void *data[1]; }; @@ -71,13 +73,14 @@ static int _fifo_free(fr_fifo_t *fi) * the callers must synchronise their access. * * @param[in] ctx to allocate fifo array in. + * @param[in] type Talloc type of elements (may be NULL). * @param[in] max The maximum number of elements allowed. * @param[in] free_node Function to use to free node data if the fifo is freed. * @return * - A new fifo queue. * - NULL on error. */ -fr_fifo_t *fr_fifo_create(TALLOC_CTX *ctx, int max, fr_fifo_free_t free_node) +fr_fifo_t *_fr_fifo_create(TALLOC_CTX *ctx, char const *type, int max, fr_fifo_free_t free_node) { fr_fifo_t *fi; @@ -89,6 +92,7 @@ fr_fifo_t *fr_fifo_create(TALLOC_CTX *ctx, int max, fr_fifo_free_t free_node) talloc_set_destructor(fi, _fifo_free); fi->max = max; + fi->type = type; fi->free_node = free_node; return fi; @@ -108,6 +112,10 @@ int fr_fifo_push(fr_fifo_t *fi, void *data) if (fi->num >= fi->max) return -1; +#ifndef TALLOC_GET_TYPE_ABORT_NOOP + if (fi->type) _talloc_get_type_abort(data, fi->type, __location__); +#endif + fi->data[fi->last++] = data; if (fi->last >= fi->max) fi->last = 0; fi->num++;