]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
parser: add cfg_string_create() API
authorColin Vidal <colin@isc.org>
Mon, 3 Nov 2025 16:24:22 +0000 (17:24 +0100)
committerColin Vidal <colin@isc.org>
Thu, 4 Dec 2025 15:09:40 +0000 (16:09 +0100)
The parser has a static function `create_string()` used
internally. But there was duplicate code to create a string node
in `namedconf.c`.  Instead of implementing the same logic twice,
`create_string()` is now publicly exposed as `cfg_string_create()`.

lib/isccfg/include/isccfg/grammar.h
lib/isccfg/namedconf.c
lib/isccfg/parser.c

index 460afb1087da582f424bfee50d847e708840ede3..bee2a224694196706acbf25dcab598884bd54f6d 100644 (file)
@@ -382,6 +382,10 @@ void
 cfg_obj_create(isc_mem_t *mctx, cfg_obj_t *file, size_t line,
               const cfg_type_t *type, cfg_obj_t **ret);
 
+void
+cfg_string_create(cfg_parser_t *pctx, const char *contents,
+                 const cfg_type_t *type, cfg_obj_t **ret);
+
 void
 cfg_print_rawuint(cfg_printer_t *pctx, unsigned int u);
 
index 69cff0317fd11f6ca268452fecb3b32c9562e144..455ef114aedfabc9039cd72253eb2f0a23614a79 100644 (file)
@@ -406,15 +406,7 @@ parse_updatepolicy(cfg_parser_t *pctx, const cfg_type_t *type,
        if (pctx->token.type == isc_tokentype_string &&
            strcasecmp(TOKEN_STRING(pctx), "local") == 0)
        {
-               cfg_obj_t *obj = NULL;
-               cfg_obj_create(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);
-               memmove(obj->value.string.base, "local", 5);
-               obj->value.string.base[5] = '\0';
-               *ret = obj;
+               cfg_string_create(pctx, "local", &cfg_type_ustring, ret);
                return ISC_R_SUCCESS;
        }
 
index ac9a4a24af56235b57eaa5331f47ef852120ab61..35721eb2f1fd41975e5efbb4e96e2256854baffa 100644 (file)
@@ -114,9 +114,6 @@ create_list(isc_mem_t *mctx, cfg_obj_t *file, size_t line,
 static void
 create_listelt(cfg_obj_t *list, cfg_listelt_t **eltp);
 
-static void
-create_string(cfg_parser_t *pctx, const char *contents, const cfg_type_t *type,
-             cfg_obj_t **ret);
 static void
 free_string(cfg_obj_t *obj);
 
@@ -766,8 +763,8 @@ parser_openfile(cfg_parser_t *pctx, const char *filename) {
                goto cleanup;
        }
 
-       create_string(pctx, filename, &cfg_type_qstring, &stringobj);
        create_listelt(pctx->open_files, &elt);
+       cfg_string_create(pctx, filename, &cfg_type_qstring, &stringobj);
        elt->obj = stringobj;
        ISC_LIST_APPEND(pctx->open_files->value.list, elt, link);
 
@@ -1409,9 +1406,9 @@ cfg_type_t cfg_type_duration_or_unlimited = { "duration_or_unlimited",
  */
 
 /* Create a string object from a null-terminated C string. */
-static void
-create_string(cfg_parser_t *pctx, const char *contents, const cfg_type_t *type,
-             cfg_obj_t **ret) {
+void
+cfg_string_create(cfg_parser_t *pctx, const char *contents,
+                 const cfg_type_t *type, cfg_obj_t **ret) {
        cfg_obj_t *obj = NULL;
        int len;
 
@@ -1439,7 +1436,7 @@ cfg_parse_qstring(cfg_parser_t *pctx, const cfg_type_t *type ISC_ATTR_UNUSED,
                cfg_parser_error(pctx, CFG_LOG_NEAR, "expected quoted string");
                return ISC_R_UNEXPECTEDTOKEN;
        }
-       create_string(pctx, TOKEN_STRING(pctx), &cfg_type_qstring, ret);
+       cfg_string_create(pctx, TOKEN_STRING(pctx), &cfg_type_qstring, ret);
        return ISC_R_SUCCESS;
 
 cleanup:
@@ -1457,7 +1454,7 @@ parse_ustring(cfg_parser_t *pctx, const cfg_type_t *type ISC_ATTR_UNUSED,
                                 "expected unquoted string");
                return ISC_R_UNEXPECTEDTOKEN;
        }
-       create_string(pctx, TOKEN_STRING(pctx), &cfg_type_ustring, ret);
+       cfg_string_create(pctx, TOKEN_STRING(pctx), &cfg_type_ustring, ret);
        return ISC_R_SUCCESS;
 
 cleanup:
@@ -1473,7 +1470,7 @@ cfg_parse_astring(cfg_parser_t *pctx, const cfg_type_t *type ISC_ATTR_UNUSED,
        REQUIRE(ret != NULL && *ret == NULL);
 
        CHECK(cfg_getstringtoken(pctx));
-       create_string(pctx, TOKEN_STRING(pctx), &cfg_type_qstring, ret);
+       cfg_string_create(pctx, TOKEN_STRING(pctx), &cfg_type_qstring, ret);
        return ISC_R_SUCCESS;
 
 cleanup:
@@ -1489,7 +1486,7 @@ cfg_parse_sstring(cfg_parser_t *pctx, const cfg_type_t *type ISC_ATTR_UNUSED,
        REQUIRE(ret != NULL && *ret == NULL);
 
        CHECK(cfg_getstringtoken(pctx));
-       create_string(pctx, TOKEN_STRING(pctx), &cfg_type_sstring, ret);
+       cfg_string_create(pctx, TOKEN_STRING(pctx), &cfg_type_sstring, ret);
        return ISC_R_SUCCESS;
 
 cleanup:
@@ -1506,7 +1503,8 @@ parse_btext(cfg_parser_t *pctx, const cfg_type_t *type ISC_ATTR_UNUSED,
                cfg_parser_error(pctx, CFG_LOG_NEAR, "expected bracketed text");
                return ISC_R_UNEXPECTEDTOKEN;
        }
-       create_string(pctx, TOKEN_STRING(pctx), &cfg_type_bracketed_text, ret);
+       cfg_string_create(pctx, TOKEN_STRING(pctx), &cfg_type_bracketed_text,
+                         ret);
        return ISC_R_SUCCESS;
 
 cleanup: