From: Arran Cudbard-Bell Date: Mon, 14 Oct 2024 01:47:07 +0000 (-0400) Subject: Keep a list of all files we saw during loading the dictionary X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e9672e65b4f00f73c00645dee31399ff815874d6;p=thirdparty%2Ffreeradius-server.git Keep a list of all files we saw during loading the dictionary This will be used later to record the files attributes were defined in, with buffer lifetimes that are equal to the attribute lifetimes --- diff --git a/src/lib/util/dict_priv.h b/src/lib/util/dict_priv.h index 9adf4aaca5e..c7279ee3e55 100644 --- a/src/lib/util/dict_priv.h +++ b/src/lib/util/dict_priv.h @@ -62,6 +62,16 @@ typedef struct { char const *dependent; //!< File holding the reference. } fr_dict_dependent_t; +/** Entry in the filename list of files associated with this dictionary + * + * Mainly used for debugging. + */ +typedef struct { + fr_dlist_t entry; //!< Entry in the list of filenames. + + char *filename; //!< Name of the file the dictionary was loaded on. +} fr_dict_filename_t; + /** Vendors and attribute names * * It's very likely that the same vendors will operate in multiple @@ -73,6 +83,9 @@ typedef struct { struct fr_dict_s { fr_dict_gctx_t *gctx; //!< Global dictionary context this dictionary ///< was allocated in. + + fr_dlist_head_t filenames; //!< Files that this dictionary was loaded from. + bool read_only; //!< If true, disallow modifications. bool in_protocol_by_name; //!< Whether the dictionary has been inserted into the diff --git a/src/lib/util/dict_tokenize.c b/src/lib/util/dict_tokenize.c index b53c6ee22fc..e9c9333604e 100644 --- a/src/lib/util/dict_tokenize.c +++ b/src/lib/util/dict_tokenize.c @@ -2008,6 +2008,30 @@ static int dict_finalise(dict_tokenize_ctx_t *ctx) return 0; } +/** Maintain a linked list of filenames which we've seen loading this dictionary + * + * This is used for debug messages, so we have a copy of the original file path + * that we can reference from fr_dict_attr_t without having the memory bloat of + * assigning a buffer to every attribute. + */ +static inline int dict_filename_add(char **filename_out, fr_dict_t *dict, char const *filename) +{ + fr_dict_filename_t *file; + + file = talloc_zero(dict, fr_dict_filename_t); + if (unlikely(file == NULL)) { + oom: + fr_strerror_const("Out of memory"); + return -1; + } + *filename_out = file->filename = talloc_typed_strdup(dict, filename); + if (unlikely(*filename_out == NULL)) goto oom; + + fr_dlist_insert_tail(&dict->filenames, file); + + return 0; +} + /** Parse a dictionary file * * @param[in] ctx Contains the current state of the dictionary parser. @@ -2091,7 +2115,13 @@ static int _dict_from_file(dict_tokenize_ctx_t *ctx, } } - ctx->stack[ctx->stack_depth].filename = fn; + /* + * Copy the filename into the dictionary and add it to the ctx + * + * This string is safe to assign to the filename pointer in + * any attributes added beneath the dictionary. + */ + if (unlikely(dict_filename_add(&ctx->stack[ctx->stack_depth].filename, ctx->dict, fn) < 0)) return -1; if ((fp = fopen(fn, "r")) == NULL) { if (!src_file) { diff --git a/src/lib/util/dict_util.c b/src/lib/util/dict_util.c index 083d46b5aa8..bfa7be79a77 100644 --- a/src/lib/util/dict_util.c +++ b/src/lib/util/dict_util.c @@ -29,6 +29,7 @@ RCSID("$Id$") #include #include #include +#include #include #include #include @@ -3597,6 +3598,11 @@ fr_dict_t *dict_alloc(TALLOC_CTX *ctx) dict->gctx = dict_gctx; /* Record which global context this was allocated in */ talloc_set_destructor(dict, _dict_free); + /* + * A list of all the files that constitute this dictionary + */ + fr_dlist_talloc_init(&dict->filenames, fr_dict_filename_t, entry); + /* * Pre-Allocate pool memory for rapid startup * As that's the working memory required during