]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add talloc type verification to fifos
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 28 Mar 2018 19:14:56 +0000 (20:14 +0100)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 28 Mar 2018 19:14:56 +0000 (20:14 +0100)
src/include/libradius.h
src/include/rbtree.h
src/lib/util/fifo.c

index 7210bd410a5102ca7efd822c3a6f01a3c64ff3ba..a1c8b016049fbc5fb7babc79962404e44fcc91d6 100644 (file)
@@ -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);
index 1a71949ecdd0755935139be9b34065e850be8d34..b9ebd8c4afa06824076e145c3bc66b7ca4326b29 100644 (file)
@@ -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)
index 5d7337274b65825e8015a8ea69ed476367affccf..6817f01fb4bd3f463739494a49c82c77d3465502 100644 (file)
@@ -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++;