From: Colin Vidal Date: Mon, 3 Nov 2025 16:24:22 +0000 (+0100) Subject: parser: add cfg_string_create() API X-Git-Tag: v9.21.17~63^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=870b7329f838cd09a58d5434ae4dc655353c41ff;p=thirdparty%2Fbind9.git parser: add cfg_string_create() API 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()`. --- diff --git a/lib/isccfg/include/isccfg/grammar.h b/lib/isccfg/include/isccfg/grammar.h index 460afb1087d..bee2a224694 100644 --- a/lib/isccfg/include/isccfg/grammar.h +++ b/lib/isccfg/include/isccfg/grammar.h @@ -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); diff --git a/lib/isccfg/namedconf.c b/lib/isccfg/namedconf.c index 69cff0317fd..455ef114aed 100644 --- a/lib/isccfg/namedconf.c +++ b/lib/isccfg/namedconf.c @@ -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; } diff --git a/lib/isccfg/parser.c b/lib/isccfg/parser.c index ac9a4a24af5..35721eb2f1f 100644 --- a/lib/isccfg/parser.c +++ b/lib/isccfg/parser.c @@ -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: