From c2238256e2f13fd3952f31943177b1fb7fba58b8 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Sun, 13 Oct 2019 17:06:05 -0400 Subject: [PATCH] [core] inline array as part of data_array value (instead of value being (array *)) --- src/array.c | 35 +++++++++++++++++------------------ src/array.h | 8 +++++--- src/configfile-glue.c | 4 ++-- src/configparser.y | 7 +++---- src/data_array.c | 9 +++------ src/data_config.c | 3 +-- src/gw_backend.c | 20 ++++++++++---------- src/mod_auth.c | 18 +++++++++--------- src/mod_dirlisting.c | 4 ++-- src/mod_openssl.c | 7 ++++--- src/mod_proxy.c | 8 ++++---- src/mod_redirect.c | 6 +++--- src/mod_rewrite.c | 6 +++--- 13 files changed, 66 insertions(+), 69 deletions(-) diff --git a/src/array.c b/src/array.c index a85db18e..7d226a6c 100644 --- a/src/array.c +++ b/src/array.c @@ -29,33 +29,32 @@ array *array_init(void) { return a; } -array *array_init_array(const array * const src) { - array *a = array_init(); +void array_free_data(array * const a) { + data_unset ** const data = a->data; + const uint32_t sz = a->size; + for (uint32_t i = 0; i < sz; ++i) { + if (data[i]) data[i]->fn->free(data[i]); + } + free(data); +} - if (0 == src->size) return a; +void array_copy_array(array * const dst, const array * const src) { + array_free_data(dst); + if (0 == src->size) return; - a->used = src->used; - a->size = src->size; + dst->used = src->used; + dst->size = src->size; - a->data = calloc(src->size, sizeof(*src->data)); - force_assert(NULL != a->data); + dst->data = calloc(src->size, sizeof(*src->data)); + force_assert(NULL != dst->data); for (uint32_t i = 0; i < src->used; ++i) { - a->data[i] = src->data[i]->fn->copy(src->data[i]); + dst->data[i] = src->data[i]->fn->copy(src->data[i]); } - - return a; } void array_free(array * const a) { if (!a) return; - - data_unset ** const data = a->data; - const uint32_t sz = a->size; - for (uint32_t i = 0; i < sz; ++i) { - if (data[i]) data[i]->fn->free(data[i]); - } - - if (data) free(data); + array_free_data(a); free(a); } diff --git a/src/array.h b/src/array.h index 1df3838b..d3c44c07 100644 --- a/src/array.h +++ b/src/array.h @@ -40,7 +40,7 @@ data_string *data_string_init(void); typedef struct { DATA_UNSET; - array *value; + array value; } data_array; data_array *data_array_init(void); @@ -57,8 +57,10 @@ __attribute_returns_nonnull__ array *array_init(void); __attribute_cold__ -__attribute_returns_nonnull__ -array *array_init_array(const array *a); +void array_copy_array(array *dst, const array *src); + +__attribute_cold__ +void array_free_data(array *a); void array_free(array *a); diff --git a/src/configfile-glue.c b/src/configfile-glue.c index f32be7a9..63d84511 100644 --- a/src/configfile-glue.c +++ b/src/configfile-glue.c @@ -54,8 +54,8 @@ int config_insert_values_internal(server *srv, const array *ca, const config_val size_t j; const data_array *da = (const data_array *)du; - for (j = 0; j < da->value->used; j++) { - data_unset *ds = da->value->data[j]; + for (j = 0; j < da->value.used; j++) { + data_unset *ds = da->value.data[j]; if (ds->type == TYPE_STRING || ds->type == TYPE_INTEGER || ds->type == TYPE_ARRAY) { array_insert_unique(cv[i].destination, ds->fn->copy(ds)); } else { diff --git a/src/configparser.y b/src/configparser.y index a4535fae..e0f5b6d9 100644 --- a/src/configparser.y +++ b/src/configparser.y @@ -95,8 +95,8 @@ static data_unset *configparser_merge_data(data_unset *op1, const data_unset *op ((data_integer *)op1)->value += ((data_integer *)op2)->value; break; case TYPE_ARRAY: { - array *dst = ((data_array *)op1)->value; - array *src = ((data_array *)op2)->value; + array *dst = &((data_array *)op1)->value; + array *src = &((data_array *)op2)->value; data_unset *du; size_t i; @@ -341,8 +341,7 @@ value(A) ::= INTEGER(B). { } value(A) ::= array(B). { A = (data_unset *)data_array_init(); - array_free(((data_array *)(A))->value); - ((data_array *)(A))->value = B; + array_copy_array(&((data_array *)(A))->value, B); B = NULL; } array(A) ::= LPARAN RPARAN. { diff --git a/src/data_array.c b/src/data_array.c index 4652e76a..4a583da8 100644 --- a/src/data_array.c +++ b/src/data_array.c @@ -11,8 +11,7 @@ static data_unset *data_array_copy(const data_unset *s) { data_array *ds = data_array_init(); if (!buffer_is_empty(&src->key)) buffer_copy_buffer(&ds->key, &src->key); - array_free(ds->value); - ds->value = array_init_array(src->value); + array_copy_array(&ds->value, &src->value); return (data_unset *)ds; } @@ -20,7 +19,7 @@ static void data_array_free(data_unset *d) { data_array *ds = (data_array *)d; free(ds->key.ptr); - array_free(ds->value); + array_free_data(&ds->value); free(d); } @@ -38,7 +37,7 @@ __attribute_cold__ static void data_array_print(const data_unset *d, int depth) { data_array *ds = (data_array *)d; - array_print(ds->value, depth); + array_print(&ds->value, depth); } data_array *data_array_init(void) { @@ -53,8 +52,6 @@ data_array *data_array_init(void) { ds = calloc(1, sizeof(*ds)); force_assert(NULL != ds); - ds->value = array_init(); - ds->type = TYPE_ARRAY; ds->fn = &fn; diff --git a/src/data_config.c b/src/data_config.c index 08e01f6c..21cef128 100644 --- a/src/data_config.c +++ b/src/data_config.c @@ -20,8 +20,7 @@ static data_unset *data_config_copy(const data_unset *s) { if (!buffer_is_empty(&src->key)) buffer_copy_buffer(&ds->key, &src->key); buffer_copy_buffer(ds->comp_tag, src->comp_tag); buffer_copy_buffer(ds->comp_key, src->comp_key); - array_free(ds->value); - ds->value = array_init_array(src->value); + array_copy_array(ds->value, src->value); return (data_unset *)ds; } diff --git a/src/gw_backend.c b/src/gw_backend.c index d6fac695..4f2e9e14 100644 --- a/src/gw_backend.c +++ b/src/gw_backend.c @@ -1185,7 +1185,7 @@ int gw_set_defaults_backend(server *srv, gw_plugin_data *p, const data_unset *du if (NULL == da) return 1; - if (da->type != TYPE_ARRAY || !array_is_kvarray(da->value)) { + if (da->type != TYPE_ARRAY || !array_is_kvarray(&da->value)) { log_error_write(srv, __FILE__, __LINE__, "s", "unexpected value for xxxxx.server; expected " "( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))"); @@ -1206,8 +1206,8 @@ int gw_set_defaults_backend(server *srv, gw_plugin_data *p, const data_unset *du * "" => ( ... ) ) */ - for (size_t j = 0; j < da->value->used; ++j) { - data_array *da_ext = (data_array *)da->value->data[j]; + for (size_t j = 0; j < da->value.used; ++j) { + data_array *da_ext = (data_array *)da->value.data[j]; /* * da_ext->key == name of the extension @@ -1221,8 +1221,8 @@ int gw_set_defaults_backend(server *srv, gw_plugin_data *p, const data_unset *du * "" => ... ) */ - for (size_t n = 0; n < da_ext->value->used; ++n) { - data_array *da_host = (data_array *)da_ext->value->data[n]; + for (size_t n = 0; n < da_ext->value.used; ++n) { + data_array *da_host = (data_array *)da_ext->value.data[n]; config_values_t fcv[] = { { "host", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */ @@ -1256,7 +1256,7 @@ int gw_set_defaults_backend(server *srv, gw_plugin_data *p, const data_unset *du }; unsigned short host_mode = GW_RESPONDER; - if (da_host->type != TYPE_ARRAY || !array_is_kvany(da_host->value)){ + if (da_host->type != TYPE_ARRAY || !array_is_kvany(&da_host->value)){ log_error_write(srv, __FILE__, __LINE__, "SBS", "unexpected value for gw.server near [", &da_host->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))"); @@ -1308,15 +1308,15 @@ int gw_set_defaults_backend(server *srv, gw_plugin_data *p, const data_unset *du fcv[21].destination = host->xsendfile_docroot; fcv[22].destination = &(host->tcp_fin_propagate); - if (0 != config_insert_values_internal(srv, da_host->value, fcv, T_CONFIG_SCOPE_CONNECTION)) { + if (0 != config_insert_values_internal(srv, &da_host->value, fcv, T_CONFIG_SCOPE_CONNECTION)) { goto error; } - for (size_t m = 0; m < da_host->value->used; ++m) { - if (NULL != strchr(da_host->value->data[m]->key.ptr, '_')) { + for (size_t m = 0; m < da_host->value.used; ++m) { + if (NULL != strchr(da_host->value.data[m]->key.ptr, '_')) { log_error_write(srv, __FILE__, __LINE__, "sb", "incorrect directive contains underscore ('_') instead of dash ('-'):", - &da_host->value->data[m]->key); + &da_host->value.data[m]->key); } } diff --git a/src/mod_auth.c b/src/mod_auth.c index be7f2c7a..201f2f7a 100644 --- a/src/mod_auth.c +++ b/src/mod_auth.c @@ -314,7 +314,7 @@ SETDEFAULTS_FUNC(mod_auth_set_defaults) { /* no auth.require for this section */ if (NULL == (da = (const data_array *)array_get_element_klen(config->value, CONST_STR_LEN("auth.require")))) continue; - if (da->type != TYPE_ARRAY || !array_is_kvarray(da->value)) { + if (da->type != TYPE_ARRAY || !array_is_kvarray(&da->value)) { log_error_write(srv, __FILE__, __LINE__, "ss", "unexpected value for auth.require; expected ", "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )"); @@ -322,15 +322,15 @@ SETDEFAULTS_FUNC(mod_auth_set_defaults) { } - for (n = 0; n < da->value->used; n++) { + for (n = 0; n < da->value.used; n++) { size_t m; - data_array *da_file = (data_array *)da->value->data[n]; + data_array *da_file = (data_array *)da->value.data[n]; const buffer *method = NULL, *realm = NULL, *require = NULL; const http_auth_scheme_t *auth_scheme; buffer *algos = NULL; int algorithm = HTTP_AUTH_DIGEST_SESS; - if (!array_is_kvstring(da_file->value)) { + if (!array_is_kvstring(&da_file->value)) { log_error_write(srv, __FILE__, __LINE__, "ss", "unexpected value for auth.require; expected ", "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )"); @@ -338,9 +338,9 @@ SETDEFAULTS_FUNC(mod_auth_set_defaults) { return HANDLER_ERROR; } - for (m = 0; m < da_file->value->used; m++) { - if (da_file->value->data[m]->type == TYPE_STRING) { - data_string *ds = (data_string *)da_file->value->data[m]; + for (m = 0; m < da_file->value.used; m++) { + if (da_file->value.data[m]->type == TYPE_STRING) { + data_string *ds = (data_string *)da_file->value.data[m]; if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("method"))) { method = &ds->value; } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("realm"))) { @@ -353,7 +353,7 @@ SETDEFAULTS_FUNC(mod_auth_set_defaults) { log_error_write(srv, __FILE__, __LINE__, "ssbs", "the field is unknown in:", "auth.require = ( \"...\" => ( ..., -> \"", - &da_file->value->data[m]->key, + &da_file->value.data[m]->key, "\" <- => \"...\" ) )"); return HANDLER_ERROR; @@ -362,7 +362,7 @@ SETDEFAULTS_FUNC(mod_auth_set_defaults) { log_error_write(srv, __FILE__, __LINE__, "ssbs", "a string was expected for:", "auth.require = ( \"...\" => ( ..., -> \"", - &da_file->value->data[m]->key, + &da_file->value.data[m]->key, "\" <- => \"...\" ) )"); return HANDLER_ERROR; diff --git a/src/mod_dirlisting.c b/src/mod_dirlisting.c index 49dd3bdf..2663bfb1 100644 --- a/src/mod_dirlisting.c +++ b/src/mod_dirlisting.c @@ -277,9 +277,9 @@ SETDEFAULTS_FUNC(mod_dirlisting_set_defaults) { } if (NULL != (du_excludes = array_get_element_klen(config->value, CONST_STR_LEN(CONFIG_EXCLUDE)))) { - array *excludes_list; + const array *excludes_list; - excludes_list = ((const data_array*)du_excludes)->value; + excludes_list = &((const data_array*)du_excludes)->value; if (du_excludes->type != TYPE_ARRAY || !array_is_vlist(excludes_list)) { log_error_write(srv, __FILE__, __LINE__, "s", diff --git a/src/mod_openssl.c b/src/mod_openssl.c index 84000ccd..63768b2b 100644 --- a/src/mod_openssl.c +++ b/src/mod_openssl.c @@ -1281,9 +1281,10 @@ SETDEFAULTS_FUNC(mod_openssl_set_defaults) if (!buffer_string_is_empty(b)) buffer_copy_buffer(s->ssl_cipher_list, b); } - s->ssl_conf_cmd = (0 == i) - ? array_init() - : array_init_array(p->config_storage[0]->ssl_conf_cmd); + s->ssl_conf_cmd = array_init(); + if (0 != i) + array_copy_array(s->ssl_conf_cmd, + p->config_storage[0]->ssl_conf_cmd); s->ssl_acme_tls_1 = buffer_init(); cv[0].destination = &(s->ssl_log_noise); diff --git a/src/mod_proxy.c b/src/mod_proxy.c index 57ac1f16..0fe8e619 100644 --- a/src/mod_proxy.c +++ b/src/mod_proxy.c @@ -252,19 +252,19 @@ SETDEFAULTS_FUNC(mod_proxy_set_defaults) { && !buffer_is_equal_string(&ds->value, CONST_STR_LEN("0")); continue; } - if (da->type != TYPE_ARRAY || !array_is_kvstring(da->value)) { + if (da->type != TYPE_ARRAY || !array_is_kvstring(&da->value)) { log_error_write(srv, __FILE__, __LINE__, "sb", "unexpected value for proxy.header; expected ( \"param\" => ( \"key\" => \"value\" ) ) near key", &da->key); return HANDLER_ERROR; } if (buffer_is_equal_string(&da->key, CONST_STR_LEN("map-urlpath"))) { - s->header.urlpaths = da->value; + s->header.urlpaths = &da->value; } else if (buffer_is_equal_string(&da->key, CONST_STR_LEN("map-host-request"))) { - s->header.hosts_request = da->value; + s->header.hosts_request = &da->value; } else if (buffer_is_equal_string(&da->key, CONST_STR_LEN("map-host-response"))) { - s->header.hosts_response = da->value; + s->header.hosts_response = &da->value; } else { log_error_write(srv, __FILE__, __LINE__, "sb", diff --git a/src/mod_redirect.c b/src/mod_redirect.c index d8dd479f..37ede72e 100644 --- a/src/mod_redirect.c +++ b/src/mod_redirect.c @@ -91,14 +91,14 @@ SETDEFAULTS_FUNC(mod_redirect_set_defaults) { da = (const data_array *)du; - if (du->type != TYPE_ARRAY || !array_is_kvstring(da->value)) { + if (du->type != TYPE_ARRAY || !array_is_kvstring(&da->value)) { log_error_write(srv, __FILE__, __LINE__, "s", "unexpected value for url.redirect; expected list of \"regex\" => \"redirect\""); return HANDLER_ERROR; } - for (j = 0; j < da->value->used; j++) { - data_string *ds = (data_string *)da->value->data[j]; + for (j = 0; j < da->value.used; j++) { + data_string *ds = (data_string *)da->value.data[j]; if (srv->srvconf.http_url_normalize) { pcre_keyvalue_burl_normalize_key(&ds->key, srv->tmp_buf); pcre_keyvalue_burl_normalize_value(&ds->value, srv->tmp_buf); diff --git a/src/mod_rewrite.c b/src/mod_rewrite.c index eba398d0..01058a39 100644 --- a/src/mod_rewrite.c +++ b/src/mod_rewrite.c @@ -76,14 +76,14 @@ static int parse_config_entry(server *srv, array *ca, pcre_keyvalue_buffer *kvb, if (NULL != da) { size_t j; - if (da->type != TYPE_ARRAY || !array_is_kvstring(da->value)) { + if (da->type != TYPE_ARRAY || !array_is_kvstring(&da->value)) { log_error_write(srv, __FILE__, __LINE__, "SSS", "unexpected value for ", option, "; expected list of \"regex\" => \"subst\""); return HANDLER_ERROR; } - for (j = 0; j < da->value->used; j++) { - data_string *ds = (data_string *)da->value->data[j]; + for (j = 0; j < da->value.used; j++) { + data_string *ds = (data_string *)da->value.data[j]; if (srv->srvconf.http_url_normalize) { pcre_keyvalue_burl_normalize_key(&ds->key, srv->tmp_buf); pcre_keyvalue_burl_normalize_value(&ds->value, srv->tmp_buf);