]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Keep a list of all files we saw during loading the dictionary
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 14 Oct 2024 01:47:07 +0000 (21:47 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 14 Oct 2024 01:50:21 +0000 (21:50 -0400)
This will be used later to record the files attributes were defined in, with buffer lifetimes that are equal to the attribute lifetimes

src/lib/util/dict_priv.h
src/lib/util/dict_tokenize.c
src/lib/util/dict_util.c

index 9adf4aaca5ead0dfc5a9f5914f09cebbd7f5fd9d..c7279ee3e55209207c35d1d157bf5eb77dd5627e 100644 (file)
@@ -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
index b53c6ee22fc177bfa65041c338457f6422d6b160..e9c9333604eeb1debf8f6ef17555c6b4d59020a3 100644 (file)
@@ -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) {
index 083d46b5aa894bb301ac26df38d996d2ab4c432f..bfa7be79a7754a1209c2dc5c5282b5fbe2a294a9 100644 (file)
@@ -29,6 +29,7 @@ RCSID("$Id$")
 #include <freeradius-devel/util/dict.h>
 #include <freeradius-devel/util/dict_ext_priv.h>
 #include <freeradius-devel/util/dict_fixup_priv.h>
+#include <freeradius-devel/util/dlist.h>
 #include <freeradius-devel/util/proto.h>
 #include <freeradius-devel/util/rand.h>
 #include <freeradius-devel/util/sbuff.h>
@@ -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