From bbd958382e596a86b07eb7bfa1c675007a31bb49 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Tue, 15 Dec 2020 04:40:43 -0500 Subject: [PATCH] [core] reorder srv->config_context to match ndx (fixes #3047) (thx altblue) reorder dc->context_ndx to match srv->config_context->data[] index. srv->config_context->data[] may have been re-ordered in configparser.y. Since the dc->context_ndx (id) is reused by config_insert*() and by plugins to index into srv->config_context->data[], reorder into the order encountered during config file parsing for least surprise to end-users writing config files. x-ref: "mismatched nested config conditions in lighttpd 1.4.56" https://redmine.lighttpd.net/issues/3047 --- src/configfile.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/configfile.c b/src/configfile.c index 85c146a3..6b5bf7a6 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -2190,6 +2190,27 @@ int config_read(server *srv, const char *fn) { return ret; } + /* reorder dc->context_ndx to match srv->config_context->data[] index. + * srv->config_context->data[] may have been re-ordered in configparser.y. + * Since the dc->context_ndx (id) is reused by config_insert*() and by + * plugins to index into srv->config_context->data[], reorder into the + * order encountered during config file parsing for least surprise to + * end-users writing config files. Note: this manipulation *breaks* the + * srv->config_context->sorted[] structure, so searching the array by key + * is no longer valid. */ + for (uint32_t i = 0; i < srv->config_context->used; ++i) { + dc = (data_config *)srv->config_context->data[i]; + if (dc->context_ndx == (int)i) continue; + for (uint32_t j = i; j < srv->config_context->used; ++j) { + dc = (data_config *)srv->config_context->data[j]; + if (dc->context_ndx == (int)i) { + srv->config_context->data[j] = srv->config_context->data[i]; + srv->config_context->data[i] = (data_unset *)dc; + break; + } + } + } + if (0 != config_insert_srvconf(srv)) { return -1; }