]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
remove parser context field from cfg_obj_t
authorColin Vidal <colin@isc.org>
Tue, 21 Oct 2025 13:41:55 +0000 (15:41 +0200)
committerEvan Hunt <each@isc.org>
Thu, 23 Oct 2025 19:59:58 +0000 (12:59 -0700)
cfg_obj_t doesn't store a pointer to its a parser context anymore,
and does not depend on the parser's lifecycle. Instead, it stores a
reference to its own memory context (and in principle, each node
could have different memory context). This also slightly simplifies
the _destroy API as there is no need to pass a context through it
anymore.

22 files changed:
bin/check/named-checkconf.c
bin/delv/delv.c
bin/dig/dighost.c
bin/dnssec/dnssec-keygen.c
bin/dnssec/dnssec-ksr.c
bin/named/config.c
bin/named/controlconf.c
bin/named/main.c
bin/named/server.c
bin/nsupdate/nsupdate.c
bin/plugins/filter-a.c
bin/plugins/filter-aaaa.c
bin/plugins/synthrecord.c
bin/rndc/rndc.c
bin/tests/system/hooks/driver/test-syncplugin.c
doc/misc/cfg_test.c
lib/isccfg/include/isccfg/cfg.h
lib/isccfg/include/isccfg/grammar.h
lib/isccfg/namedconf.c
lib/isccfg/parser.c
tests/isccfg/duration_test.c
tests/isccfg/parser_test.c

index a4da14d8e3b6bb7ef6840673e4356c9b42808edd..4f7bfcee56069ef6744ef25f9d2b651b0ce1fc86 100644 (file)
@@ -745,7 +745,7 @@ main(int argc, char **argv) {
 
 cleanup:
        if (config != NULL) {
-               cfg_obj_destroy(parser, &config);
+               cfg_obj_destroy(&config);
        }
 
        if (parser != NULL) {
index cfcc67fe5c03e777f5c655ff4fb15abc09e9b89e..997c9fb73bd99e2148733e193581c8056da7ce52 100644 (file)
@@ -862,7 +862,7 @@ setup_dnsseckeys(dns_client_t *client, dns_view_t *toview) {
 
 cleanup:
        if (bindkeys != NULL) {
-               cfg_obj_destroy(parser, &bindkeys);
+               cfg_obj_destroy(&bindkeys);
        }
        if (parser != NULL) {
                cfg_parser_destroy(&parser);
index c7d8016362678c30b4351373fcf9a8336e3d8a93..a79bf61f9dca597be96e0b5f734aa95b19541d47 100644 (file)
@@ -1119,7 +1119,7 @@ read_confkey(void) {
 cleanup:
        if (pctx != NULL) {
                if (file != NULL) {
-                       cfg_obj_destroy(pctx, &file);
+                       cfg_obj_destroy(&file);
                }
                cfg_parser_destroy(&pctx);
        }
index c7b4abe98abfb7f49d65442a4de1c57a5e61bc31..04b8a8056fafa8f27b2ee5e5d4bcc8b70bc32f92 100644 (file)
@@ -1199,7 +1199,7 @@ main(int argc, char **argv) {
                        }
 
                        dns_kasp_detach(&kasp);
-                       cfg_obj_destroy(parser, &config);
+                       cfg_obj_destroy(&config);
                        cfg_parser_destroy(&parser);
                }
        } else {
index 86eb1362fc3bdf3e39d19a1420732f74bfce2e6a..073bbda1a92de972739afa0bd0c26f51892bb54a 100644 (file)
@@ -187,7 +187,7 @@ getkasp(ksr_ctx_t *ksr, dns_kasp_t **kasp) {
        if (ISC_LIST_EMPTY(dns_kasp_keys(*kasp))) {
                fatal("dnssec-policy '%s' has no keys configured", ksr->policy);
        }
-       cfg_obj_destroy(parser, &config);
+       cfg_obj_destroy(&config);
        cfg_parser_destroy(&parser);
 }
 
index ce0c17ac4190064dd5320694e016995ef6d02c4b..9268185444d91af60fd365aab2cb4bf40cd3d72f 100644 (file)
@@ -457,7 +457,7 @@ named_config_parsefile(cfg_parser_t *parser, cfg_obj_t **conf) {
 
 cleanup:
        if (*conf) {
-               cfg_obj_destroy(parser, conf);
+               cfg_obj_destroy(conf);
        }
 
 out:
index 84003f86d6778c1ba0cc3f647fe8fb0790461dab..26b0fb91975898ce00e4767dd4ce58870d799c30 100644 (file)
@@ -861,7 +861,7 @@ cleanup:
                free_controlkey(keyid, mctx);
        }
        if (config != NULL) {
-               cfg_obj_destroy(pctx, &config);
+               cfg_obj_destroy(&config);
        }
        if (pctx != NULL) {
                cfg_parser_destroy(&pctx);
index ef466ba7a800e2f2159f6c6e9c6f36f822142e36..3c12ba0d1dc4c9955106887fa3a42e89e546b88e 100644 (file)
@@ -618,7 +618,7 @@ printversion(bool verbose) {
        if (cfg_obj_isstring(obj)) {
                printf("  geoip-directory:      %s\n", cfg_obj_asstring(obj));
        }
-       cfg_obj_destroy(parser, &config);
+       cfg_obj_destroy(&config);
        cfg_parser_destroy(&parser);
        isc_mem_detach(&geoip_mctx);
 #endif /* HAVE_GEOIP2 */
index 82072750be846e3655e414ba8a8d524724a7597a..d8654be66d2821a15be93f0e23fe57ffff466d68 100644 (file)
@@ -2544,7 +2544,7 @@ cleanup:
                dns_zone_detach(&zone);
        }
        if (zoneconf != NULL) {
-               cfg_obj_destroy(cfg->add_parser, &zoneconf);
+               cfg_obj_destroy(&zoneconf);
        }
        if (dnsforwarders != NULL) {
                dns_forwarders_detach(&dnsforwarders);
@@ -2783,7 +2783,7 @@ catz_reconfigure(dns_catz_entry_t *entry, void *arg1, void *arg2) {
 
 cleanup:
        if (zoneconf != NULL) {
-               cfg_obj_destroy(cfg->add_parser, &zoneconf);
+               cfg_obj_destroy(&zoneconf);
        }
 
        dns_zone_detach(&zone);
@@ -7615,7 +7615,7 @@ data_to_cfg(dns_view_t *view, MDB_val *key, MDB_val *data, isc_buffer_t **text,
 
 cleanup:
        if (zoneconf != NULL) {
-               cfg_obj_destroy(named_g_addparser, &zoneconf);
+               cfg_obj_destroy(&zoneconf);
        }
 
        return result;
@@ -7696,14 +7696,14 @@ for_all_newzone_cfgs(newzone_cfg_cb_t callback, cfg_obj_t *config,
                /*
                 * Destroy the configuration object created in this iteration.
                 */
-               cfg_obj_destroy(named_g_addparser, &zconfigobj);
+               cfg_obj_destroy(&zconfigobj);
        }
 
        if (text != NULL) {
                isc_buffer_free(&text);
        }
        if (zconfigobj != NULL) {
-               cfg_obj_destroy(named_g_addparser, &zconfigobj);
+               cfg_obj_destroy(&zconfigobj);
        }
        mdb_cursor_close(cursor);
 
@@ -7841,7 +7841,7 @@ cleanup:
        UNLOCK(&view->new_zone_lock);
 
        if (zoneconf != NULL) {
-               cfg_obj_destroy(named_g_addparser, &zoneconf);
+               cfg_obj_destroy(&zoneconf);
        }
        if (text != NULL) {
                isc_buffer_free(&text);
@@ -9348,10 +9348,10 @@ load_configuration(named_server_t *server, bool first_time) {
 
 cleanup:
        if (bindkeys != NULL) {
-               cfg_obj_destroy(parser, &bindkeys);
+               cfg_obj_destroy(&bindkeys);
        }
        if (config != NULL) {
-               cfg_obj_destroy(parser, &config);
+               cfg_obj_destroy(&config);
        }
        cfg_parser_destroy(&parser);
 
@@ -9594,7 +9594,7 @@ shutdown_server(void *arg) {
                cfg_aclconfctx_detach(&server->aclctx);
        }
 
-       cfg_obj_destroy(named_g_parser, &named_g_defaultconfig);
+       cfg_obj_destroy(&named_g_defaultconfig);
        cfg_parser_destroy(&named_g_parser);
        cfg_parser_destroy(&named_g_addparser);
 
@@ -13029,7 +13029,7 @@ cleanup:
        }
 
        if (nzf_config != NULL) {
-               cfg_obj_destroy(named_g_addparser, &nzf_config);
+               cfg_obj_destroy(&nzf_config);
        }
 
        return result;
@@ -13153,7 +13153,7 @@ newzone_parse(named_server_t *server, char *command, dns_view_t **viewp,
 
 cleanup:
        if (zoneconf != NULL) {
-               cfg_obj_destroy(named_g_addparser, &zoneconf);
+               cfg_obj_destroy(&zoneconf);
        }
        if (view != NULL) {
                dns_view_detach(&view);
@@ -13197,7 +13197,7 @@ delete_zoneconf(dns_view_t *view, cfg_parser_t *pctx, const cfg_obj_t *config,
 
                e = UNCONST(elt);
                ISC_LIST_UNLINK(*list, e, link);
-               cfg_obj_destroy(pctx, &e->obj);
+               cfg_obj_destroy(&e->obj);
                isc_mem_put(pctx->mctx, e, sizeof(*e));
                result = ISC_R_SUCCESS;
                break;
@@ -13721,7 +13721,7 @@ cleanup:
                (void)putnull(text);
        }
        if (zoneconf != NULL) {
-               cfg_obj_destroy(named_g_addparser, &zoneconf);
+               cfg_obj_destroy(&zoneconf);
        }
        if (view != NULL) {
                dns_view_detach(&view);
@@ -14212,7 +14212,7 @@ named_server_showzone(named_server_t *server, isc_lex_t *lex,
 cleanup:
 #ifdef HAVE_LMDB
        if (nzconfig != NULL) {
-               cfg_obj_destroy(named_g_addparser, &nzconfig);
+               cfg_obj_destroy(&nzconfig);
        }
 #endif /* HAVE_LMDB */
        if (isc_buffer_usedlength(*text) > 0) {
@@ -14232,16 +14232,16 @@ newzone_cfgctx_destroy(void **cfgp) {
 
        if (cfg->conf_parser != NULL) {
                if (cfg->config != NULL) {
-                       cfg_obj_destroy(cfg->conf_parser, &cfg->config);
+                       cfg_obj_destroy(&cfg->config);
                }
                if (cfg->vconfig != NULL) {
-                       cfg_obj_destroy(cfg->conf_parser, &cfg->vconfig);
+                       cfg_obj_destroy(&cfg->vconfig);
                }
                cfg_parser_destroy(&cfg->conf_parser);
        }
        if (cfg->add_parser != NULL) {
                if (cfg->nzf_config != NULL) {
-                       cfg_obj_destroy(cfg->add_parser, &cfg->nzf_config);
+                       cfg_obj_destroy(&cfg->nzf_config);
                }
                cfg_parser_destroy(&cfg->add_parser);
        }
index 8f8b47af15f464a0a927bb7bd6ad42f0adf5ec94..c9d9ce0adff00c49b4a2b064ce336d73e78c6c75 100644 (file)
@@ -604,7 +604,7 @@ read_sessionkey(isc_mem_t *mctx) {
 cleanup:
        if (pctx != NULL) {
                if (sessionkey != NULL) {
-                       cfg_obj_destroy(pctx, &sessionkey);
+                       cfg_obj_destroy(&sessionkey);
                }
                cfg_parser_destroy(&pctx);
        }
index e9c314b0088d8413b3abf133b66841358e6343fa..8b99c6c4bc56503ad83a6a5a216a44e7d390b97b 100644 (file)
@@ -300,7 +300,7 @@ parse_parameters(filter_instance_t *inst, const char *parameters,
 
 cleanup:
        if (param_obj != NULL) {
-               cfg_obj_destroy(parser, &param_obj);
+               cfg_obj_destroy(&param_obj);
        }
        if (parser != NULL) {
                cfg_parser_destroy(&parser);
@@ -383,7 +383,7 @@ plugin_check(const char *parameters, const void *cfg, const char *cfg_file,
 
 cleanup:
        if (param_obj != NULL) {
-               cfg_obj_destroy(parser, &param_obj);
+               cfg_obj_destroy(&param_obj);
        }
        if (parser != NULL) {
                cfg_parser_destroy(&parser);
index 84dd0acd627a79e2a94ba938a04cd78511bfe08b..b9a2b52d5497534704259058d800651b8534def7 100644 (file)
@@ -303,7 +303,7 @@ parse_parameters(filter_instance_t *inst, const char *parameters,
 
 cleanup:
        if (param_obj != NULL) {
-               cfg_obj_destroy(parser, &param_obj);
+               cfg_obj_destroy(&param_obj);
        }
        if (parser != NULL) {
                cfg_parser_destroy(&parser);
@@ -387,7 +387,7 @@ plugin_check(const char *parameters, const void *cfg, const char *cfg_file,
 
 cleanup:
        if (param_obj != NULL) {
-               cfg_obj_destroy(parser, &param_obj);
+               cfg_obj_destroy(&param_obj);
        }
        if (parser != NULL) {
                cfg_parser_destroy(&parser);
index b5582bad49835bdc99d973d7e9c80839c6d30c0c..e963cdad5b018a01d79420861f6286de45903361 100644 (file)
@@ -593,7 +593,7 @@ synthrecord_parseconfig(synthrecord_t *inst, const char *parameters,
 
 cleanup:
        if (synthrecordcfg != NULL) {
-               cfg_obj_destroy(parser, &synthrecordcfg);
+               cfg_obj_destroy(&synthrecordcfg);
        }
 
        if (parser != NULL) {
index d191fd97d1b4edfec72dee910b4228718d9fad19..6aa1e4e67b2641d8aa06761382ba0ca0c561cb3d 100644 (file)
@@ -999,7 +999,7 @@ main(int argc, char **argv) {
 
        isccc_ccmsg_invalidate(&rndc_ccmsg);
 
-       cfg_obj_destroy(pctx, &config);
+       cfg_obj_destroy(&config);
        cfg_parser_destroy(&pctx);
 
        isc_mem_put(isc_g_mctx, args, argslen);
index 556a91d0655da6596e10d07ad912a4f71890885d..86bea21cf409f28cb9c3542440b888b342578d56 100644 (file)
@@ -217,7 +217,7 @@ cleanup:
        }
 
        if (syncplugincfg != NULL) {
-               cfg_obj_destroy(parser, &syncplugincfg);
+               cfg_obj_destroy(&syncplugincfg);
        }
 
        if (parser != NULL) {
index d039bf07cf631cd1c7f14630a03c62f817ca795f..db596e3df8fcf8f4f64107ea211785111e0ea844 100644 (file)
@@ -146,7 +146,7 @@ main(int argc, char **argv) {
 
                cfg_print(cfg, output, NULL);
 
-               cfg_obj_destroy(pctx, &cfg);
+               cfg_obj_destroy(&cfg);
 
                cfg_parser_destroy(&pctx);
        }
index 34ca10b7931f1123e95b5b0a622f917093f0399d..77576fcf2b48ca115e3d33b2aa6121795551432b 100644 (file)
@@ -191,6 +191,13 @@ cfg_parser_destroy(cfg_parser_t **pctxp);
  * more references.
  */
 
+cfg_obj_t *
+cfg_parser_currentfile(cfg_parser_t *pctx);
+/*%<
+ * Returns the current file of a parser (as an cfg_obj_t qstring). NULL is non
+ * existent.
+ */
+
 bool
 cfg_obj_isvoid(const cfg_obj_t *obj);
 /*%<
@@ -550,14 +557,14 @@ cfg_obj_attach(cfg_obj_t *src, cfg_obj_t **dest);
  */
 
 void
-cfg_obj_destroy(cfg_parser_t *pctx, cfg_obj_t **obj);
+cfg_obj_destroy(cfg_obj_t **obj);
 /*%<
  * Delete a reference to a configuration object; destroy the object if
  * there are no more references.
  *
  * Require:
  * \li     '*obj' is a valid cfg_obj_t.
- * \li     'pctx' is a valid cfg_parser_t.
+ * \li     'mctx' is a valid isc_mem_t.
  */
 
 void
index ae15fd9ab2a4b41b7d488a0b96297b39e02a3664..c06fedefb296884e3a070fabec42c8798f94f50d 100644 (file)
@@ -102,7 +102,7 @@ typedef isc_result_t (*cfg_parsefunc_t)(cfg_parser_t *, const cfg_type_t *type,
                                        cfg_obj_t **);
 typedef void (*cfg_printfunc_t)(cfg_printer_t *, const cfg_obj_t *);
 typedef void (*cfg_docfunc_t)(cfg_printer_t *, const cfg_type_t *);
-typedef void (*cfg_freefunc_t)(cfg_parser_t *, cfg_obj_t *);
+typedef void (*cfg_freefunc_t)(cfg_obj_t *);
 
 /*
  * Structure definitions
@@ -180,6 +180,10 @@ struct cfg_rep {
  */
 
 struct cfg_obj {
+       unsigned int   magic;
+       isc_mem_t     *mctx;
+       isc_refcount_t references;
+
        const cfg_type_t *type;
        union {
                uint32_t         uint32;
@@ -197,10 +201,8 @@ struct cfg_obj {
                cfg_netprefix_t   netprefix;
                isccfg_duration_t duration;
        } value;
-       isc_refcount_t references; /*%< reference counter */
-       cfg_obj_t     *file;       /*%< refcounted string */
-       unsigned int   line;
-       cfg_parser_t  *pctx;
+       cfg_obj_t   *file; /*%< separate string with its own refcount */
+       unsigned int line;
 };
 
 /*% A list element. */
@@ -354,7 +356,8 @@ cfg_ungettoken(cfg_parser_t *pctx);
 #define CFG_LEXOPT_QSTRING (ISC_LEXOPT_QSTRING | ISC_LEXOPT_QSTRINGMULTILINE)
 
 isc_result_t
-cfg_create_obj(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **objp);
+cfg_create_obj(isc_mem_t *mctx, cfg_obj_t *file, size_t line,
+              const cfg_type_t *type, cfg_obj_t **ret);
 
 void
 cfg_print_rawuint(cfg_printer_t *pctx, unsigned int u);
index 4b022cefe28ec258d04f052d70489d2adf99d030..46e0c54d6df625b27edef85b1ebd87a29500b655 100644 (file)
        } while (0)
 
 /*% Clean up a configuration object if non-NULL. */
-#define CLEANUP_OBJ(obj)                               \
-       do {                                           \
-               if ((obj) != NULL)                     \
-                       cfg_obj_destroy(pctx, &(obj)); \
+#define CLEANUP_OBJ(obj)                         \
+       do {                                     \
+               if ((obj) != NULL)               \
+                       cfg_obj_destroy(&(obj)); \
        } while (0)
 
 /*%
@@ -414,7 +414,8 @@ parse_updatepolicy(cfg_parser_t *pctx, const cfg_type_t *type,
            strcasecmp(TOKEN_STRING(pctx), "local") == 0)
        {
                cfg_obj_t *obj = NULL;
-               CHECK(cfg_create_obj(pctx, &cfg_type_ustring, &obj));
+               CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                                    pctx->line, &cfg_type_ustring, &obj));
                obj->value.string.length = strlen("local");
                obj->value.string.base =
                        isc_mem_get(pctx->mctx, obj->value.string.length + 1);
@@ -972,7 +973,8 @@ parse_qstringornone(cfg_parser_t *pctx, const cfg_type_t *type,
        if (pctx->token.type == isc_tokentype_string &&
            strcasecmp(TOKEN_STRING(pctx), "none") == 0)
        {
-               return cfg_create_obj(pctx, &cfg_type_none, ret);
+               return cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                                     pctx->line, &cfg_type_none, ret);
        }
        cfg_ungettoken(pctx);
        return cfg_parse_qstring(pctx, type, ret);
@@ -1014,7 +1016,8 @@ parse_boolorauto(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
        if (pctx->token.type == isc_tokentype_string &&
            strcasecmp(TOKEN_STRING(pctx), "auto") == 0)
        {
-               return cfg_create_obj(pctx, &cfg_type_auto, ret);
+               return cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                                     pctx->line, &cfg_type_auto, ret);
        }
        cfg_ungettoken(pctx);
        return cfg_parse_boolean(pctx, type, ret);
@@ -1068,12 +1071,15 @@ parse_serverid(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
        if (pctx->token.type == isc_tokentype_string &&
            strcasecmp(TOKEN_STRING(pctx), "none") == 0)
        {
-               return cfg_create_obj(pctx, &cfg_type_none, ret);
+               return cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                                     pctx->line, &cfg_type_none, ret);
        }
        if (pctx->token.type == isc_tokentype_string &&
            strcasecmp(TOKEN_STRING(pctx), "hostname") == 0)
        {
-               result = cfg_create_obj(pctx, &cfg_type_hostname, ret);
+               result = cfg_create_obj(pctx->mctx,
+                                       cfg_parser_currentfile(pctx),
+                                       pctx->line, &cfg_type_hostname, ret);
                if (result == ISC_R_SUCCESS) {
                        (*ret)->value.boolean = true;
                }
@@ -2814,7 +2820,8 @@ parse_sizeval(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
        }
        CHECK(parse_unitstring(TOKEN_STRING(pctx), &val));
 
-       CHECK(cfg_create_obj(pctx, &cfg_type_uint64, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, &cfg_type_uint64, &obj));
        obj->value.uint64 = val;
        *ret = obj;
        return ISC_R_SUCCESS;
@@ -2845,13 +2852,15 @@ parse_sizeval_percent(cfg_parser_t *pctx, const cfg_type_t *type,
        percent = strtoull(TOKEN_STRING(pctx), &endp, 10);
 
        if (*endp == '%' && *(endp + 1) == 0) {
-               CHECK(cfg_create_obj(pctx, &cfg_type_percentage, &obj));
+               CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                                    pctx->line, &cfg_type_percentage, &obj));
                obj->value.uint32 = (uint32_t)percent;
                *ret = obj;
                return ISC_R_SUCCESS;
        } else {
                CHECK(parse_unitstring(TOKEN_STRING(pctx), &val));
-               CHECK(cfg_create_obj(pctx, &cfg_type_uint64, &obj));
+               CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                                    pctx->line, &cfg_type_uint64, &obj));
                obj->value.uint64 = val;
                *ret = obj;
                return ISC_R_SUCCESS;
@@ -3278,7 +3287,8 @@ parse_querysource(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
            strcasecmp(TOKEN_STRING(pctx), "none") == 0)
        {
                CHECK(cfg_gettoken(pctx, 0));
-               CHECK(cfg_create_obj(pctx, &cfg_type_none, ret));
+               CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                                    pctx->line, &cfg_type_none, ret));
        } else {
                CHECK(cfg_parse_sockaddr_generic(pctx, &cfg_type_querysource,
                                                 type, ret));
@@ -3479,7 +3489,9 @@ parse_logseverity(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
                         * This makes little sense, but we support it for
                         * compatibility with BIND 8.
                         */
-                       CHECK(cfg_create_obj(pctx, &cfg_type_uint32, ret));
+                       CHECK(cfg_create_obj(
+                               pctx->mctx, cfg_parser_currentfile(pctx),
+                               pctx->line, &cfg_type_uint32, ret));
                        (*ret)->value.uint32 = 1;
                }
                (*ret)->type = &cfg_type_debuglevel; /* XXX kludge */
index 8a036f558bde8929988e9b1d644332128dc0729d..9042493bb098d1b058a28ff61797a642f4436a78 100644 (file)
        } while (0)
 
 /* Clean up a configuration object if non-NULL. */
-#define CLEANUP_OBJ(obj)                               \
-       do {                                           \
-               if ((obj) != NULL)                     \
-                       cfg_obj_destroy(pctx, &(obj)); \
+#define CLEANUP_OBJ(obj)                         \
+       do {                                     \
+               if ((obj) != NULL)               \
+                       cfg_obj_destroy(&(obj)); \
        } while (0)
 
+/* cfg_obj_t magic number */
+#define CFGOBJ_MAGIC ISC_MAGIC('c', 'f', 'g', 'o')
+
 /*
  * Forward declarations of static functions.
  */
 
 static void
-free_tuple(cfg_parser_t *pctx, cfg_obj_t *obj);
+free_tuple(cfg_obj_t *obj);
 
 static isc_result_t
 parse_list(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
@@ -108,7 +111,7 @@ static void
 print_list(cfg_printer_t *pctx, const cfg_obj_t *obj);
 
 static void
-free_list(cfg_parser_t *pctx, cfg_obj_t *obj);
+free_list(cfg_obj_t *obj);
 
 static isc_result_t
 create_listelt(cfg_parser_t *pctx, cfg_listelt_t **eltp);
@@ -118,23 +121,23 @@ create_string(cfg_parser_t *pctx, const char *contents, const cfg_type_t *type,
              cfg_obj_t **ret);
 
 static void
-free_string(cfg_parser_t *pctx, cfg_obj_t *obj);
+free_string(cfg_obj_t *obj);
 
 static void
-free_sockaddrtls(cfg_parser_t *pctx, cfg_obj_t *obj);
+free_sockaddrtls(cfg_obj_t *obj);
 
 static isc_result_t
 create_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **objp);
 
 static void
-free_map(cfg_parser_t *pctx, cfg_obj_t *obj);
+free_map(cfg_obj_t *obj);
 
 static isc_result_t
 parse_symtab_elt(cfg_parser_t *pctx, const char *name, cfg_type_t *elttype,
                 isc_symtab_t *symtab, bool callback);
 
 static void
-free_noop(cfg_parser_t *pctx, cfg_obj_t *obj);
+free_noop(cfg_obj_t *obj);
 
 static isc_result_t
 cfg_getstringtoken(cfg_parser_t *pctx);
@@ -236,6 +239,25 @@ print_close(cfg_printer_t *pctx) {
        cfg_print_cstr(pctx, "}");
 }
 
+cfg_obj_t *
+cfg_parser_currentfile(cfg_parser_t *pctx) {
+       cfg_listelt_t *elt = NULL;
+       cfg_obj_t *fileobj = NULL;
+
+       if (pctx->open_files == NULL) {
+               return NULL;
+       }
+
+       elt = ISC_LIST_TAIL(pctx->open_files->value.list);
+       if (elt == NULL) {
+               return NULL;
+       }
+
+       fileobj = elt->obj;
+       INSIST(fileobj->type == &cfg_type_qstring);
+       return fileobj;
+}
+
 isc_result_t
 cfg_parse_obj(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
        isc_result_t result;
@@ -299,7 +321,8 @@ cfg_create_tuple(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
                nfields++;
        }
 
-       CHECK(cfg_create_obj(pctx, type, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, type, &obj));
        obj->value.tuple = isc_mem_cget(pctx->mctx, nfields,
                                        sizeof(cfg_obj_t *));
        for (f = fields, i = 0; f->name != NULL; f++, i++) {
@@ -386,7 +409,7 @@ cfg_doc_tuple(cfg_printer_t *pctx, const cfg_type_t *type) {
 }
 
 static void
-free_tuple(cfg_parser_t *pctx, cfg_obj_t *obj) {
+free_tuple(cfg_obj_t *obj) {
        unsigned int i;
        const cfg_tuplefielddef_t *fields = obj->type->of;
        const cfg_tuplefielddef_t *f;
@@ -400,8 +423,7 @@ free_tuple(cfg_parser_t *pctx, cfg_obj_t *obj) {
                CLEANUP_OBJ(obj->value.tuple[i]);
                nfields++;
        }
-       isc_mem_cput(pctx->mctx, obj->value.tuple, nfields,
-                    sizeof(cfg_obj_t *));
+       isc_mem_cput(obj->mctx, obj->value.tuple, nfields, sizeof(cfg_obj_t *));
 }
 
 bool
@@ -746,7 +768,8 @@ cfg_parse_void(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
 
        UNUSED(type);
 
-       return cfg_create_obj(pctx, &cfg_type_void, ret);
+       return cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                             pctx->line, &cfg_type_void, ret);
 }
 
 void
@@ -804,7 +827,8 @@ cfg_parse_percentage(cfg_parser_t *pctx, const cfg_type_t *type,
                return ISC_R_UNEXPECTEDTOKEN;
        }
 
-       CHECK(cfg_create_obj(pctx, &cfg_type_percentage, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, &cfg_type_percentage, &obj));
        obj->value.uint32 = (uint32_t)percent;
        *ret = obj;
 
@@ -877,7 +901,8 @@ cfg_parse_fixedpoint(cfg_parser_t *pctx, const cfg_type_t *type,
                return ISC_R_UNEXPECTEDTOKEN;
        }
 
-       CHECK(cfg_create_obj(pctx, &cfg_type_fixedpoint, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, &cfg_type_fixedpoint, &obj));
 
        obj->value.uint32 = strtoul(p, NULL, 10) * 100;
        switch (n3) {
@@ -943,7 +968,8 @@ cfg_parse_uint32(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
                return ISC_R_UNEXPECTEDTOKEN;
        }
 
-       CHECK(cfg_create_obj(pctx, &cfg_type_uint32, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, &cfg_type_uint32, &obj));
 
        obj->value.uint32 = pctx->token.value.as_ulong;
        *ret = obj;
@@ -1164,7 +1190,8 @@ parse_duration(cfg_parser_t *pctx, cfg_obj_t **ret) {
                goto cleanup;
        }
 
-       CHECK(cfg_create_obj(pctx, &cfg_type_duration, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, &cfg_type_duration, &obj));
        obj->value.duration = duration;
        *ret = obj;
 
@@ -1219,7 +1246,8 @@ cfg_parse_duration_or_unlimited(cfg_parser_t *pctx, const cfg_type_t *type,
                duration.iso8601 = false;
                duration.unlimited = true;
 
-               CHECK(cfg_create_obj(pctx, &cfg_type_duration, &obj));
+               CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                                    pctx->line, &cfg_type_duration, &obj));
                obj->value.duration = duration;
                *ret = obj;
                return ISC_R_SUCCESS;
@@ -1271,7 +1299,8 @@ create_string(cfg_parser_t *pctx, const char *contents, const cfg_type_t *type,
        cfg_obj_t *obj = NULL;
        int len;
 
-       CHECK(cfg_create_obj(pctx, type, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, type, &obj));
        len = strlen(contents);
        obj->value.string.length = len;
        obj->value.string.base = isc_mem_get(pctx->mctx, len + 1);
@@ -1543,16 +1572,16 @@ print_sstring(cfg_printer_t *pctx, const cfg_obj_t *obj) {
 }
 
 static void
-free_string(cfg_parser_t *pctx, cfg_obj_t *obj) {
-       isc_mem_put(pctx->mctx, obj->value.string.base,
+free_string(cfg_obj_t *obj) {
+       isc_mem_put(obj->mctx, obj->value.string.base,
                    obj->value.string.length + 1);
 }
 
 static void
-free_sockaddrtls(cfg_parser_t *pctx, cfg_obj_t *obj) {
+free_sockaddrtls(cfg_obj_t *obj) {
        if (obj->value.sockaddrtls.tls.base != NULL) {
                INSIST(obj->value.sockaddrtls.tls.length != 0);
-               isc_mem_put(pctx->mctx, obj->value.sockaddrtls.tls.base,
+               isc_mem_put(obj->mctx, obj->value.sockaddrtls.tls.base,
                            obj->value.sockaddrtls.tls.length + 1);
        }
 }
@@ -1881,7 +1910,8 @@ cfg_parse_boolean(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
                goto bad_boolean;
        }
 
-       CHECK(cfg_create_obj(pctx, &cfg_type_boolean, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, &cfg_type_boolean, &obj));
        obj->value.boolean = value;
        *ret = obj;
        return result;
@@ -1922,7 +1952,8 @@ cfg_create_list(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **obj) {
        REQUIRE(type != NULL);
        REQUIRE(obj != NULL && *obj == NULL);
 
-       CHECK(cfg_create_obj(pctx, type, obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, type, obj));
        ISC_LIST_INIT((*obj)->value.list);
 cleanup:
        return result;
@@ -1940,17 +1971,17 @@ create_listelt(cfg_parser_t *pctx, cfg_listelt_t **eltp) {
 }
 
 static void
-free_listelt(cfg_parser_t *pctx, cfg_listelt_t *elt) {
+free_listelt(isc_mem_t *mctx, cfg_listelt_t *elt) {
        if (elt->obj != NULL) {
-               cfg_obj_destroy(pctx, &elt->obj);
+               cfg_obj_destroy(&elt->obj);
        }
-       isc_mem_put(pctx->mctx, elt, sizeof(*elt));
+       isc_mem_put(mctx, elt, sizeof(*elt));
 }
 
 static void
-free_list(cfg_parser_t *pctx, cfg_obj_t *obj) {
+free_list(cfg_obj_t *obj) {
        ISC_LIST_FOREACH(obj->value.list, elt, link) {
-               free_listelt(pctx, elt);
+               free_listelt(obj->mctx, elt);
        }
 }
 
@@ -2012,7 +2043,7 @@ parse_list(cfg_parser_t *pctx, const cfg_type_t *listtype, cfg_obj_t **ret) {
 
 cleanup:
        if (elt != NULL) {
-               free_listelt(pctx, elt);
+               free_listelt(pctx->mctx, elt);
        }
        CLEANUP_OBJ(listobj);
        return result;
@@ -2274,7 +2305,7 @@ cfg_parse_mapbody(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
                                CHECK(parser_openfile(pctx, g.gl_pathv[i]));
                        }
 
-                       cfg_obj_destroy(pctx, &includename);
+                       cfg_obj_destroy(&includename);
                        globfree(&g);
 
                        goto redo;
@@ -2302,7 +2333,7 @@ cfg_parse_mapbody(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
                         */
                        CHECK(cfg_parse_obj(pctx, &cfg_type_unsupported,
                                            &eltobj));
-                       cfg_obj_destroy(pctx, &eltobj);
+                       cfg_obj_destroy(&eltobj);
                        CHECK(parse_semicolon(pctx));
                        continue;
                }
@@ -2786,7 +2817,8 @@ parse_token(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
 
        UNUSED(type);
 
-       CHECK(cfg_create_obj(pctx, &cfg_type_token, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, &cfg_type_token, &obj));
        CHECK(cfg_gettoken(pctx, CFG_LEXOPT_QSTRING));
        if (pctx->token.type == isc_tokentype_eof) {
                cfg_ungettoken(pctx);
@@ -3056,7 +3088,8 @@ parse_netaddr(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
        isc_netaddr_t netaddr;
        unsigned int flags = *(const unsigned int *)type->of;
 
-       CHECK(cfg_create_obj(pctx, type, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, type, &obj));
        CHECK(cfg_parse_rawaddr(pctx, flags, &netaddr));
        isc_sockaddr_fromnetaddr(&obj->value.sockaddr, &netaddr, 0);
        *ret = obj;
@@ -3185,7 +3218,8 @@ cfg_parse_netprefix(cfg_parser_t *pctx, const cfg_type_t *type,
                }
                prefixlen = addrlen;
        }
-       CHECK(cfg_create_obj(pctx, &cfg_type_netprefix, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, &cfg_type_netprefix, &obj));
        obj->value.netprefix.address = netaddr;
        obj->value.netprefix.prefixlen = prefixlen;
        *ret = obj;
@@ -3313,7 +3347,8 @@ parse_sockaddrsub(cfg_parser_t *pctx, const cfg_type_t *type, int flags,
                goto cleanup;
        }
 
-       CHECK(cfg_create_obj(pctx, type, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, type, &obj));
        if (have_tls == 1) {
                obj->value.sockaddrtls.tls = tls;
        }
@@ -3595,40 +3630,6 @@ cfg_parser_warning(cfg_parser_t *pctx, unsigned int flags, const char *fmt,
 
 #define MAX_LOG_TOKEN 30 /* How much of a token to quote in log messages. */
 
-static bool
-have_current_file(cfg_parser_t *pctx) {
-       cfg_listelt_t *elt;
-       if (pctx->open_files == NULL) {
-               return false;
-       }
-
-       elt = ISC_LIST_TAIL(pctx->open_files->value.list);
-       if (elt == NULL) {
-               return false;
-       }
-
-       return true;
-}
-
-static cfg_obj_t *
-current_file(cfg_parser_t *pctx) {
-       cfg_listelt_t *elt;
-       cfg_obj_t *fileobj;
-
-       if (!have_current_file(pctx)) {
-               return NULL;
-       }
-
-       elt = ISC_LIST_TAIL(pctx->open_files->value.list);
-       if (elt == NULL) { /* shouldn't be possible, but... */
-               return NULL;
-       }
-
-       fileobj = elt->obj;
-       INSIST(fileobj->type == &cfg_type_qstring);
-       return fileobj;
-}
-
 static void
 parser_complain(cfg_parser_t *pctx, bool is_warning, unsigned int flags,
                const char *format, va_list args) {
@@ -3637,6 +3638,7 @@ parser_complain(cfg_parser_t *pctx, bool is_warning, unsigned int flags,
        static char message[2048];
        int level = ISC_LOG_ERROR;
        const char *prep = "";
+       const cfg_obj_t *file = NULL;
        size_t len;
 
        if (is_warning) {
@@ -3644,10 +3646,10 @@ parser_complain(cfg_parser_t *pctx, bool is_warning, unsigned int flags,
        }
 
        where[0] = '\0';
-       if (have_current_file(pctx)) {
+       file = cfg_parser_currentfile(pctx);
+       if (file != NULL) {
                snprintf(where, sizeof(where),
-                        "%s:%u: ", cfg_obj_asstring(current_file(pctx)),
-                        pctx->line);
+                        "%s:%u: ", cfg_obj_asstring(file), pctx->line);
        } else if (pctx->buf_name != NULL) {
                snprintf(where, sizeof(where), "%s: ", pctx->buf_name);
        }
@@ -3736,19 +3738,19 @@ cfg_obj_line(const cfg_obj_t *obj) {
 }
 
 isc_result_t
-cfg_create_obj(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
+cfg_create_obj(isc_mem_t *mctx, cfg_obj_t *file, size_t line,
+              const cfg_type_t *type, cfg_obj_t **ret) {
        cfg_obj_t *obj;
-       cfg_obj_t *file = NULL;
 
-       REQUIRE(pctx != NULL);
+       REQUIRE(mctx != NULL);
        REQUIRE(type != NULL);
        REQUIRE(ret != NULL && *ret == NULL);
 
-       obj = isc_mem_get(pctx->mctx, sizeof(cfg_obj_t));
-       *obj = (cfg_obj_t){ .type = type, .line = pctx->line, .pctx = pctx };
-       isc_refcount_init(&obj->references, 1);
+       obj = isc_mem_get(mctx, sizeof(cfg_obj_t));
+       *obj = (cfg_obj_t){ .magic = CFGOBJ_MAGIC, .type = type, .line = line };
 
-       file = current_file(pctx);
+       isc_refcount_init(&obj->references, 1);
+       isc_mem_attach(mctx, &obj->mctx);
        if (file != NULL) {
                cfg_obj_attach(file, &obj->file);
        }
@@ -3762,12 +3764,12 @@ static void
 map_symtabitem_destroy(char *key, unsigned int type, isc_symvalue_t symval,
                       void *userarg) {
        cfg_obj_t *obj = symval.as_pointer;
-       cfg_parser_t *pctx = (cfg_parser_t *)userarg;
 
        UNUSED(key);
        UNUSED(type);
+       UNUSED(userarg);
 
-       cfg_obj_destroy(pctx, &obj);
+       cfg_obj_destroy(&obj);
 }
 
 static isc_result_t
@@ -3776,7 +3778,8 @@ create_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
        isc_symtab_t *symtab = NULL;
        cfg_obj_t *obj = NULL;
 
-       CHECK(cfg_create_obj(pctx, type, &obj));
+       CHECK(cfg_create_obj(pctx->mctx, cfg_parser_currentfile(pctx),
+                            pctx->line, type, &obj));
        isc_symtab_create(pctx->mctx, map_symtabitem_destroy, pctx, false,
                          &symtab);
        obj->value.map.symtab = symtab;
@@ -3793,7 +3796,7 @@ cleanup:
 }
 
 static void
-free_map(cfg_parser_t *pctx, cfg_obj_t *obj) {
+free_map(cfg_obj_t *obj) {
        CLEANUP_OBJ(obj->value.map.id);
        isc_symtab_destroy(&obj->value.map.symtab);
 }
@@ -3810,21 +3813,24 @@ cfg_obj_istype(const cfg_obj_t *obj, const cfg_type_t *type) {
  * Destroy 'obj', a configuration object created in 'pctx'.
  */
 void
-cfg_obj_destroy(cfg_parser_t *pctx, cfg_obj_t **objp) {
+cfg_obj_destroy(cfg_obj_t **objp) {
        REQUIRE(objp != NULL && *objp != NULL);
-       REQUIRE(pctx != NULL);
+       REQUIRE((*objp)->magic == CFGOBJ_MAGIC);
 
        cfg_obj_t *obj = *objp;
        *objp = NULL;
 
        if (isc_refcount_decrement(&obj->references) == 1) {
+               obj->magic = 0;
+
                if (obj->file != NULL) {
-                       cfg_obj_destroy(obj->file->pctx, &obj->file);
+                       cfg_obj_destroy(&obj->file);
                }
 
-               obj->type->rep->free(pctx, obj);
+               obj->type->rep->free(obj);
+
                isc_refcount_destroy(&obj->references);
-               isc_mem_put(pctx->mctx, obj, sizeof(cfg_obj_t));
+               isc_mem_putanddetach(&obj->mctx, obj, sizeof(cfg_obj_t));
        }
 }
 
@@ -3838,8 +3844,7 @@ cfg_obj_attach(cfg_obj_t *src, cfg_obj_t **dest) {
 }
 
 static void
-free_noop(cfg_parser_t *pctx, cfg_obj_t *obj) {
-       UNUSED(pctx);
+free_noop(cfg_obj_t *obj) {
        UNUSED(obj);
 }
 
@@ -3943,7 +3948,7 @@ breakout:
 
 cleanup:
        if (elt != NULL) {
-               free_listelt(pctx, elt);
+               free_listelt(pctx->mctx, elt);
        }
        CLEANUP_OBJ(destobj);
 
index 8d39ef5939c236cf0f2cbba17824a7430625af86..a9524a0c4ae3c89b17e2af4ac06beb0baba41e14 100644 (file)
@@ -192,7 +192,7 @@ ISC_RUN_TEST_IMPL(duration) {
                        assert_int_equal(cmp, 0);
                }
 
-               cfg_obj_destroy(p1, &c1);
+               cfg_obj_destroy(&c1);
                cfg_parser_destroy(&p1);
        }
 }
index da95ddded08ffcbfdad7880d13a6999da13812b6..7efc320dae43bb40647a5f855058340c7e8e56a7 100644 (file)
@@ -102,7 +102,7 @@ ISC_RUN_TEST_IMPL(addzoneconf) {
                strlcat(buf, ";", sizeof(buf));
                assert_string_equal(tests[i], buf);
 
-               cfg_obj_destroy(p, &conf);
+               cfg_obj_destroy(&conf);
                cfg_parser_reset(p);
        }
 
@@ -141,8 +141,8 @@ ISC_RUN_TEST_IMPL(parse_buffer) {
        assert_int_equal(result, ISC_R_SUCCESS);
        assert_int_equal(p2->line, 104);
 
-       cfg_obj_destroy(p1, &c1);
-       cfg_obj_destroy(p2, &c2);
+       cfg_obj_destroy(&c1);
+       cfg_obj_destroy(&c2);
 
        cfg_parser_destroy(&p1);
        cfg_parser_destroy(&p2);